<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpReceiptOrders\Pages;

use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Filament\Resources\Pages\CreateRecord;
use App\Models\DberpSalesOrder;

class CreateDberpReceiptOrder extends CreateRecord
{
    protected static string $resource = DberpReceiptOrderResource::class;

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

        $salesOrderId = request()->integer('sales_order_id');

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

        $order = DberpSalesOrder::query()->find($salesOrderId);

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

        $prefill = [
            'sales_order_id' => $order->getKey(),
            'customer_id' => $order->customer_id,
        ];

        $customerId = request()->integer('customer_id');

        if ($customerId > 0) {
            $prefill['customer_id'] = $customerId;
        }

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