<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpPaymentOrders\Pages;

use App\Filament\Resources\DberpPaymentOrders\DberpPaymentOrderResource;
use App\Filament\Resources\Pages\CreateRecord;
use App\Models\DberpPurchaseOrder;

class CreateDberpPaymentOrder extends CreateRecord
{
    protected static string $resource = DberpPaymentOrderResource::class;

    public function mount(): void
    {
        parent::mount();

        $purchaseOrderId = request()->integer('purchase_order_id');

        if ($purchaseOrderId <= 0) {
            return;
        }

        $order = DberpPurchaseOrder::query()->find($purchaseOrderId);

        if ($order === null) {
            return;
        }

        $prefill = [
            'purchase_order_id' => $order->getKey(),
            'supplier_id' => $order->supplier_id,
        ];

        $supplierId = request()->integer('supplier_id');

        if ($supplierId > 0) {
            $prefill['supplier_id'] = $supplierId;
        }

        $this->form->fill($prefill);
    }
}
