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

class PresenteeismSummaryExport implements FromCollection, WithHeadings
{
    protected $grouped;

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

    public function collection()
    {
        $rows = collect();

        foreach ($this->grouped as $date => $departments) {

            // Date row
            $rows->push([$date, '']);

            foreach ($departments as $dept) {
                $rows->push([
                    $dept->department_name,
                    $dept->present_count
                ]);
            }
        }

        return $rows;
    }

    public function headings(): array
    {
        return [
            'Department / Date',
            'Present Count',
        ];
    }
}
