<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Encryption\DecryptException;
use App\Rules\ExistRule;
use App\Rules\UpdateUniqueRule;
use Auth;
use EF;



class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */



    public function authorize()
    {
        return true;
    }


    public function getValidationList()
    {
        $validation = [

            'user_image'        =>  'nullable|mimes:jpeg,png,jpg|max:250',
            'email'             =>  'nullable|email|unique:users|min:0|max:50,',
            'cnic'              =>  'required|max:15|unique:users',
            'full_name'         =>  'required|min:0|max:50',
            'father_name'       =>  'required|min:0|max:50',
            'relation'          =>  'nullable|in:S/O,W/O,D/O',

            'contact'           =>  'nullable|min:0|max:60',
            'other_contact'     =>  'nullable|min:0|max:60',
            'passport'          =>  'nullable|min:0|max:20',
            'password'          =>  'nullable|min:8|max:16|same:confirm_password',
            'confirm_password'  =>  'nullable|min:8|max:16|same:password',
            'nationality'       =>  'nullable|min:0|max:40',
            'occupation'        =>  'nullable|min:0|max:40',
            'designation'       =>  'nullable|min:0|max:100',
            'dob'               =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',
            'temporary_address' =>  'nullable|min:0|max:250',
            'permanent_address' =>  'nullable|min:0|max:250',
            'employee_form'     =>  'nullable|mimes:pdf,png,jpeg,jpg|max:10240',
            // EF::branchWhiteLabel()['module']   =>  ['required', new ExistRule('branches','id')]

        ];

        return $validation;
    }

    public function getValidationByName($associative_validation)
    {
        $validation = $this->getValidationList();

        return $validation[$associative_validation];
    }

    /**
     * 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('customercommit')) {
            $validation = $this->getValidationList();

            $validation['contact'] = 'required|min:0|max:15';

            $validation[EF::branchWhiteLabel()['module']] = 'nullable';


            return $validation;
            // return $this->storeCustomer();
        } else if ($current_path === url('customerupdate')) {
            $validation = $this->getValidationList();

            $validation['cnic'] = ['required', 'max:15', new UpdateUniqueRule('users', 'cnic', $this->user_id)];

            $validation['user_id'] = ['required', new ExistRule('users', 'id')];

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user_id)];

            $validation[EF::branchWhiteLabel()['module']] = 'nullable';



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

            $validation['type'] = 'required|In:Dealer,Sub Dealer,Vendor';

            $validation['filer'] =  'nullable|in:on';

            $validation['ntn_number'] =  'nullable|min:0|max:25';

            $validation['agency_name'] = 'nullable|min:0|max:50';

            $validation['agency_address']    =   'nullable|min:0|max:250';

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

            $validation['user_id'] = ['required', new ExistRule('users', 'id')];

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user_id)];

            $validation['cnic'] = ['nullable', 'max:15', new UpdateUniqueRule('users', 'cnic', $this->user_id)];

            $validation['filer'] =  'nullable|in:on';

            $validation['ntn_number'] =  'nullable|min:0|max:25';

            $validation['agency_name'] = 'nullable|min:0|max:50';

            $validation['agency_address']    =   'nullable|min:0|max:250';

            // $validation['user_type'] = 'required|In:Dealer,Sub Dealer';


            return $validation;
            // return $this->updateDealer();
        } elseif ($current_path === url('employeecommit')) {
            $employee_type_list = implode(',', EF::getEmployeeType());

            $salary_mode_list   = implode(',', EF::getSalaryMode());

            $blood_group_list   = implode(',', EF::getBloodGroupList());

            $gender_list = implode(',', EF::getGenderList());

            $validation = $this->getValidationList();

            $validation['employee_type']    =  'In:' . $employee_type_list;
            $validation['salary_mode']      =  'In:' . $salary_mode_list;
            $validation['blood_group']      =  'nullable|In:' . $blood_group_list;
            $validation['bank_branch']      =  'nullable|min:0|max:50';
            $validation['bank_name']        =  'nullable|min:0|max:50';
            $validation['account_no']       =  'nullable|min:0|max:50';
            $validation['gender']           =  'In:' .  $gender_list;



            return $validation;
        } elseif ($current_path === url('employeeupdate')) {
            $employee_type_list = implode(',', EF::getEmployeeType());

            $salary_mode_list   = implode(',', EF::getSalaryMode());


            $blood_group_list   = implode(',', EF::getBloodGroupList());

            $gender_list = implode(',', EF::getGenderList());

            $validation = $this->getValidationList();

            $validation['user_id'] = ['required', new ExistRule('users', 'id')];

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

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user_id)];

            $validation['employee_type']    = 'In:' . $employee_type_list;
            $validation['salary_mode']      = 'In:' . $salary_mode_list;
            $validation['blood_group']      =  'nullable|In:' . $blood_group_list;
            $validation['gender']           =  'In:' .  $gender_list;
            $validation['bank_branch']      =  'nullable|min:0|max:50';
            $validation['bank_name']        =  'nullable|min:0|max:50';
            $validation['account_no']       =  'nullable|min:0|max:50';

            $validation['status']           =   'in:' . implode(',', EF::getStatusList());

            return $validation;
            // return $this->updateEmployee();
        } elseif ($current_path === url('contractorcommit')) {

            return $this->getValidationList();

            // return $this->storeEmployee();
        } elseif ($current_path === url('contractorupdate')) {

            $validation = $this->getValidationList();

            $validation['user_id'] = ['required', new ExistRule('users', 'id')];

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

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user_id)];

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

            $validation['vendor_type'] = ['required', new ExistRule('vendor_types', 'id')];

            return $validation;

            // return $this->storeEmployee();
        } elseif ($current_path === url('supplierupdate')) {

            $validation = $this->getValidationList();

            $validation['user_id'] = ['required', new ExistRule('users', 'id')];

            $validation['vendor_type'] = ['required', new ExistRule('vendor_types', 'id')];

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

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user_id)];

            return $validation;
            // return $this->updateEmployee();
        } elseif ($current_path === url('nomineecommit')) {

            // dd($this->request);

            $validation = $this->getValidationList();

            $validation['relation_ship'] = 'required|min:0|max:25';

            $validation['nok_type'] = 'required|min:0|max:50';

            $validation['booking_id'] = ['required', new ExistRule('bookings', 'id')];

            $user_id = $this->input('user_id');

            // dd($user_id);

            if (isset($user_id)) {
                // user is set so father name and cnic validations needs to be changed.
                $validation['cnic'] = 'required|exists:users,cnic';

                $validation['full_name']         =  'nullable|min:0|max:50';
                $validation['father_name']       =  'nullable|min:0|max:50';
            }

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

            $validation['relation_ship'] = 'required|min:0|max:25';

            $validation['nok_type'] = 'required|min:0|max:50';

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

            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', $this->user)];
            
            return $validation;

            // return $this->updateNominee();
        } elseif ($current_path === url('profileupdate')) {
            //GET LIST OF ALL VALIDATION
            $validation = $this->getValidationList();

            // MODIFICATION IN VALIDATION LIST EMAIL
            $validation['email'] = ['nullable', 'email', new UpdateUniqueRule('users', 'email', Auth::user()->id)];

            // MODIFICATION IN VALIDATION LIST CNIC
            $validation['cnic']  = 'required|max:15';

            // MODIFICATION IN VALIDATION LIST BRANCH
            $validation[EF::branchWhiteLabel()['module']] = 'nullable';

            // dd($validation);
            return $validation;
            // return $this->updateProfile();
        }
    }

    //  public function storeCustomer()
    //  {

    //      try
    //      {
    //          //  return [

    //          // 'full_name'         =>  'required|min:3|max:50',
    //          // 'father_name'       =>  'required|min:3|max:50',
    //          // 'cnic'              =>  'required|max:15|unique:users',
    //          // 'relation'          =>  'required|in:S/O,W/O,D/O',
    //          // 'nationality'       =>  'nullable|min:0|max:40',
    //          // 'occupation'        =>  'nullable|min:0|max:40',
    //          // 'contact'           =>  'required|min:0|max:60',
    //          // 'dob'               =>  'nullable|date|date_format:Y-m-d',
    //          // 'temporary_address' =>  'nullable|min:0|max:250',
    //          // 'permanent_address' =>  'nullable|min:0|max:250',
    //          // 'branch'            =>  ['required', new ExistRule('branches','id')],
    //          // 'user_image'        =>  'nullable|image:jpeg,png,jpg|max:250',
    //          // 'email'             =>  'nullable|email|unique:users|min:0|max:50',
    //          // 'password'          =>  'nullable|min:8|max:16',
    //          // 'confirm_password'  =>  'nullable|min:8|max:16|same:password',

    //          // ];

    //        dd($this->getValidationList());

    //        return $this->getValidationList();

    //      }
    //      catch (DecryptException $e)
    //      {

    //      }

    //  }


    //  public function updateCustomer()
    //  {

    //      try
    //      {
    //           return [

    //          'full_name'         =>  'required|min:3|max:50',
    //          'father_name'       =>  'required|min:3|max:50',
    //          'cnic'              =>  ['required','max:15', new UpdateUniqueRule('users','cnic',$this->user_id)],
    //          'relation'          =>  'required|in:S/O,W/O,D/O',
    //          'nationality'       =>  'nullable|min:0|max:40',
    //          'occupation'        =>  'nullable|min:0|max:40',
    //          'contact'           =>  'required|min:0|max:60',
    //          'dob'               =>  'required|date|date_format:Y-m-d',
    //          'temporary_address' =>  'nullable||min:0|max:250',
    //          'permanent_address' =>  'nullable||min:0|max:250',
    //          'branch'            =>  ['required', new ExistRule('branches','id')],
    //          'user_id'           =>  ['required', new ExistRule('users','id')],
    //          'user_image'        =>  'nullable|image:jpeg,png,jpg|max:250',
    //          'email'             =>  ['nullable','email', new UpdateUniqueRule('users','email',$this->user_id)],
    //          'password'          =>  'nullable|min:8|max:16',
    //          'confirm_password'  =>  'nullable|min:8|max:16|same:password',

    //          ];

    //      }
    //      catch (DecryptException $e)
    //      {

    //      }

    //  }

    //  public function storeDealer()
    // {
    //     try
    //     {



    //          return [

    //         'full_name'         =>  'required|min:3|max:50',
    //         'father_name'       =>  'required|min:3|max:50',
    //         'cnic'              =>  'required|max:15|unique:users',
    //         'contact'           =>  'required|min:0|max:60',
    //         'branch'            =>  ['required', new ExistRule('branches','id')],
    //         'email'             =>  'nullable|email|unique:users|min:0|max:50',

    //     ];

    //     }
    //     catch (DecryptException $e)
    //     {

    //     }

    // }

    // public function updateDealer()
    // {
    //     try
    //     {



    //          return [

    //         'full_name'         =>  'required|min:3|max:50',
    //         'user_id'           =>  ['required', new ExistRule('users','id')],
    //         'father_name'       =>  'required|min:3|max:50',
    //         'contact'           =>  'nullable|min:0|max:60',
    //         'branch'            =>  ['required', new ExistRule('branches','id')],
    //         'email'             =>  ['nullable','email', new UpdateUniqueRule('users','email',$this->user_id)],

    //     ];

    //     }
    //     catch (DecryptException $e)
    //     {

    //     }

    // }

    // public function storeEmployee(){
    //  try
    //      {
    //           return [

    //          'full_name'         =>  'required|min:3|max:50',
    //          'father_name'       =>  'required|min:3|max:50',
    //          'cnic'              =>  'required|max:15|unique:users',
    //          'contact'           =>  'required|min:0|max:60',
    //          'designation'       =>  'nullable|min:0|max:60',
    //          'branch'            =>  ['required', new ExistRule('branches','id')],
    //          'email'             =>  'nullable|email|unique:users|min:0|max:50',
    //          'password'          =>  'nullable|min:8|max:16',
    //          'confirm_password'  =>  'nullable|min:8|max:16|same:password',

    //          ];

    //      }
    //      catch (\Exception $e)
    //      {

    //      }
    // }

    //   public function updateEmployee(){

    //  try
    //      {
    //           return [

    //         'full_name'         =>  'required|min:0|max:50',
    //         'user_id'           =>  ['required', new ExistRule('users','id')],
    //         'father_name'       =>  'required|min:0|max:50',
    //         'contact'           =>  'required|min:0|max:60',
    //         'branch'            =>  ['required', new ExistRule('branches','id')],
    //         'email'             =>  ['nullable','email', new UpdateUniqueRule('users','email',$this->user_id)],
    //         'designation'       =>  'required|min:0|max:100',


    //          ];

    //      }
    //      catch (\Exception $e)
    //      {

    //      }
    // }

    //  public function updateProfile(){


    //  try
    //      {
    //        $validation=$this->getValidationList();

    //        $validation['email'] = ['nullable','email', new UpdateUniqueRule('users','email',Auth::user()->id)];

    //        $validation['cnic']  = 'required|max:15';

    //        $validation['branch'] = 'nullable';

    //        return $validation;

    //      }
    //      catch (\Exception $e)
    //      {

    //      }
    // }

    //  public function storeNominee()
    //  {
    //    // dd($this->input());

    //      try
    //      {

    //           return [

    //          'full_name'         =>  'required|min:3|max:50',
    //          'father_name'       =>  'required|min:3|max:50',
    //          'cnic'              =>  'required|max:15|unique:users',
    //          'relation'          =>  'required|in:S/O,W/O,D/O',
    //          'nationality'       =>  'nullable|min:0|max:40',
    //          'occupation'        =>  'nullable|min:0|max:40',
    //          'contact'           =>  'required|min:0|max:60',
    //          'dob'               =>  'nullable|date|date_format:Y-m-d',
    //          'temporary_address' =>  'nullable|min:0|max:255',
    //          'permanent_address' =>  'nullable|min:0|max:255',
    //          'user_image'        =>  'nullable|image:jpeg,png,jpg|max:250',
    //          'email'             =>  'nullable|email|unique:users|min:0|max:50',
    //          'relation_ship'     =>  'required|min:0|max:25',
    //          'nok_type'          =>   'required|min:0|max:50',
    //          'booking_id'        =>   ['required', new ExistRule('bookings','id')],
    //          'branch'            =>   ['required', new ExistRule('branches','id')],


    //          ];


    //      }
    //      catch (DecryptException $e)
    //      {

    //      }
    //    }



    //        public function updateNominee()
    //  {


    //      try
    //      {

    //           return [

    //          'full_name'         =>  'required|min:3|max:50',
    //          'father_name'       =>  'required|min:3|max:50',
    //          'cnic'              =>  'required|max:15',
    //          'relation'          =>  'required|in:S/O,W/O,D/O',
    //          'nationality'       =>  'nullable|min:0|max:40',
    //          'occupation'        =>  'nullable|min:0|max:40',
    //          'contact'           =>  'required|min:0|max:20',
    //          'dob'               =>  'nullable|date|date_format:Y-m-d',
    //          'temporary_address' =>  'nullable|min:0|max:255',
    //          'permanent_address' =>  'nullable|min:0|max:255',

    //          'email'             =>  ['nullable','email', new UpdateUniqueRule('users','email',$this->user_id)],
    //          'relation_ship'     =>  'required|min:0|max:50',
    //          'nok_type'          =>   'required|min:0|max:50',


    //          ];


    //      }
    //      catch (DecryptException $e)
    //      {

    //      }


    //  }

}
