<?php
/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 10/27/18
 * Time: 10:30 AM
 */

namespace App\Exports;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class Exporter implements FromArray, WithTitle, WithEvents, ShouldAutoSize
{
    private $request;
    private $sheetName = '';

    public function __construct(Request $request){
        $this->request = $request;
    }

    public function title(): string
    {
        $this->sheetName = $this->request->get('sheet');
        return $this->request->get('sheet');
    }

    /**
     * @return array
     */
    public function array(): array
    {
        // TODO: Implement array() method.
        return $this->request->get('data');
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {

                if(strpos($this->sheetName,' Loan Applications') !== false){
                    $sheet = $event->sheet;

                    $sheet->mergeCells('A2:B2');
                    $sheet->setCellValue('A6', "Applicant");
    
                    $sheet->mergeCells('A56:B56');
                    $sheet->setCellValue('A11', "Solar Details");
    
                    $sheet->mergeCells('A66:B66');
                    $sheet->setCellValue('D11', "Co Applicant");
    
                    // $styleArray1 = [
                    //     'alignment' => [
                    //         'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                    //     ],
                    // ];
                    
                    $styleArray = [
                        'alignment' => [
                            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                        ],
                    ];
                    
                    $cellRange1 = 'A2:B2'; // All headers
                    $sheet->getDelegate()->getStyle($cellRange1)->applyFromArray($styleArray);
                    $cellRange2 = 'A56:B56'; // All headers
                    $sheet->getDelegate()->getStyle($cellRange2)->applyFromArray($styleArray);
                    $cellRange3 = 'A66:B66'; // All headers
                    $sheet->getDelegate()->getStyle($cellRange3)->applyFromArray($styleArray);
    
                    $sheet->getStyle('A2:B2')->applyFromArray([
                        'font' => [
                            'bold' => true
                        ]
                    ]);
    
                    $sheet->getStyle('A56:B56')->applyFromArray([
                        'font' => [
                            'bold' => true
                        ]
                    ]);

                    $sheet->getStyle('A66:B66')->applyFromArray([
                        'font' => [
                            'bold' => true
                        ]
                    ]);
    
                    $col = 'A';
                    while(true){
                        $tempCol = $col++;
                        $sheet->getColumnDimension($tempCol)->setAutoSize(true);
                        if($tempCol == $sheet->getHighestDataColumn()){
                            break;
                        }
                    }
                }else{

                    $event->sheet->getStyle('A1:AN1')->applyFromArray([
                        'font' => [
                            'bold' => true
                        ]
                    ]);
                }
            },
        ];
    }
}
