<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;
use EF;
use App\Deduction;

class LoanDateRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */

     private $parms;

    public function __construct($parms)
    {
          $this->parms = $parms;

          $this->message = "No two installments can be in same month";
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {




    if(@count($this->parms['installment_id']) == @count($this->parms['installment_date']))
       {

        $this->parms['dummy_installment_date'] = $this->parms['installment_date'];

       for($i=0; $i< @count($this->parms['installment_date']); $i++)
       {
        if($this->parms['dummy_installment_date'][$i] == null)
        {
            unset($this->parms['dummy_installment_date'][$i]);

        }
        else
        {
              $this->parms['dummy_installment_date'][$i] = date('m-Y',strtotime($this->parms['dummy_installment_date'][$i]));
        }

       }

       // $previous_array = $this->getLoanPreviousInstallments(decrypt($this->parms['loan_id']));


     if(@count($this->parms['dummy_installment_date']) === @count(array_unique($this->parms['dummy_installment_date'])))
     {

        return true;

     }
     else
     {
        $this->setMessage('No two installments can be in same month and year');
         return false;
     }
       }
       else
       {
        return false;
       }
      
         return true;
      
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    
 public function setMessage($msg)
    {
        $this->message = $msg;
    }

    public function message()
    {
        return $this->message;
    }

     public function getMessage()
    {
        return $this->message;
    }

    // public function getLoanPreviousInstallments($loan_id)
    // {
    //     $deduction = new Deduction;

    // $curr_deductions = $deduction->getDeductionsByLoan($loan_id);

   
   

    // $previous_array = [];

    // for($i = 0; $i < @count($curr_deductions); $i++)
    // { 

    //     if($curr_deductions[$i]['created_at'] == null)
    //     {
            

    //     }
    //     else
    //     {
    //           $previous_array[$i] = date('m-Y',strtotime($curr_deductions[$i]['created_at']));
    //     }

        

    // }

    // dd($previous_array);


    // }



    

}
