<?php

namespace App\Filament\Resources\PengajuanSurats\Pages;

use App\Filament\Resources\PengajuanSurats\PengajuanSuratResource;
use App\Models\PengajuanSurat;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Str;

class CreatePengajuanSurat extends CreateRecord
{
    protected static string $resource = PengajuanSuratResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // Auto-generate nomor tiket dengan prefix SRT-
        do {
            $nomorTiket = 'SRT-' . strtoupper(Str::random(8));
        } while (PengajuanSurat::where('nomor_tiket', $nomorTiket)->exists());

        $data['nomor_tiket'] = $nomorTiket;

        return $data;
    }

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }

    public function getTitle(): string
    {
        return 'Buat Pengajuan Surat Baru';
    }
}
