<?php

declare(strict_types=1);


namespace Plugins\Neatstudio\Payment\Filament\Resources;

use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Plugins\Neatstudio\Payment\Filament\Resources\Pages\ListPayments;
use Plugins\Neatstudio\Payment\Models\Payment;

class PaymentResource extends Resource
{
    protected static \UnitEnum|string|null $navigationGroup = '管理';

    protected static ?string $model = Payment::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedBanknotes;

    protected static ?string $navigationLabel = '订单';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                \Filament\Forms\Components\TextInput::make('order_no')->label('订单号')->disabled(),
                \Filament\Forms\Components\TextInput::make('subject')->label('商品')->disabled(),
                \Filament\Forms\Components\TextInput::make('amount')->label('金额（分）')->numeric()->disabled(),
                \Filament\Forms\Components\TextInput::make('channel')->label('渠道')->disabled(),
                \Filament\Forms\Components\TextInput::make('status')->label('状态')->disabled(),
                \Filament\Forms\Components\DateTimePicker::make('paid_at')->label('支付时间')->disabled(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                \Filament\Tables\Columns\TextColumn::make('order_no')->label('订单号')->searchable(),
                \Filament\Tables\Columns\TextColumn::make('user.name')->label('用户'),
                \Filament\Tables\Columns\TextColumn::make('subject')->label('商品')->limit(30),
                \Filament\Tables\Columns\TextColumn::make('amount')->label('金额')
                    ->formatStateUsing(fn ($state) => '¥'.number_format((float) $state / 100, 2)),
                \Filament\Tables\Columns\TextColumn::make('channel')->label('渠道')->badge(),
                \Filament\Tables\Columns\TextColumn::make('status')->label('状态')
                    ->badge()
                    ->formatStateUsing(fn ($state) => match ($state) {
                        'paid' => '已支付',
                        'pending' => '待支付',
                        'failed' => '失败',
                        'closed' => '已关闭',
                        default => $state,
                    })
                    ->color(fn ($state) => match ($state) {
                        'paid' => 'success',
                        'pending' => 'warning',
                        default => 'gray',
                    }),
                \Filament\Tables\Columns\TextColumn::make('paid_at')->label('支付时间')->dateTime('Y-m-d H:i'),
                \Filament\Tables\Columns\TextColumn::make('created_at')->label('创建时间')->dateTime('Y-m-d H:i'),
            ])
            ->filters([
                \Filament\Tables\Filters\SelectFilter::make('status')
                    ->options(['paid' => '已支付', 'pending' => '待支付', 'failed' => '失败', 'closed' => '已关闭']),
            ])
            ->recordActions([])
            ->defaultSort('created_at', 'desc');
    }

    public static function getPages(): array
    {
        return [
            'index' => ListPayments::route('/'),
        ];
    }
}
