<?php

namespace App\Http\Requests;

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

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

            $validation += ['user_id'   =>   ['required', new ExistRule('users','id')]];
            $validation += ['minute_id' =>   ['required', new ExistRule('minute_sheets','id')]];
            $validation += ['deduction' =>   ['nullable', new ExistRule('deductions','id',false)]];

            return $validation;
        }

        elseif($current_path === url('payrolldetailupdate'))
        {
             return $this->updateValidationList();
        }

       
   

    }

    public function getValidationList()
    {

        return [


            'title'           =>'required|min:1|max:50',

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


    public function updateValidationList()
    {

       $validate = $this->input();


          return [

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

            'minute_id'           =>   ['required', new ExistRule('minute_sheets','id')],

            'allowances.*'        =>   ['required', new ExistRule('contract_details','id')],

            'deductions.*'        =>   ['required', new ExistRule('deductions','id')],

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

            'allowance_amount.*'  =>   'nullable|Integer|bail|gte:0',

            'deduction_amount.*'  =>   'nullable|Integer|bail|gte:0',

            'gross_salary'        =>   ['required', new PayrollRule($validate)],

          ];

    }

}
