<?php
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class TimesheetDetailsExport implements FromCollection, WithHeadings
{
    protected $records;

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

    public function collection()
    {
        return $this->records->map(function ($row) {
            return [
                optional($row->attendance)->attendance_date,
                $row->employee->employee_code,
                $row->employee->first_name.' '.$row->employee->last_name,
                optional($row->employee->department)->name,
                optional($row->employee->designation)->name,
                optional($row->in_time)->format('H:i'),
                optional($row->out_time)->format('H:i'),
                $row->attendance_status,
                gmdate('H:i', $row->ot_minutes * 60),
                gmdate('H:i', $row->dot_minutes * 60),
                $row->late_minutes,
            ];
        });
    }

    public function headings(): array
    {
        return [
            'Date',
            'Emp Code',
            'Emp Name',
            'Department',
            'Designation',
            'In Time',
            'Out Time',
            'Status',
            'Single OT',
            'Double OT',
            'Late Minutes',
        ];
    }
}