<?php

namespace App\Http\Requests;

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


use EF;

class VoucherRequest 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('approvemultiplevouchers')) {
            $voucher_status  = implode(',', EF::getVoucherStatus());

            return [

                'status.*'              =>  'required|in:' . $voucher_status,
                'date.*'                =>  'required|date|date_format:Y-m-d|after:1970-01-01',
                'transaction.*'         =>  'required',

            ];
        }
        else if ($current_path === url('voucherstatus')) {
            $voucher_status  = implode(',', EF::getVoucherStatus());

            return [

                'status'           =>  'required|in:' . $voucher_status,
                'date'             =>  'required|date|date_format:Y-m-d|after:1970-01-01',
                'journal_id'       =>  'required',
                'transaction_id'   =>  'required',
                'account.*'        =>  ['required', new ExistRule('accounts', 'id')],

            ];
        }
        // else if($current_path === url('vouchercommit') || $current_path === url('payinstallment')  || $current_path === url('paycommission'))
        elseif ($current_path === url('vouchercommit') || $current_path === url('paypayment')) {
            return $this->getValidationList();
            // return $this->storeVoucher();
        } else if ($current_path === url('voucherupdate')) {

            $validation = $this->getValidationList();

            $validation += [
                'transaction'        =>  ['required', new ExistRule('transactions', 'id')],

                'journal_list.*'     =>  ['required', new ExistRule('journals', 'id')],
            ];

            return $validation;
        }
        else
        {
            return [];
        }
        // else if($current_path === url('payinstallment'))
        // {
        //     return $this->payInstallment();
        // }
        // else if($current_path === url('paycommission'))
        // {
        //     return $this->payCommission();
        // }


    }

    public function getValidationList()
    {
        $validate = $this->input();

        $voucher_type  = implode(',', EF::getVoucherType());
        $payment_mode  = implode(',', EF::getPaymentMode());
        $voucher_list  = implode(',', EF::getVoucherList());
        $payment_type  = implode(',', EF::getPaymentType());

        return [
            'compound_type'    =>  'required|in:' . $voucher_type,
            'voucher_list'     =>  'required|in:' . $voucher_list,
            'date'             =>  'required|date|date_format:Y-m-d|after:1970-01-01',
            'amount.*'         =>  'required|integer|bail|gt:0',
            'narration.*'      =>  'required',
            'payment_mode.*'   =>  'required|in:' . $payment_mode,


            'debit_subhead.*'  =>  ['required', new ExistRule('accounts', 'id')],
            'credit_subhead.*' =>  ['required', new ExistRule('accounts', 'id'), new AccountVerificationRule($validate)],

            'user.*'          =>  [new ExistRule('users', 'id', false)],

             'booking.*'       =>  [new ExistRule('bookings', 'id', false)],

            EF::branchWhiteLabel()['module'] =>  ['required', new ExistRule('branches', 'id')],

            // PAYMENT TYPE ACTUAL PAYMENT ID
            // 'payment_type.*' =>  new PaymentVerficationRule($validate),
            'payment_type.*' =>  [new ExistRule('payments', 'id', false)],






        ];

        return $validation;
    }

    //  public function storeVoucher(){

    //  try
    //      {
    //        $validate = $this->input();

    //        $voucher_type  = implode(',',EF::getVoucherType());
    //        $payment_mode  = implode(',',EF::getPaymentMode());
    //        $voucher_list  = implode(',',EF::getVoucherList());
    //        $payment_type  = implode(',',EF::getPaymentType());

    //           return [

    //         'compound_type'    =>  'required|in:'.$voucher_type,
    //         'voucher_list'     =>  'required|in:'.$voucher_list,
    //         'payment_type.*'   =>  'required|in:0,'.$payment_type,
    //         'date'             =>  'required|date|date_format:Y-m-d|after:1970-01-01',
    //         'amount.*'           =>  'required|numeric|gt:0',

    //         // 'amount.*'         =>  ['required',new AmountVerificationRule($validate)],
    //         // 'amount'           => 'required',
    //         'narration.*'      =>  'required',
    //         'payment_mode.*'   =>  'required|in:'.$payment_mode,
    //         EF::branchWhiteLabel()['module']           =>  ['required', new ExistRule('branches','id')],
    //         'debit_subhead.*'  =>  ['required', new ExistRule('accounts','id')],
    //         'credit_subhead.*' =>  ['required', new ExistRule('accounts','id'), new AccountVerificationRule($validate)],
    //          'user.*'          =>  [new ExistRule('users','id',false)],
    //         'booking.*'        =>  [new ExistRule('bookings','id',false)],

    //         'installment'      =>  [new InstallmentRule($this->booking,$this->installment)],
    //         'commission'       =>  [new CommissionRule($this->booking,$this->commission)],
    //          ];

    //      }
    //      catch (\Exception $e)
    //      {

    //      }
    // // }
    //  public function updateVoucherStatus(){


    //  try
    //      {





    //      }
    //      catch (\Exception $e)
    //      {

    //      }
    // }

    // public function payInstallment()
    // {
    //    return [
    //  'compound_type'                =>  'required|in:General',
    //  'payment_type'                 =>  'required|in:Installment',
    //  'drawn_on'                     =>  'nullable|min:0|max:50',
    //  'instrument'                   =>  'nullable|min:0|max:50',
    //  // 'instrument_date'              =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',

    //    ];

    // }
    // public function payCommission()
    // {

    //    return [
    //     'compound_type'             =>  'required|in:General',
    //     'payment_type'              =>  'required|in:Commission,Refund,Deduction,Transfer,Revenue',
    //     'drawn_on'                  =>  'nullable|min:0|max:50',
    //     'instrument'                =>  'nullable|min:0|max:50',
    //     // 'instrument_date'           =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',

    //    ];
    // }

    // public function updateVoucher()
    // {

    //    $voucher_type  = implode(',',EF::getVoucherType());
    //    $payment_mode  = implode(',',EF::getPaymentMode());
    //    $voucher_list  = implode(',',EF::getVoucherList());
    //    $payment_type  = implode(',',EF::getPaymentType());
    //        $validate = $this->input();


    //    $data = [

    //         'compound_type'      =>  'required|in:'.$voucher_type,
    //          'debit_subhead.*'    =>  ['required', new ExistRule('accounts','id')],
    //         'credit_subhead.*' =>  ['required', new ExistRule('accounts','id'), new AccountVerificationRule($validate)],
    //         'voucher_list'       =>  'required|in:'.$voucher_list,
    //         'mode.*'             =>  'required|in:'.$payment_mode,
    //         'payment_type.*'     =>  'required|in:0,'.$payment_type,
    //         'date'               =>  'required|date|date_format:Y-m-d|after:1970-01-01',
    //         'drawn_on'           =>  'nullable|min:0|max:50',
    //         'instrument'         =>  'nullable|min:0|max:50',
    //         // 'instrument_date'    =>  'nullable|date|date_format:Y-m-d',
    //         'amount.*'           =>  'required|numeric|gt:0',
    //         'narration.*'        =>  'required',

    //         'user.*'             =>  [new ExistRule('users','id',false)],
    //         'booking.*'          =>  [new ExistRule('bookings','id',false)],

    //         'installment'        =>  [new InstallmentRule($this->booking,$this->installment)],

    //         'commission'         =>  [new CommissionRule($this->booking,$this->commission)],

    //         EF::branchWhiteLabel()['module']             =>  ['required', new ExistRule('branches','id')],
    //         'transaction'        =>  ['required', new ExistRule('transactions','id')],
    //         'journal_list.*'     =>  ['required', new ExistRule('journals','id')],


    //          ];


    //          return $data;



    // }


}
