<?php

namespace Gisplus\Package\Crm\Requests;

use App\Rules\ExistRule;
use EF;
use App\Rules\UpdateUniqueRule;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;

class LeadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $current_path = url()->current();
        if ($current_path === url('leadcommit')) {

          
            $validation = $this->getValidationList();

            $is_buisness_referral = $this->input('is_business_referral');
           
            if($is_buisness_referral == "1")
            {
          
                $validation['referral_name'] = ['required'];
                $validation['referral_cnic'] = ['required'];
                $validation['type'] = ['required'];

            }
            else{

                $validation['referral_name'] = ['nullable'];
                $validation['referral_cnic'] = ['nullable'];

            }
            


            return $validation;
        }
        elseif (Request::is('updatelead')) {

            $validation = $this->getValidationList();


            $validation['lead'] = ['required', new ExistRule('leads', 'id')];

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

            $validation['referral_name'] = ['nullable'];
            $validation['referral_cnic'] = ['nullabe'];

            return $validation;
        }

        elseif($current_path === url('/leadtrash/{id}')){
 dd($current_path);
            $validation = $this->getValidationList();
            $validation['lead'] = ['required', new ExistRule('leads', 'id')];

        }
      
    }
    public function getValidationList()
    {
        $status_list = implode(',', EF::getLeadStatus());
        $validation = [

            'name'              =>  'required|min:0|max:50',
            'email'             =>  'nullable|min:0|max:50,',
            'type'              =>  'required',
            'contact'           =>  'nullable|min:0|max:60',
            'date'              =>  'required|date|date_format:Y-m-d',
            'title'             =>  'nullable|min:0|max:100',
            'description'       =>  'nullable|min:0|max:255',
            'added_by'          =>  'nullable',
            'status'            =>  'nullable|In:'.$status_list,
            'referral_email'    =>  'nullable|min:0|max:60',
            'referral_contact'  =>  'nullable|min:0|max:60',
            'company_name'      =>  'nullable|min:0|max:100',

        ];

        return $validation;
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
 
}
