<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

use App\Commission;
use Crypt;



class CommissionExistRule 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)
    {
        $user = $this->checkCommission();

        if($user)
        {
          $this->setMessage("You have already added commission for this user");
                     return false;
        }
        else
        {
            return true;
        }
        
    }

    /**
     * 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 checkCommission()
    {
       
         try 
        {
            $user_id = $this->parms['user_id'];
            $booking_id = $this->parms['booking_id'];

            //DECRYPTING ENCRYPTED IDS
            $user_id    = decrypt($user_id);
            $booking_id = decrypt($booking_id);
           $commission  = new Commission;
           return $commission->getCommissionUser($user_id,$booking_id);
           
        } 
        catch (DecryptException $e) 
        {
            
        }


    }
}
