<?php

namespace App\Filament\Resources\SuratTugasResource\Pages;

use App\Filament\Resources\SuratTugasResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditSuratTugas extends EditRecord
{
    protected static string $resource = SuratTugasResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\ViewAction::make(),
            Actions\DeleteAction::make(),
        ];
    }

    protected function mutateFormDataBeforeSave(array $data): array
    {
        // 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');
    }
}
