<?php

namespace App\Http\Requests;

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


class VendorPaymentRequest 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('vendorpaymentcommit'))
        {
            $validation_list['vendor'] = [new ExistRule('vendors','id')];

            return $validation_list;
        }
        else if($current_path === url('vendorpaymentupdate'))
        {

            $validation_list['invoice_no']   =  new UpdateUniqueRule('vendor_payments','invoice_no',$this->vendor_payment);

            $validation_list['payment_option']   =  'nullable';
     
            return $validation_list;
        }
       
    }

    public function validationList()
    {
        return [

            'payment_option'   => 'required|in:'.implode(',',EF::getVendorPaymentType()),

            'invoice_no'          => 'required|unique:vendor_payments,invoice_no|min:1|max:50',

            'amount'           => 'required|integer|bail|gt:0',

            'description'      => 'nullable|min:0|max:499',

            'debit_account'         =>  new ExistRule('accounts','id'),
            'credit_account'         =>  new ExistRule('accounts','id'),

        ];
    }
}
