<?php

namespace App\Filament\Resources\Contracts\ContractResource\RelationManagers;

use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Actions\ViewAction;
use Filament\Actions\EditAction;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Placeholder;

class ScheduleItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'scheduleItems';

    protected static ?string $title = 'Lịch thanh toán thực tế';

    protected static ?string $modelLabel = 'Đợt thanh toán';
    protected static ?string $pluralModelLabel = 'Đợt thanh toán';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Chi tiết đợt thanh toán')
                    ->columns(2)
                    ->schema([
                        Placeholder::make('installment_no')->label('Đợt số')->content(fn ($record) => $record->installment_no),
                        Placeholder::make('type')->label('Loại đợt')->content(fn ($record) => $record->type?->getLabel()),
                        TextInput::make('amount')->label('Số tiền (VND)')->numeric()->required(),
                        DatePicker::make('due_date')->label('Ngày đến hạn')->required(),
                    ]),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('installment_no')->label('Đợt')->badge(),
                TextColumn::make('type')->label('Loại')->badge(),
                TextColumn::make('amount')->label('Số tiền dự kiến')->money('VND'),
                TextColumn::make('due_date')->label('Hạn thanh toán')->date('d/m/Y'),
            ])
            ->defaultSort('installment_no', 'asc')
            ->actions([
                ViewAction::make(),
                EditAction::make(),
            ]);
    }
}
