<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Installment;
use App\Booking;

class AmountVerificationRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private $parms;
    private $message; 

    public function __construct($data)
    {
          $this->parms = $data;
          $this->setMessage("The Amount is Invalid");
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $credit = $this->AmountVerification();

       try 
       {
            $booking_id   = $this->parms['booking'][0];

            $booking_id   = decrypt($booking_id);

             if($booking_id > 0 && $this->parms['amount'][0] > $credit)
             {
                     $this->setMessage("The Amount is Invalid");
                     return false;
             }
             else
             {
                return true;
             }

        } 
        catch (DecryptException $e) 
        {

        }

            
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */

    // SET ERROR MESSAGE
    public function setMessage($msg)
    {
        $this->message = $msg;
    }

    // RETURN MESSAGE
    public function message()
    {
        return $this->message;
    }
    public function AmountVerification()
    {

       try 
       {
        $booking_id   = $this->parms['booking'][0];

        $booking_id   = decrypt($booking_id);

        $booking      = new Booking;

        $balance      = $booking->getBalanceDetail($booking_id);

      return $balance['credit'];

    } 
    catch (DecryptException $e) 
    {

    }
    
    }

}
