<?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 EmployeeBirthdayExport implements FromCollection, WithHeadings, WithColumnWidths, WithStyles, WithTitle
{
    use ProfessionalExcelStyles;

    protected $data;

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

    public function title(): string { return 'Employee Birthdays'; }

    public function headings(): array
    {
        return ['#','Emp Code','EPF No','Name','Department','Designation','Team','Phone','DOB','Age'];
    }

    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,
                optional($emp->team)->name,
                $emp->mobile_number,
                $emp->date_of_birth,
                $emp->age,
            ];
        });
    }

    public function columnWidths(): array
    {
        return ['A'=>5,'B'=>14,'C'=>12,'D'=>32,'E'=>22,'F'=>22,'G'=>14,'H'=>15,'I'=>14,'J'=>8];
    }

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