<?php

namespace App\Exports;

use App\Models\Category;
use App\Models\Supplier;
use App\Models\Unit;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class MedicinesTemplateExport implements WithHeadings, WithEvents
{
    public function headings(): array
    {
        return [
            'Kategori',
            'Satuan',
            'Supplier',
            'Kode Obat',
            'Nama Obat',
            'Harga Beli',
            'Harga Jual',
            'Stok Minimum',
            'Stok Awal',
            'Rak',
            'Nomor Batch',
            'Tanggal Expired (YYYY-MM-DD)'
        ];
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $categories = Category::pluck('name')->toArray();
                $units = Unit::pluck('name')->toArray();
                $suppliers = Supplier::pluck('name')->toArray();

                $spreadsheet = $event->sheet->getDelegate()->getParent();
                $dropdownSheet = new Worksheet($spreadsheet, 'Dropdowns');
                $spreadsheet->addSheet($dropdownSheet);

                // Populate Dropdowns sheet
                foreach ($categories as $i => $val) { $dropdownSheet->setCellValue('A'.($i+1), $val); }
                foreach ($units as $i => $val) { $dropdownSheet->setCellValue('B'.($i+1), $val); }
                foreach ($suppliers as $i => $val) { $dropdownSheet->setCellValue('C'.($i+1), $val); }

                // Hide the sheet
                $dropdownSheet->setSheetState(Worksheet::SHEETSTATE_HIDDEN);

                $sheet = $event->sheet->getDelegate();

                $applyValidation = function($column, $formula) use ($sheet) {
                    $validation = $sheet->getCell($column . '2')->getDataValidation();
                    $validation->setType(DataValidation::TYPE_LIST);
                    $validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
                    $validation->setAllowBlank(true);
                    $validation->setShowInputMessage(true);
                    $validation->setShowErrorMessage(true);
                    $validation->setShowDropDown(true);
                    $validation->setErrorTitle('Input Error');
                    $validation->setError('Sebaiknya pilih dari daftar dropdown.');
                    $validation->setFormula1($formula);

                    for ($i = 2; $i <= 1000; $i++) {
                        $sheet->getCell($column . $i)->setDataValidation(clone $validation);
                    }
                };

                if (count($categories) > 0) $applyValidation('A', 'Dropdowns!$A$1:$A$'.count($categories));
                if (count($units) > 0) $applyValidation('B', 'Dropdowns!$B$1:$B$'.count($units));
                if (count($suppliers) > 0) $applyValidation('C', 'Dropdowns!$C$1:$C$'.count($suppliers));
            },
        ];
    }
}
