<?php

namespace App\Http\Requests;

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

class AssignAllowanceRequest 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('assignallowancecommit'))
        {
          $validation = $this->getValidationList();

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

             $validation += ['assign_allowance' => new ExistRule('assign_allowance','id')];

            return $validation;
        }

    }

    public function getValidationList()
    {

        $validate = $this->input();
         return [

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

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

            'allowance'     =>  ['required', new ExistRule('allowances','id'), new AllowanceVerificationRule($validate)],

           

        ];
    }
}
