<?php

declare(strict_types=1);

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])),
            ]);
    }
}
