<?php

namespace App\Filament\Widgets;

use App\Models\PaymentScheduleItem;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class UpcomingPaymentsTable extends BaseWidget
{
    protected int | string | array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                PaymentScheduleItem::query()
                    ->whereHas('schedule.contract')
                    ->whereDate('due_date', '>=', now())
                    ->whereDate('due_date', '<=', now()->addDays(30))
                    ->orderBy('due_date')
            )
            ->columns([
                Tables\Columns\TextColumn::make('schedule.contract.contract_number')
                    ->label('Số HĐ')
                    ->searchable(),
                Tables\Columns\TextColumn::make('installment_no')
                    ->label('Đợt')
                    ->alignCenter(),
                Tables\Columns\TextColumn::make('type')
                    ->label('Loại')
                    ->badge(),
                Tables\Columns\TextColumn::make('amount')
                    ->label('Số tiền')
                    ->money('VND'),
                Tables\Columns\TextColumn::make('due_date')
                    ->label('Ngày đến hạn')
                    ->date('d/m/Y')
                    ->color('danger'),
                Tables\Columns\TextColumn::make('remaining_amount')
                    ->label('Còn thiếu')
                    ->money('VND'),
            ])
            ->paginated([10, 25, 50]);
    }
}
