<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\ExistRule;
use App\Rules\SimpleUniqueRule;
use App\Rules\UpdateUniqueRule;
use App\Rules\FileNumberRule;




use App\Rules\AgreedAmountRule;
use App\Rules\PlotVerificationRule;
use App\Rules\DateRule;
use EF;



class BookingRequest 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('bookingcommit'))
        {
           return $this->storeBooking();
        }

        elseif($current_path === url('bookingupdate'))
        {
            return $this->updateBooking();
        }
        elseif($current_path === url('bookingcancel'))
        {
            return $this->cancelBooking();
        }
         elseif($current_path === url('approvecancelbooking'))
        {
            return $this->approveCancelbooking();
        }
    }

    public function storeBooking()
    {
        $validate = $this->input();
        $mode_list = implode(',', EF::getPaymentMode());
        $label_list = EF::getWhiteLabel(); 
      
   
        return [
             $label_list['branch']                             =>  ['required', new ExistRule('branches','id'),'bail'],
             'user_id'                            =>  ['required', new ExistRule('users','id')],
              $label_list['plot']                 =>  ['required', new PlotVerificationRule('plots','id',0,$validate)],
             'customer_cnic'                      =>  'required|min:0|max:25',
             'customer_image'                     =>  'nullable|image:jpeg,png,jpg|size:300',
            $label_list['file']                  =>  ['required', new FileNumberRule('bookings', 'file_number',['status','']), 'min:0', 'max:50'],
            
             $label_list['agreed_amount']         =>  ['integer','bail',new AgreedAmountRule($validate)],
             $label_list['reference']             =>  'nullable|min:0|max:50',
             $label_list['discount']              =>  'nullable|integer',
             $label_list['feature_charges']       =>  'nullable|integer',
             // $label_list['rate_per_marla']        =>  'required|integer',
             // $label_list['token_amount']          =>  'nullable|integer',
             // $label_list['receipt']               =>  'nullable|min:0|max:50',
             // $label_list['payment_mode']          =>  'In:'.$mode_list ,
             $label_list['start_date']          =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',
             $label_list['end_date']          =>  'nullable|date|date_format:Y-m-d|after:1970-01-01',
             $label_list['booking_date']          =>  '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|after:1970-01-01',
             $label_list['remarks']               =>  'nullable|max:500',
          
        ];
    }


    public function updateBooking()
    {
        $validate = $this->input();
        $mode_list = implode(',', EF::getPaymentMode());
        $label_list = EF::getWhiteLabel();      


        return [
             $label_list['branch']                          =>  ['required', new ExistRule('branches','id')],
             'booking_id'                      =>  ['required', new ExistRule('bookings','id')],
              $label_list['plot']              =>  ['required', new PlotVerificationRule('plots','id',$validate['booking_id'],$validate)],
              $label_list['booking_date']      =>  ['required', new DateRule('bookings','approve_date',$validate['booking_id'])],
              $label_list['file']              =>  'nullable|min:0|max:50',
              $label_list['agreed_amount']     =>   new AgreedAmountRule($validate),
              $label_list['reference']         =>  'nullable|min:0|max:50',
            $label_list['file']                =>  ['required', new UpdateUniqueRule('bookings', 'file_number', $this->booking_id), 'min:0', 'max:50'],

              $label_list['discount']          =>  'nullable|integer',
              $label_list['feature_charges']   =>  'nullable|integer',
               // $label_list['rate_per_marla']    =>  'required|integer',
             //  $label_list['token_amount']      =>  'nullable|integer',
             //  $label_list['receipt']           =>  'nullable',
             //  $label_list['payment_mode']      =>  'In:'.$mode_list ,
             // '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',
              $label_list['remarks']           =>  'nullable|max:500',
          
        ];
    }

    public function cancelBooking()
    {
          $validate = $this->input();
         $label_list = EF::getCancelFormWhiteLabel();    

         return
         [

            'booking_id'                       =>  ['required', new ExistRule('bookings','id')],
             $label_list['deduction_amount']   =>  'nullable|integer',
             $label_list['refund_amount']      =>  'nullable|integer',
              'cancel_date'                     =>  'required|date|date_format:Y-m-d|after:1970-01-01',

         ];

   

    }
        public function approveCancelBooking()
    {
          $validate = $this->input();
         $label_list = EF::getCancelFormWhiteLabel();    

         return
         [

            'booking_id'                       =>  ['required', new ExistRule('bookings','id')],
             $label_list['deduction_amount']   =>  'nullable|integer',
             $label_list['refund_amount']      =>  'nullable|integer',
             'cancel_date'                     =>  'required|date|date_format:Y-m-d|after:1970-01-01',

         ];

   

    }



}