<?php

namespace App\Exports;

use App\Models\Leave;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class LeaveExport implements FromCollection, WithHeadings
{
    /**
     * Headings for the export.
     */
    public function headings(): array
    {
        return ['Sr.', 'Employee', 'Leave Type', 'From Date', 'To Date', 'Total Days', 'Reason', 'Attachment', 'Status', 'Approved By'];
    }

    /**
     * Collection of rows for Excel export.
     */
    public function collection()
    {
        $data = Leave::with(['employee', 'leaveType'])
->latest()
->whereNull('deleted_at')
->get();

        // Add serial numbers
        $sr = 1;
        return $data->map(function ($row) use (&$sr) {
            return [
                $sr++,
                strip_tags($row->employee ? $row->employee->first_name : ''), strip_tags($row->leaveType ? $row->leaveType->name : ''), strip_tags($row->from_date), strip_tags($row->to_date), strip_tags($row->total_days), strip_tags($row->reason), strip_tags($row->attachment), strip_tags($row->status), strip_tags($row->approved_by)
            ];
        });
    }
}
