<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Schema;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;
use EF;

class DateRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
            private $field_name;
            private $row_id;
            private $table_name;
            private $message;
            private $flag;

    public function __construct($table,$field,$id,$required = true)
    {

        $this->table_name = $table;
        $this->field_name = $field;
        $this->row_id     = $id;
        $this->flag       = $required;
        $this->message    = 'Invalid date.';

       
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        
            $value = date('Y-m-d',strtotime($value));


            if($value > 0)
            {
                if($this->verification())
                {
                    $date = $this->getQuery();

                    $date = str_replace('1970-01-01',NULL, $date);

                    if($date != NULL && $value <= $date) 
                    {
                        
                        return true;
                    }
                    elseif($date == NULL)
                    {
                        return true;
                    }
                    else
                    {
                        $this->setMessage('Invalid date');
                        
                    }
                }
            }
            else
            {
                // TRUE CASE REQUIRED FIELD
                // if($this->getFlag())
                // {
                //     $this->setMessage('The '.$attribute.' does not exist.');
                // }
                // else
                // {
                //     return true;
                // }                
            }

        return false;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
 
        // RETURN TABLE NAME
    public function getTableName()
    {
        return $this->table_name;
    }


    public function getQuery()
    {
        $query = DB::table($this->getTableName())->where(['id' => $this->getRowId()])->first();

        if(isset($query))
        {
            $query = json_encode($query);
            $query = json_decode($query,true);

            return date('Y-m-d',strtotime($query[$this->getFieldName()]));
        }
        else
        {
            return null;
        }
       
    }

    public function verification()
    {
        if(Schema::hasTable($this->getTableName()) && Schema::hasColumn($this->getTableName(),$this->getFieldName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

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

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

    // RETURN FIELD NAME
    public function getFieldName()
    {
        return $this->field_name;
    }

    public function getRowId()
    {
        try 
        {
            return decrypt($this->row_id);
        } 
        catch (DecryptException $e) 
        {
            return 0;
        }
        
    }

     // RETURN FIELD NAME
    public function getFlag()
    {
        return $this->flag;
    }

}
