<?php

namespace App\Terms;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class RelatorioEstatisticoProcedimentoExecutado
{

    public function getRelatorioEstatisticoProcedimentoExecutadoPdf($dados)
    {
        $html = '';
        $html .= '<html>';
        $html .= '<head>';
        $html .= '	<title></title>';
        $html .= '	<style>
	                    h3 {
	                        margin: 40px 0px;
	                    }
	                    .info{
	                        margin: 40px 0px;
	                    }
	                    .rodape{
	                        margin-top: 350px;
	                        text-align: center;
	                    }
	
	
                    </style>';
        $html .= '</head>';
        $html .= '<body>';
        $html .= '<h3>';
        $html .= " <div style='text-align: center;'>";
        $html .= " RELATÓRIO ESTATÍSTICO DE PROCEDIMENTO EXECUTADO <br/>";
        $html .= " </h3>";
        $html .= "";

        $html .= '<table style="font-size: 13px" border="0" width="100%">
                 <tr>
                    <td style="text-align: center"><strong>Procedimento</strong></td>
                    <td style="text-align: center"><strong>Data de Execução</strong></td>
                    <td style="text-align: center"><strong>Convenio</strong></td>
                    <td style="text-align: center"><strong>Unidade</strong></td>
                </tr>';

        $html .= '
            <tr>
                <td colspan="12"><hr></td>
            </tr>
        ';

        // FOREACH PETS
        foreach ( $dados as $key => $value ) {
            $html .= '
                <tr>
                    <td style="text-align: center;">' . $value["procedimento_descricao"] . ' </td>
                    <td style="text-align: center;">' . date( "d/m/Y", strtotime( $value["data_execucao"] ) ) . ' </td>
                    <td style="text-align: center;">' . $value["convenio_descricao"] . ' </td>
                    <td style="text-align: center;">' . $value["unidade_descricao"] . ' </td>
                </tr>
            ';
        }
        $html .= '</table>';


        $html .= "</body>";
        $html .= "</html>";

        return $html;
    }



    public function getRelatorioEstatisticoProcedimentoExecutadoExcel($dados) {
        header( 'Content-Type: application/vnd.ms-excel' );
        header( 'Content-Disposition: attachment;filename="relatorio-estatistico-procedimento.xlsx"' );
        $spreadsheet = new Spreadsheet();

        $sheet = $spreadsheet->getActiveSheet();
        $cols = [
            "Procedimento",
            "Data de Execução",
            "Convenio",
            "Unidade"
        ];

        $sheet->fromArray( $cols, null, 'A1' );
        $row_start = 2;

        foreach ( $dados as $value ) {
            $array_row [] = $value["procedimento_descricao"];
            $array_row [] = date( "d/m/Y", strtotime( $value["data_execucao"] ) );
            $array_row [] = $value["convenio_descricao"];
            $array_row [] = $value["unidade_descricao"];

            $sheet->fromArray( $array_row, null, 'A' . $row_start );
            unset( $array_row );
            $row_start++;
        }

        $writer = new Xlsx( $spreadsheet );
        return $writer;
    }
}