<?php

declare(strict_types=1);

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

namespace App\Filament\Resources\DberpSettlementWriteOffs\RelationManagers;

use App\Business\Settlement\Enums\SettlementWriteOffLineStatus;
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 LinesRelationManager extends RelationManager
{
    protected static string $relationship = 'lines';

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

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

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

    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('receiptOrder.code')
                    ->label(__('dberp.fields.receipt_code')),
                TextColumn::make('paymentOrder.code')
                    ->label(__('dberp.fields.payment_code')),
                TextColumn::make('receivableRecord.code')
                    ->label(__('dberp.fields.receivable_record_code')),
                TextColumn::make('payableRecord.code')
                    ->label(__('dberp.fields.payable_record_code')),
                TextColumn::make('write_off_amount')
                    ->label(__('dberp.fields.write_off_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('effective_amount')
                    ->label(__('dberp.fields.effective_amount'))
                    ->formatStateUsing(fn ($state): string => DberpNumberFormatter::money($state)),
                TextColumn::make('line_status')
                    ->label(__('dberp.fields.status'))
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => SettlementWriteOffLineStatus::tryFrom((string) $state)?->label() ?? (string) $state),
                TextColumn::make('business_date')
                    ->label(__('dberp.fields.business_time'))
                    ->dateTime(),
            ])
            ->headerActions([])
            ->recordActions([]);
    }
}
