<?php

namespace App\Nova\Resources\Order\Loan;

use App\Models\Order\Loan\LoanOrder as LoanOrderModel;
use App\Nova\Resource;
use App\Nova\Resources\Branch\Concerns\BranchNovaRepo;
use App\Nova\Resources\Order\Loan\Concerns\LoanOrderEvents;
use App\Nova\Resources\Order\Loan\Concerns\LoanOrderFieldsForIndex;
use App\Repos\Order\Loan\BranchRepo;
use App\Repos\Order\Loan\LoanTypeRepo;
use App\Repos\System\Settings\Legal\EducationRepo;
use App\Repos\System\Settings\Legal\MarriageRepo;
use App\Repos\System\Settings\Legal\PassportRepo;
use App\Repos\System\Settings\Location\RegionRepo;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Email;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Nurmuhammet\NovaInputmask\NovaInputmask;

class LoanOrder extends Resource
{
    use LoanOrderEvents;

    /**
     * The model the resource corresponds to.
     *
     * @var class-string<LoanOrderModel>
     */
    public static $model = LoanOrderModel::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'unique_id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'unique_id', 'customer_name', 'customer_surname',
    ];

    /**
     * Indicates whether the resource should automatically poll for new resources.
     *
     * @var bool
     */
    public static $polling = true;

    /**
     * The interval at which Nova should poll for new resources.
     *
     * @var int
     */
    public static $pollingInterval = 120;

    /**
     * Indicates whether to show the polling toggle button inside Nova.
     *
     * @var bool
     */
    public static $showPollingToggle = true;

    /**
     * Get the displayable label of the resource.
     */
    public static function label(): string
    {
        return __('Loan orders');
    }

    /**
     * Get the displayable singular label of the resource.
     */
    public static function singularLabel(): string
    {
        return __('Loan order');
    }

    /**
     * Get the fields for index.
     */
    public function fieldsForIndex(NovaRequest $request): array
    {
        return LoanOrderFieldsForIndex::make();
    }

    /**
     * Get the fields displayed by the resource.
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            new Panel(__('Loan'), [
                Select::make(__('Loan type'), 'loan_type')
                    ->displayUsingLabels()
                    ->fullWidth()
                    ->searchable()
                    ->options(LoanTypeRepo::values())
                    ->rules('required')
                    ->sortable(),
            ]),

            new Panel(__('Location'), [
                Select::make(__('Region'), 'region')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(RegionRepo::values())
                    ->default(RegionRepo::default())
                    ->size('w-1/2')
                    ->rules('required')
                    ->sortable(),

                Select::make(__('Branch'), 'branch_id')
                    ->displayUsingLabels()
                    ->searchable()
                    ->dependsOn('region', BranchNovaRepo::dependsOnRegion())
                    ->size('w-1/2')
                    ->rules('required')
                    ->sortable(),
            ]),

            new Panel(__('Personal data'), [
                Text::make(__('Name'), 'customer_name')
                    ->size('w-1/3')
                    ->rules('required', 'string', 'max:255'),

                Text::make(__('Surname'), 'customer_surname')
                    ->size('w-1/3')
                    ->rules('required', 'string', 'max:255'),

                Text::make(__('Patronic name'), 'customer_patronic_name')
                    ->size('w-1/3')
                    ->rules('required', 'string', 'max:255'),

                Select::make(__('Education'), 'education')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(EducationRepo::values())
                    ->default(EducationRepo::default())
                    ->size('w-1/3')
                    ->rules('required')
                    ->sortable(),

                Select::make(__('Marriage status'), 'marriage_status')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(MarriageRepo::values())
                    ->default(MarriageRepo::default())
                    ->size('w-1/3')
                    ->rules('required')
                    ->sortable(),

                Date::make(__('Date of birth'), 'born_at')
                    ->size('w-1/3')
                    ->rules('required'),

                Text::make(__('Residence (passport)'), 'passport_address')
                    ->size('w-1/2')
                    ->rules('required', 'string', 'max:255'),

                Text::make(__('Current Residence'), 'real_address')
                    ->size('w-1/2')
                    ->rules('required', 'string', 'max:255'),
            ]),

            new Panel(__('Passport'), [
                Select::make(__('Passport serie'), 'passport_serie')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(PassportRepo::values())
                    ->size('w-1/3')
                    ->rules('required')
                    ->sortable(),

                Number::make(__('Passport id'), 'passport_id')
                    ->size('w-1/3')
                    ->rules('required'),

                Date::make(__('Passport date of issue'), 'passport_given_at')
                    ->size('w-1/3')
                    ->rules('required'),

                Text::make(__('Passport given by'), 'passport_given_by')
                    ->size('w-1/2')
                    ->rules('required'),

                Text::make(__('Born place (passport)'), 'born_place')
                    ->size('w-1/2')
                    ->rules('required'),
            ]),

            new Panel(__('Contact data'), [
                Email::make(__('Email'), 'email')
                    ->size('w-1/4')
                    ->rules('nullable', 'email'),

                NovaInputmask::make(__('Phone'), 'phone')
                    ->phonenumber('TM')
                    ->size('w-1/4')
                    ->rules('required'),

                NovaInputmask::make(__('Phone Additional'), 'phone_additional')
                    ->phonenumber('TM')
                    ->size('w-1/4')
                    ->rules('nullable'),

                NovaInputmask::make(__('Home phone'), 'phone_home')
                    ->size('w-1/4')
                    ->rules('required'),
            ]),

            new Panel(__('Job'), [
                Text::make(__('Work company name'), 'work_company')
                    ->rules('required', 'string', 'max:255')
                    ->size('w-1/2'),

                NovaInputmask::make(__('HR department work number'), 'work_company_accountant_number')
                    ->size('w-1/2')
                    ->rules('required'),

                Select::make(__('Work region'), 'work_region')
                    ->displayUsingLabels()
                    ->searchable()
                    ->options(RegionRepo::values())
                    ->default(RegionRepo::default())
                    ->size('w-1/2')
                    ->rules('required')
                    ->sortable(),

                Select::make(__('Work province'), 'work_province')
                    ->displayUsingLabels()
                    ->searchable()
                    ->dependsOn('region', BranchNovaRepo::dependsOnRegion('work_region'))
                    ->size('w-1/2')
                    ->rules('required'),

                Text::make(__('Position'), 'work_position')
                    ->size('w-1/2')
                    ->rules('required'),

                Text::make(__('Salary'), 'work_salary')
                    ->size('w-1/4')
                    ->rules('required'),

                Date::make(__('Work started at'), 'work_started_at')
                    ->size('w-1/4')
                    ->rules('required'),
            ]),

            new Panel(__('Passport'), [
                Image::make(__('Passport (page 1)'), 'passport_one')
                    ->size('w-1/2')
                    ->rules('required', 'max:2048'),

                Image::make(__('Passport (page 2-3)'), 'passport_two')
                    ->size('w-1/2')
                    ->rules('required', 'max:2048'),

                Image::make(__('Passport (page 8-9)'), 'passport_three')
                    ->size('w-1/2')
                    ->rules('required', 'max:2048'),

                Image::make(__('Passport (page 32)'), 'passport_four')
                    ->size('w-1/2')
                    ->rules('required', 'max:2048'),
            ]),

            // $table->foreignId('filled_by')->constrained('users')->restrictOnDelete();
            // $table->foreignId('user_id')->constrained('users')->restrictOnDelete();
            // $table->string('status')->index();
            // $table->string('status_reason')->nullable();
            // $table->string('notes')->nullable();
        ];
    }

    /**
     * Get the cards available for the request.
     */
    public function cards(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     */
    public function filters(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     */
    public function lenses(NovaRequest $request): array
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     */
    public function actions(NovaRequest $request): array
    {
        return [];
    }
}
