<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use EF;
use App\Rules\InstallmentRule;
use App\Rules\CommissionRule;
use App\Rules\ExistRule;




class PaymentRequest 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()
    {
        EF::errorMessage('Required form validation.');
        
        $current_path = url()->current();

        if($current_path === url('paymentcommit'))
        {
            return $this->getValidationList();
        }
        else if($current_path === url('updatepayment'))
        {
            $validation = $this->getValidationList();

            $validation['payment'] = ['required', new ExistRule('payments','id')];
           
            $validation['installment']  = 'nullable';

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

             $validation['booking']    = 'nullable';
             $validation['user']       = 'nullable';

            return $validation;
        }
    }

    public function getValidationList()
    {
    
        $installment[0] = $this->installment;
        $commission[0] = $this->commission;

        $booking[0] = $this->booking;

        $mode_list = implode(',', EF::getPaymentMode());
        
        return [
            
            'booking'     =>  ['required', new ExistRule('bookings','id')],
            'user'        =>  ['required', new ExistRule('users','id')],
            'installment' =>  ['required', new InstallmentRule($booking,$installment)],
            'commission'  =>  ['required', new CommissionRule($booking,$commission)],
            'mode'        =>  'required|In:'.$mode_list,
            'amount'    =>  'required|integer|bail|gt:0',
            'date'        =>  'required|date|date_format:Y-m-d|after:1970-01-01',
            'receipt'     =>  'nullable',

        ];
    }
}
