<?php
namespace App\Modules\HR\Reports\Exports;

use App\Modules\HR\Reports\Exports\Concerns\ProfessionalExcelStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class PaymentDetailsExport implements FromCollection, WithHeadings, WithColumnWidths, WithStyles, WithTitle
{
    use ProfessionalExcelStyles;

    protected $data;

    public function __construct($data) { $this->data = $data['employees']; }

    public function title(): string { return 'Payment Details'; }

    public function headings(): array
    {
        return ['#','Emp Code','EPF No','Name','Department','Designation','Date of Join','Payment Method','Bank Code','Bank Name','Branch','Account Number'];
    }

    public function collection()
    {
        return $this->data->values()->map(function ($emp, $i) {
            return [
                $i + 1,
                $emp->employee_code,
                $emp->epf_no,
                trim($emp->first_name . ' ' . ($emp->middle_name ?? '') . ' ' . $emp->last_name),
                optional($emp->department)->name,
                optional($emp->designation)->name,
                $emp->date_of_joining,
                optional($emp->paymentMethod)->name,
                optional($emp->bank)->code,
                optional($emp->bank)->name,
                optional($emp->bankBranch)->name,
                $emp->account_number,
            ];
        });
    }

    public function columnWidths(): array
    {
        return ['A'=>5,'B'=>13,'C'=>12,'D'=>30,'E'=>22,'F'=>22,'G'=>14,'H'=>18,'I'=>12,'J'=>22,'K'=>18,'L'=>20];
    }

    public function styles(Worksheet $sheet)
    {
        $last = $this->data->count() + 1;
        $this->applyStyles($sheet, $last, 'L', ['A','G','I'], []);
        return [];
    }
}
