<?php

declare(strict_types=1);

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

namespace App\Filament\Resources\DberpSettlementStatements\RelationManagers;

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

class ItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'items';

    public static function getTitle(Model $ownerRecord, string $pageClass): string
    {
        return __('dberp.relations.statement_items');
    }

    protected static function getModelLabel(): ?string
    {
        return __('dberp.fields.statement_item');
    }

    protected static function getPluralModelLabel(): ?string
    {
        return __('dberp.relations.statement_items');
    }

    public function form(Schema $schema): Schema
    {
        return $schema->components([]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('line_no', 'asc')
            ->columns([
                TextColumn::make('line_no')
                    ->label(__('dberp.fields.line_no')),
                TextColumn::make('business_time')
                    ->label(__('dberp.fields.business_time'))
                    ->dateTime(),
                TextColumn::make('source_type')
                    ->label(__('dberp.fields.source_type'))
                    ->formatStateUsing(fn (?string $state): string => __('dberp.options.settlement_source_type')[$state] ?? (string) $state),
                TextColumn::make('source_document_no')
                    ->label(__('dberp.fields.document_code')),
                TextColumn::make('summary')
                    ->label(__('dberp.fields.summary')),
                TextColumn::make('debit_amount')
                    ->label(__('dberp.fields.debit_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('credit_amount')
                    ->label(__('dberp.fields.credit_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('net_change_amount')
                    ->label(__('dberp.fields.net_change_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('balance_after')
                    ->label(__('dberp.fields.balance_after'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
            ])
            ->headerActions([])
            ->recordActions([]);
    }
}
