<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\LeaveVerificationRule;

use App\Rules\ExistRule;
use Crypt;
use EF;

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

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

            return $validation;
        }
         elseif($current_path === url('leaveupdate'))
        {
            
            $form_collect = $this->input();

            $validation = $this->getValidationList();

            $validation += ['leave' => ['required', new ExistRule('leaves','id'), new LeaveVerificationRule($form_collect,$this->input('leave'))]];



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



            return $validation;
           
        }
    }

    public function getValidationList()
    {

        $leave_type_list = EF::getLeavesTypeList();

        $leave_type_list = implode(',',$leave_type_list);

        $validation = $this->input();


        return [

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

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

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

            'no_of_hours' =>'nullable|integer|bail|gt:0|between:1,5',

            'status'     =>'required|in:Approved,Pending',

            'type'       =>'required|in:'.$leave_type_list,


        ];
    }
}
