<?php

namespace Gisplus\Package\EstatechVendor\Requests;

use Illuminate\Foundation\Http\FormRequest;

use App\Rules\ExistRule;

use Illuminate\Validation\Rule;


class InvoiceRequest 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()
    {
        // parent::errorMessage('Required form validation.');

        $current_path = url()->current();

       if($current_path === url('invoiceupdate'))
        {

            return  $this->getValidationList([
                // 'invoice_no' =>  [
                //         'required','min:1','max:250',Rule::unique('invoices')->where(function ($query) {
                //         $query->where(['invoice_no' => $this->invoice_no, 'branch_id' => decrypt($this->branch),'is_recycle' => 0]);
                //     })->ignore(decrypt($this->invoice)),
                //         ],
            ]);
          
        }
        elseif($current_path === url('invoicecommit'))
        {
            return  $this->getValidationList();

        }
    }


    public function getValidationList($new_validation = [])
    {
     
        $validation = $this->input();

        return $new_validation + [
            
            // 'invoice_no'          =>  ['required',Rule::unique('invoices')->where(function ($query) {
            //             $query->where(['invoice_no' => $this->invoice_no, 'branch_id' => decrypt($this->branch),'is_recycle' => 0]);
            //         })],
            'branch'              =>  ['required', new ExistRule('branches','id')],
            'user'                =>  ['required', new ExistRule('users','id')],
            'due_date'            =>   'required|date|date_format:Y-m-d|after:1970-01-01',  
            'date'                =>   'required|date|date_format:Y-m-d|after:1970-01-01',  

            'item_code.*'         =>   'required|min:0|max:50',

            'description.*'       =>   'required|min:0|max:100',
            'unit_price.*'        =>   'required|integer|gt:0',
            'discount.*'          =>   'nullable|integer|gte:0',
            'quantity.*'          =>   'required|integer|gt:0',
            'gst_percentage.*'    =>   'nullable|integer|gte:0',



        ];
    }
}
