<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use App\Rules\InstallmentVerificationRule;
use App\Rules\OnetimeVerificationRule;
use App\Rules\UpdateInstallmentRule;
use App\Rules\ExistRule;
use App\Booking;
use App\Installment;
use EF;
use Crypt;


class InstallmentRequest 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('installmentcommit'))
        {
           return $this->storeInstallment();
        }
        elseif($current_path === url('onetimecommit'))
        {
           return $this->storeOnetime();
        }
        elseif($current_path === url('installmentupdate'))
        {
           return $this->updateInstallment();
        }
    }

    public function storeOnetime()
    {
        $validate = $this->input();
        $type_list = implode(',', EF::getPlanType());
       

        $id  = decrypt($validate['booking_id']);


        $booking = Booking::where('id',$id)->first();
      
        return [

           'description.*'  => 'required|min:0|max:25',
           'booking_id'     =>  ['required', new ExistRule('bookings','id')],

           // 'date.*'         => 'required|date|before_or_equal:'.date('Y-m-d',strtotime($booking->branch->end_date)).'|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),

           'date.*'         => 'required|date|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),


           'percentage.*'   => [new OnetimeVerificationRule($validate)],

           'payment_type.*' =>  'In:'.$type_list ,

           'plan_type'      => 'In:One Time',


          
                   
             ];
    }

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

       $type_list = implode(',', EF::getPlanType());

       $id  = decrypt($validate['booking_id']);


       $booking = Booking::where('id',$id)->first();



        return [

           'description.*'  => 'required|min:0|max:25',

            // 'date.*'         => 'required|date|before_or_equal:'.date('Y-m-d',strtotime($booking->branch->end_date)).'|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),
           'date.*'         => 'required|date|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),



           'payment_type.*' => 'In:'.$type_list ,

           'plan_type'      => 'In:Installment',

           'amount.*'       => ['required','gte:0','integer',new InstallmentVerificationRule($validate)] ,  
                   
            ];
    }

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

             $id  = decrypt($validate['id']);


             $installment = Installment::find($id);

             $booking = $installment->booking;


         
        return [

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

            // 'due_date'         => 'required|date|before_or_equal:'.date('Y-m-d',strtotime($booking->branch->end_date)).'|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),
           'due_date'         => 'required|date|after_or_equal:'.date('Y-m-d',strtotime($booking->approve_date)),



           'amount'       => ['required','integer',new UpdateInstallmentRule($validate)] ,  
                   
            ];
    }
}
