<?php

namespace App\Filament\Pages\Laporan;

use App\Models\SuratUtama;
use Carbon\Carbon;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;

class LaporanSurat extends Page implements HasForms
{
    use InteractsWithForms;

    protected static ?string $navigationIcon = 'heroicon-o-document-text';
    protected static ?string $navigationGroup = 'Laporan';
    protected static ?int $navigationSort = 7;
    protected static ?string $title = 'Laporan Semua Surat';

    protected static string $view = 'filament.pages.laporan.laporan-surat';

    public $start_date;
    public $end_date;
    public $tipe_surat = 'semua_surat'; // Default tipe surat
    public $records;

    public $tipeSuratOptions = [
        'semua_surat' => 'Semua Surat',
        'surat_kelahiran' => 'Surat Kelahiran',
        'surat_kematian' => 'Surat Kematian',
        'surat_keterangan_ahli_waris' => 'Surat Keterangan Ahli Waris',
        'surat_keterangan_domisili' => 'Surat Keterangan Domisili',
        'surat_keterangan_kehilangan' => 'Surat Keterangan Kehilangan',
        'surat_keterangan_skck' => 'Surat Keterangan SKCK',
        'surat_keterangan_usaha' => 'Surat Keterangan Usaha',
        'surat_pendatang' => 'Surat Keterangan Pendatang',
        'surat_pengantar_perkawinan' => 'Surat Pengantar Perkawinan',
        'surat_permohonan' => 'Surat Permohonan',
        'surat_pindah' => 'Surat Keterangan Pindah',
        'surat_sktm' => 'Surat Keterangan Tidak Mampu (SKTM)',
    ];

    public function mount()
    {
        $this->form->fill([
            'start_date' => now()->subMonth(),
            'end_date' => now(),
            'tipe_surat' => 'semua_surat',
        ]);
        $this->generateReport();
    }

    protected function getFormSchema(): array
    {
        return [
            Grid::make(2)
                ->schema([
                    DatePicker::make('start_date')
                        ->label('Tanggal Mulai')
                        ->default(now()->subMonth())
                        ->required()
                        ->reactive(),
                    DatePicker::make('end_date')
                        ->label('Tanggal Akhir')
                        ->default(now())
                        ->required()
                        ->reactive(),
                ]),
            Select::make('tipe_surat')
                ->label('Tipe Surat')
                ->options($this->tipeSuratOptions)
                ->default('semua_surat')
                ->required()
                ->reactive(),
        ];
    }

    public function generateReport()
    {
        $data = $this->form->getState();
        $startDate = Carbon::parse($data['start_date'])->startOfDay();
        $endDate = Carbon::parse($data['end_date'])->endOfDay();

        $query = SuratUtama::query();
        
        // Cek jika tipe surat bukan 'semua_surat', maka terapkan filter
        if ($data['tipe_surat'] !== 'semua_surat') {
            $query->where('id_tipe_surat', $data['tipe_surat']);
        }
        
        $this->records = $query->whereBetween('tanggal_pengajuan', [$startDate, $endDate])->get();
    }
}