<?php

namespace App\Filament\Resources;

use App\Filament\Exports\RequestQuotationExporter;
use App\Filament\Imports\RequestQuotationImporter;
use App\Filament\Resources\RequestQuotationResource\Pages;
use App\Filament\Resources\RequestQuotationResource\RelationManagers;
use App\Models\RequestQuotation;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Auth;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Actions\ExportAction;

class RequestQuotationResource extends Resource
{
    protected static ?string $model = RequestQuotation::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $navigationGroup = 'Request For Quotation';

    public static function form(Form $form): Form
    {
        $canEdit = Auth::user()?->can('edit_transport_fields');
        $canEditData = Auth::user()?->can('edit_all_transport_fields');
        return $form

            ->schema([
                Forms\Components\TextInput::make('rfq_number')
                    ->label('RFQ Number')
                    ->readOnly()
                    ->required(),
                Forms\Components\Select::make('spot_rate_transport_master_id')
                    ->label('Group Name')
                    ->relationship('spotRateTransportMaster', 'group_name')
                    ->required(),
                Forms\Components\DateTimePicker::make('rfq_date_time')
                    ->label('RFQ Date Time')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('pickup_address')
                    ->label('PickUp Address')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('delivery_address')
                    ->label('Delivery Address')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('weight')
                    ->label('Weight')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('volumetrice_size_inch')
                    ->label('Volumetrice')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('type_of_vehicle')
                    ->label('Type of Vehicle')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('special_type')
                    ->label('Special Type')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('no_of_vehicle')
                    ->label('No of Vehicle')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('product_name')
                    ->label('Product Name')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('loading_by')
                    ->label('Loading By')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('unloading_by')
                    ->label('Unloading By')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('pick_and_delivery')
                    ->label('Pick and Delivery')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('payment_term')
                    ->label('Payment Term')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\TextInput::make('paid_topay')
                    ->label('Paid Today')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\DateTimePicker::make('require_date_time')
                    ->label('Require Date Time')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                Forms\Components\DateTimePicker::make('rfq_rec_on_or_before')
                    ->label('RFQ Receive On Or Before')
                    ->disabled(fn () => !$canEditData)
                    ->required(),
                // Forms\Components\TextInput::make('transporter_name')
                //     ->label('Transporter Name')
                //     ->disabled(fn () => !($canEdit || $canEditData)),
                // Forms\Components\TextInput::make('total_freight_charge')
                //     ->label('Total Freight Charge')
                //     ->disabled(fn () => !($canEdit || $canEditData)),
                // Forms\Components\TextInput::make('transit_day')
                //     ->label('Transit Day')
                //     ->disabled(fn () => !($canEdit || $canEditData)),
                Forms\Components\Hidden::make('created_by')
                    ->label('Created By'),
                Forms\Components\Hidden::make('updated_by')
                    ->label('Updated By'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('No.')
                    ->label('No.')
                    ->alignCenter()
                    ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
                        $paginator = $livewire->getTableRecords();
                        $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
                        $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;

                        return ($currentPage - 1) * $perPage + $rowLoop->iteration;
                    }),
                Tables\Columns\TextColumn::make('rfq_number')
                    ->label('RFQ Number')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('spotRateTransportMaster.group_name')
                    ->label('Group Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('rfq_date_time')
                    ->label('RFQ Date Time')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('pickup_address')
                    ->label('PickUp Address')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('delivery_address')
                    ->label('Delivery Address')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('weight')
                    ->label('Weight')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('volumetrice_size_inch')
                    ->label('Volumetrice Size Inch')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('type_of_vehicle')
                    ->label('Type Of Vehicle')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('product_name')
                    ->label('Product Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('loading_by')
                    ->label('Loading By')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('unloading_by')
                    ->label('Unloading By')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('unloading_by')
                    ->label('Unloading By')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('pick_and_delivery')
                    ->label('Pick and Delivery')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('payment_term')
                    ->label('Payment Term')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('paid_topay')
                    ->label('Paid Today')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('require_date_time')
                    ->label('Require Date Time')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('rfq_rec_on_or_before')
                    ->label('RFQ Receive On Or Before')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('transporter_name')
                    ->label('Transporter Name')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('total_freight_charge')
                    ->label('Total Freight Charge')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('transit_day')
                    ->label('Transit Day')
                    ->alignCenter()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Updated At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('deleted_at')
                    ->label('Deleted At')
                    ->alignCenter()
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ])
            ->headerActions([
                ImportAction::make()
                    ->label('Import  RequestQuotation')
                    ->color('warning')
                    ->importer(RequestQuotationImporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view import request quotation');
                    }),
                ExportAction::make()
                    ->label('Export RequestQuotation')
                    ->color('warning')
                    ->exporter(RequestQuotationExporter::class)
                    ->visible(function() {
                        return Filament::auth()->user()->can('view export request quotation');
                    }),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListRequestQuotations::route('/'),
            'create' => Pages\CreateRequestQuotation::route('/create'),
            'view' => Pages\ViewRequestQuotation::route('/{record}'),
            'edit' => Pages\EditRequestQuotation::route('/{record}/edit'),
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
