<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PaymentTemplateResource\Pages;
use App\Models\PaymentTemplate;
use App\Enums\NavigationGroup;
use App\Enums\PaymentType;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\Repeater;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class PaymentTemplateResource extends Resource
{
    protected static ?string $model = PaymentTemplate::class;
    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-table-cells';
    protected static string | \UnitEnum | null $navigationGroup = NavigationGroup::FINANCE->value;
    protected static ?int $navigationSort = 1;

    protected static ?string $modelLabel = 'Mẫu thanh toán';
    protected static ?string $pluralModelLabel = 'Mẫu thanh toán';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Thông tin mẫu')
                    ->columns(2)
                    ->schema([
                        Select::make('project_id')->label('Dự án')->relationship('project', 'name')->required(),
                        TextInput::make('name')->label('Tên mẫu')->required(),
                    ]),
                Section::make('Chi tiết đợt')
                    ->schema([
                        Repeater::make('items')
                            ->label('Danh sách đợt')
                            ->relationship('items')
                            ->schema([
                                TextInput::make('installment_no')->label('Đợt')->numeric()->required(),
                                Select::make('type')->label('Loại')->options(PaymentType::class)->required(),
                                TextInput::make('percentage')->label('%')->numeric()->required(),
                                TextInput::make('days_after_signing')->label('Ngày sau ký (F0)')->numeric(),
                                TextInput::make('days_after_previous')->label('Ngày sau đợt trước')->numeric(),
                                \Filament\Forms\Components\DatePicker::make('due_date')->label('Ngày cố định'),
                            ])->columns(3),
                    ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('project.name')->label('Dự án'),
                Tables\Columns\TextColumn::make('name')->label('Tên mẫu'),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPaymentTemplates::route('/'),
            'create' => Pages\CreatePaymentTemplate::route('/create'),
            'edit' => Pages\EditPaymentTemplate::route('/{record}/edit'),
        ];
    }
}
