<?php

declare(strict_types=1);

namespace App\Filament\Resources\RationReports\Pages;

use App\Filament\Resources\RationReports\RationReportResource;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\RestoreAction;
use Filament\Resources\Pages\EditRecord;

class EditRationReport extends EditRecord
{
    protected static string $resource = RationReportResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('registerInvoice')
                ->label('ثبت فاکتور')
                ->icon('heroicon-o-document-text')
                ->color('gray')
                ->url(fn () => \App\Filament\Resources\Invoices\InvoiceResource::getUrl('index', [
                    'tableFilters' => [
                        // keep simple query for frontend/admin deep-link
                    ],
                ]).'?report_id='.$this->getRecord()->getKey().'&create_invoice=1')
                ->openUrlInNewTab(false),
            DeleteAction::make(),
            ForceDeleteAction::make(),
            RestoreAction::make(),
        ];
    }

    protected function getFormActions(): array
    {
        return [
            Action::make('saveDraft')
                ->label('ذخیره پیش‌نویس')
                ->color('gray')
                ->action('saveDraft'),
            $this->getSaveFormAction()->label('ویرایش/ذخیره نهایی'),
            $this->getCancelFormAction(),
        ];
    }

    public function saveDraft(): void
    {
        $this->form->fill(array_merge($this->form->getState(), ['status' => 'draft']));
        $this->save();
    }

    protected function getSavedNotificationTitle(): ?string
    {
        if ($this->getRecord() && $this->getRecord()->status === 'draft') {
            return 'جیره به صورت پیش‌نویس ذخیره شد.';
        }
        return parent::getSavedNotificationTitle();
    }
}
