<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ExistRule;
use App\Rules\LoanDateRule;
use EF;

class LoanRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }



    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        EF::errorMessage('Required form validation.');

        $current_path = url()->current();

        if($current_path === url('loancommit'))
        {
            $validation = $this->getValidationList();

            $validation['status'] = 'nullable';

            return $validation;
        }
         elseif($current_path === url('loanupdate'))
        {
            $validation = $this->getValidationList();

            $validation += ['loan' => ['required', new ExistRule('loans','id')]];



            $validation['employee'] = 'nullable';



            return $validation;
           
        }
        elseif($current_path === url('loandateupdate'))
        {
            return $this->loanDateValidation();
        }
        elseif($current_path === url('loanvoucher'))
        {

            return $this->loanApproveValidation();

        }
    }

    public function getValidationList()
    {

        return [

            
            'employee'        =>['required', new ExistRule('users','id')],

            'interest_rate'   =>'required|integer|bail|gte:0|between:0,100',

            'no_of_months'    =>'required|integer|bail|gt:0',

            'amount'          =>'required|integer|bail|gt:0',

            'net_amount'      =>'required|integer|bail|gt:0',

            'loan_form'             => 'nullable|mimes:pdf,png,jpeg,jpg|max:10240',
            
        ];
    }

    public function loanDateValidation()
    {
     $validate = $this->input();
       
        return
        [
         'installment_id.*'   => ['required', new ExistRule('deductions','id')],

         'loan_id'            => ['required', new ExistRule('loans','id')],

         'installment_date'   => ['nullable',new LoanDateRule($validate)]


        ];
    }

    public function loanApproveValidation()
    {

        $mode_list = implode(',', EF::getPaymentMode());
         $label_list = EF::getWhiteLabel(); 
       
        return
        [

             'loan'                                   => ['required', new ExistRule('loans','id')],
             $label_list['payment_mode']              =>  'In:'.$mode_list ,
             'drawn_on'                               =>   ['nullable', new ExistRule('accounts','id',false)],
             'instrument'                             =>  'nullable|min:0|max:50',
             'instrument_date'                        =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',

         
        ];
    }
}

