<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Logs Model
 *
 * @property \App\Model\Table\MembersTable&\Cake\ORM\Association\BelongsTo $Members
 * @property \App\Model\Table\ClientsTable&\Cake\ORM\Association\BelongsTo $Clients
 *
 * @method \App\Model\Entity\Log get($primaryKey, $options = [])
 * @method \App\Model\Entity\Log newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Log[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Log|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Log saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Log patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Log[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Log findOrCreate($search, callable $callback = null, $options = [])
 */
class LogsTable extends AppTable
{
    
    public $ACCION_ARRAY = [
        '1'=>'LetterStream',
        '2'=>'Ringcentral',
        '3'=>'Dropbox',
        '4'=>'Google Drive',
        '5'=>'One Drive',
        '6'=>'Email',
        '7'=>'Zapier'
    ];


    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('logs');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

//        $this->belongsTo('Members', [
//            'foreignKey' => 'member_id',
//            'joinType' => 'INNER'
//        ]);
//        $this->belongsTo('Clients', [
//            'foreignKey' => 'client_id',
//            'joinType' => 'INNER'
//        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', null, 'create');

        $validator
            ->notEmptyString('status');

        $validator
            ->integer('action')
            ->notEmptyString('action');

        $validator
            ->scalar('source')
            ->maxLength('source', 100)
            ->allowEmptyString('source');

        $validator
            ->dateTime('created_on')
            ->allowEmptyDateTime('created_on');

        $validator
            ->dateTime('modified_on')
            ->allowEmptyDateTime('modified_on');

        return $validator;
    }

    
    public function getAllLogs($searchParams){
        $condition = "";
        if(isset($searchParams['from_date']) && !empty($searchParams['from_date'])){
            $searchParams['from_date'] = $searchParams['from_date']." 00:00:00";
            if(!empty($condition)){
                $condition .= " AND created_on >= '".$searchParams['from_date']."'";;
            }else{
                $condition = "created_on >= '".$searchParams['from_date']."'";;
            }
        }
        
        if(isset($searchParams['to_date']) && !empty($searchParams['to_date'])){
            $searchParams['to_date'] = $searchParams['to_date']." 23:23:59";
            if(!empty($condition)){
                $condition .= " AND created_on <= '".$searchParams['to_date']."'";;
            }else{
                $condition = "created_on <= '".$searchParams['to_date']."'";
            }
        }
        
        if(isset($searchParams['member_id']) && !empty($searchParams['member_id'])){
            if(!empty($condition)){
                $condition .= " AND member_id = ".$searchParams['member_id'];
            }else{
                $condition = "member_id = ".$searchParams['member_id'];
            }
        }
        
        if(isset($searchParams['client_id']) && !empty($searchParams['client_id'])){
            if(!empty($condition)){
                $condition .= " AND client_id = ".$searchParams['client_id'];
            }else{
                $condition = "client_id = ".$searchParams['client_id'];
            }
        }
        
        if(isset($searchParams['action_id']) && !empty($searchParams['action_id'])){
            if(!empty($condition)){
                $condition .= " AND action = ".$searchParams['action_id'];
            }else{
                $condition = "action = ".$searchParams['action_id'];
            }
        }
        
        if(isset($searchParams['search']) && !empty($searchParams['search'])){
            if(!empty($condition)){
                $condition .= " AND source Like '%".$searchParams['search']."%'";
            }else{
                $condition = "source Like '%".$searchParams['search']."%'";
            }
        }
        if(isset($searchParams['status']) && !empty($searchParams['status'])){
            if(!empty($condition)){
                $condition .= " AND status = ".$searchParams['status'];
            }else{
                $condition = "status = ".$searchParams['status'];
            }
        }
        
        return $this->find()->where($condition);
    }

    public function createtLog($inputData){
        $result = false;
        if(!empty($inputData)){
            $entity = $this->newEntity();
            $entity->member_id = $inputData['member_id'];
            $entity->client_id = $inputData['client_id'];
            $entity->source = $inputData['source'];
            $entity->status = $inputData['status'];
            $entity->action = $inputData['action'];
            $entity->description = $inputData['description'];
            $entity->created_on = $this->getSystemCurrentTimeStamp();
            $entity->modified_on = $this->getSystemCurrentTimeStamp();
            if($this->save($entity)){
                $result = true;
            }
        }
         return $result;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
//    public function buildRules(RulesChecker $rules)
//    {
//        $rules->add($rules->existsIn(['member_id'], 'Members'));
//        $rules->add($rules->existsIn(['client_id'], 'Clients'));
//
//        return $rules;
//    }
}
