<?php

namespace App\Filament\Resources\SuratTugasResource\Pages;

use App\Filament\Resources\SuratTugasResource;
use Filament\Resources\Pages\CreateRecord;

class CreateSuratTugas extends CreateRecord
{
    protected static string $resource = SuratTugasResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['created_by'] = auth()->id();

        // Clear location_id if using custom location
        if (isset($data['location_type']) && $data['location_type'] === 'custom') {
            $data['location_id'] = null;
        } else {
            // Clear custom fields if using existing location
            $data['custom_location_name'] = null;
            $data['custom_latitude'] = null;
            $data['custom_longitude'] = null;
            $data['custom_address'] = null;
        }

        // Remove the temporary field
        unset($data['location_type']);

        return $data;
    }

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