<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ExistRule;
use App\Rules\UpdateUniqueRule;
use EF;
class BranchRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */

    public function __construct()
    {
        $this->setWhiteLabel(EF::branchWhiteLabel());
    }

    public function setWhiteLabel($list = [])
    {
        $this->white_labels = $list;
    }

    public function getWhiteLabel()
    {
        return $this->white_labels;
    }

    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // parent::errorMessage('Required form validation.');
        
        $current_path = url()->current();

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

            return $validation;
            // return $this->storeBranch();
        }
        elseif($current_path === url('branchupdate'))
        {
            $labels = $this->getWhiteLabel();

            $validation = $this->getValidationList();


            $validation[$labels['code']]  = ['required','min:3','max:50', new UpdateUniqueRule('branches','code',$this->branch)];

            $validation[$labels['branch']] = ['required', new ExistRule('branches','id')];

            // $validation[$labels['email']]  = ['required','email', new UpdateUniqueRule('branches','email',$this->input($labels['branch']))];
            return $validation;

           // return $this->updateBranch();
        }
    
    }


    
    public function getValidationList()
    {
        $labels = $this->getWhiteLabel();

        $city_list = implode(',',EF::getCityList());
        
        $validation = [

          $labels['code']          => 'required|unique:branches,code|min:0|max:50',

          $labels['name']          => 'required|min:3|max:50',

          $labels['city']          => 'required',


          // $labels['email']         => 'nullable|email|unique:branches,email|min:0|max:50',

          // $labels['contact']       => 'required|min:0|max:25',

          // $labels['commission']    => 'required|Integer|between:0,100',

          $labels['branch']          => 'nullable',

          $labels['location']        => 'required|min:1|max:500',

          $labels['start_date']          =>  'required|date|date_format:Y-m-d|after:1970-01-01',

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



          
        ];



        return $validation;
    }
}
