<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Resources\DberpPurchaseOrders\RelationManagers;

use App\Filament\Resources\DberpPurchaseReceipts\DberpPurchaseReceiptResource;
use App\Support\DberpNumberFormatter;
use Filament\Actions\Action;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class ReceiptsRelationManager extends RelationManager
{
    protected static string $relationship = 'receipts';

    public static function getTitle(Model $ownerRecord, string $pageClass): string
    {
        return '分批到货记录';
    }

    protected static function getModelLabel(): ?string
    {
        return '分批到货单';
    }

    protected static function getPluralModelLabel(): ?string
    {
        return '分批到货记录';
    }

    public function form(Schema $schema): Schema
    {
        return $schema;
    }

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('arrival_date', 'desc')
            ->columns([
                TextColumn::make('code')
                    ->label(__('dberp.fields.purchase_receipt_code')),
                TextColumn::make('arrival_date')
                    ->label(__('dberp.fields.arrival_date'))
                    ->date(),
                TextColumn::make('total_arrival_quantity')
                    ->label(__('dberp.fields.arrived_quantity'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                TextColumn::make('total_received_quantity')
                    ->label(__('dberp.fields.receipt_quantity'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::quantity($state)),
                TextColumn::make('status')
                    ->label(__('dberp.fields.status'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => $state === 'effective' ? '已入库' : ($state === 'draft' ? '待登记' : ($state === 'voided' ? '已作废' : (string) $state))),
                TextColumn::make('delivery_note')
                    ->label(__('dberp.fields.delivery_note'))
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('handlerEmployee.name')
                    ->label(__('dberp.fields.handler_employee')),
                TextColumn::make('effective_at')
                    ->label(__('dberp.fields.effective_at'))
                    ->dateTime()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->headerActions([
                Action::make('create_arrival')
                    ->label(__('dberp.actions.register_arrival'))
                    ->url(fn (): string => DberpPurchaseReceiptResource::getUrl('create', ['purchase_order_id' => $this->getOwnerRecord()->getKey()])),
            ])
            ->recordActions([
                Action::make('open')
                    ->label(__('dberp.actions.view_purchase_receipt_documents'))
                    ->url(fn (Model $record): string => DberpPurchaseReceiptResource::getUrl('edit', ['record' => $record])),
            ]);
    }
}
