<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ContractVerificationRule;
use App\Rules\ExistRule;
use Crypt;
use EF;
class ContractRequest 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()
    {
        $form_collect = $this->input();
        
        EF::errorMessage('Required form validation.');

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

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

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

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


            $validation += ['contract' => ['required', new ExistRule('contracts','id'), new ContractVerificationRule($form_collect,$this->input('contract')) ]];



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



            return $validation;
           
        }
        elseif($current_path === url('allotcontract'))
        {
            return $this->storeDetailByContract();
        }
        elseif($current_path === url('allowanceassign'))
        {

            return $this->storeDetailByAllowance();

        }
        elseif($current_path === url('assetassign'))
        {
            return $this->storeDetailByAsset();
        }
       
    }

    public function getValidationList()
    {

        $location_list   = implode(',', EF::getLocationList());

        $validation = $this->input();

        return [

            'designation'  => ['required', new ExistRule('designations','id',false)],

            \EF::branchWhiteLabel()['module']  => ['required', new ExistRule('branches','id')],


            'scale'        => ['required', new ExistRule('scales','id',false)],

            'employee'     =>['required', new ExistRule('users','id'), new ContractVerificationRule($validation,Crypt::encrypt(0))],

            'start_date'   =>'required|date|date_format:Y-m-d',

            'end_date'     =>'nullable|date|date_format:Y-m-d|after:'.$this->input('start_date'),

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

            'status'                    =>'required|in:Active,Deactive',
            
            'contract_form'             => 'nullable|mimes:pdf,png,jpeg,jpg|max:10240',

            // 'location'                  => 'In:'.$location_list

        ];
    }


    public function storeDetailByContract()
    {
      
        return [

             'assign_allowance'                    =>  ['required', new ExistRule('assign_allowances','id')],
             'contract_list.*'                               =>  ['required', new ExistRule('contracts','id')],
             
             
             
          
        ];
    }

     public function storeDetailByAllowance()
    {
      
        return [

             'contract_id'                                   =>  ['required', new ExistRule('contracts','id')],
             'allowance_id.*'                               =>  ['required', new ExistRule('allowances','id')],
             
             
             
          
        ];
    }

        public function storeDetailByAsset()
    {
      
        return [

             'contract'                                 =>  ['required', new ExistRule('contracts','id')],
             'asset_id.*'                               =>  ['required', new ExistRule('assets','id')],
             
             
             
          
        ];
    }
}
