<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ExistRule;
use App\Rules\UpdateUniqueRule;



class VendorRequest 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()
    {
        $current_path = url()->current();

        $validation_list = $this->validationList();

        if($current_path === url('vendorandpaymentcommit'))
        {
            return $validation_list;
        }
        else if($current_path === url('vendorandpaymentupdate'))
        {
           $validation_list['vendor'] = [new ExistRule('vendors','id')];

           $validation_list['code']   =  new UpdateUniqueRule('vendors','code',$this->vendor);

            
            return $validation_list;
        }
       
    }

    public function validationList()
    {
        return [

            'code'             => 'required|unique:vendors,code|min:3|max:50',

            'vendor_name'      => 'required|min:3|max:50',

            'ntn_number'       => 'nullable|unique:vendors,code|min:3|max:50',

            'person_name'      => 'required|min:3|max:50',

            'cnic'             => 'required|max:15',

            'contact'          => 'required|min:0|max:15',

            'vendor'           => [new ExistRule('vendors','id',false)],

            'photo'            =>  'nullable|mimes:pdf,png,jpeg,jpg|max:10240',

        ];
    }
}
