<?php

namespace App\Terms;

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

class RelatorioEstatisticoFaturamento
{

    public function getRelatorioEstatisticoConsultaPdf($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 FATURAMENTO - RECEBIMENTOS <br/>";
        $html .= " </h3>";
        $html .= "";

        $html .= '<table style="font-size: 13px" border="0" width="100%">
                 <tr>
                    <td style="text-align: center"><strong>Forma Pagamento</strong></td>
                    <td style="text-align: center"><strong>Pagamento</strong></td>
                    <td style="text-align: center"><strong>Cliente</strong></td>
                    <td style="text-align: center"><strong>Valor</strong></td>
                    <td style="text-align: center"><strong>Responsável</strong></td>
                </tr>';

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

        foreach ( $dados['recebimentos'] as $key => $value ) {
            $html .= '
                <tr>
                    <td style="text-align: center;">' . $value["id_forma_pagamento"] . ' </td>
                    <td style="text-align: center;">' . $value["descricao"] . ' </td>
                    <td style="text-align: center;">' . $value["cliente"] . ' </td>
                    <td style="text-align: center;">' . $value["valor"] . ' </td>
                    <td style="text-align: center;">' . $value["responsavel"] . ' </td>
                </tr>
            ';
        }
        $html .= '</table>';


        // VALOR TOTAL DE RECEBIMENTO
        $html .= '<h3>';
        $html .= " <div style='text-align: center;'>";
        $html .= " SOMATÓRIO DE RECEBIMENTOS <br/>";
        $html .= " </h3>";
        $html .= "";

        $html .= '<table style="font-size: 13px" border="0" width="100%">
                 <tr>
                    <td style="text-align: center"><strong>Forma Pagamento</strong></td>
                    <td style="text-align: center"><strong>Pagamento</strong></td>
                    <td style="text-align: center"><strong>Total</strong></td>
                </tr>';

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

        foreach ( $dados['recebimentos_total'] as $key => $value ) {
            $html .= '
                <tr>
                    <td style="text-align: center;">' . $value["id_forma_pagamento"] . ' </td>
                    <td style="text-align: center;">' . $value["descricao"] . ' </td>
                    <td style="text-align: center;">' . $value["total"] . ' </td>
                </tr>
            ';
        }
        $html .= '</table>';


        // ESTORNOS
        $html .= '<h3>';
        $html .= " <div style='text-align: center;'>";
        $html .= " RELATÓRIO ESTATÍSTICO DE FATURAMENTO - ESTORNOS <br/>";
        $html .= " </h3>";
        $html .= "";

        $html .= '<table style="font-size: 13px" border="0" width="100%">
                 <tr>
                    <td style="text-align: center"><strong>Forma Estorno</strong></td>
                    <td style="text-align: center"><strong>Estorno</strong></td>
                    <td style="text-align: center"><strong>Item</strong></td>
                    <td style="text-align: center"><strong>Cliente</strong></td>
                    <td style="text-align: center"><strong>Valor</strong></td>
                </tr>';

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

        foreach ( $dados['estornos'] as $key => $value ) {
            $html .= '
                <tr>
                    <td style="text-align: center;">' . $value["id_tipo_extorno"] . ' </td>
                    <td style="text-align: center;">' . $value["tipo_estorno"] . ' </td>
                    <td style="text-align: center;">' . $value["item"] . ' </td>
                    <td style="text-align: center;">' . $value["cliente"] . ' </td>
                    <td style="text-align: center;">' . $value["valor"] . ' </td>
                </tr>
            ';
        }
        $html .= '</table>';


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

        return $html;
    }



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

        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setTitle('Recebimentos');
        $cols = [
            "Forma Pagamento",
            "Pagamento",
            "Cliente",
            "Valor",
            "Responsável"
        ];

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

        foreach ( $dados['recebimentos'] as $value ) {
            $array_row [] = $value["id_forma_pagamento"];
            $array_row [] = $value["descricao"];
            $array_row [] = $value["cliente"];
            $array_row [] = $value["valor"];
            $array_row [] = $value["responsavel"];

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

        $sheet2 = $spreadsheet->createSheet();
        $sheet2->setTitle('Total de Recebimentos');
        $spreadsheet->setActiveSheetIndex(1);
        $cols = [
            "Forma Pagamento",
            "Descricao",
            "Total",
        ];

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

        foreach ( $dados['recebimentos_total'] as $value ) {
            $array_row [] = $value["id_forma_pagamento"];
            $array_row [] = $value["descricao"];
            $array_row [] = $value["total"];

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


        $sheet3 = $spreadsheet->createSheet();
        $sheet3->setTitle('Estornos');
        $spreadsheet->setActiveSheetIndex(2);
        $cols = [
            "Forma Estorno",
            "Estorno",
            "Item",
            "Cliente",
            "Valor",
            "Observação",
        ];

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

        foreach ( $dados['estornos'] as $value ) {
            $array_row [] = $value["id_tipo_extorno"];
            $array_row [] = $value["tipo_estorno"];
            $array_row [] = $value["item"];
            $array_row [] = $value["cliente"];
            $array_row [] = $value["valor"];
            $array_row [] = $value["observacao"];

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


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