<?php
namespace App\Model\Table;

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

/**
 * CmsuserStatus Model
 *
 * @property \App\Model\Table\CmsusersTable&\Cake\ORM\Association\BelongsTo $Cmsusers
 *
 * @method \App\Model\Entity\CmsuserStatus get($primaryKey, $options = [])
 * @method \App\Model\Entity\CmsuserStatus newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\CmsuserStatus[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\CmsuserStatus|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\CmsuserStatus saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\CmsuserStatus patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\CmsuserStatus[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\CmsuserStatus findOrCreate($search, callable $callback = null, $options = [])
 */
class CmsuserStatusTable extends AppTable
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

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

        $this->belongsTo('Cmsusers', [
            'foreignKey' => 'cmsuser_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
            ->notEmptyFile('is_account_profile_set');

        $validator
            ->notEmptyString('is_first_client_added');

        $validator
            ->notEmptyString('is_credit_report_uploaded');

        $validator
            ->notEmptyString('is_send_first_letter');

        $validator
            ->notEmptyString('is_automation');

        $validator
            ->notEmptyString('is_personalized_domain');

        $validator
            ->notEmptyString('is_agreement');

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

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

        return $validator;
    }

    /**
     * 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(['cmsuser_id'], 'Cmsusers'));
//
//        return $rules;
//    }
    
    public function insertCmsuserStatus($inputData){
        $result = false;
        if(!empty($inputData)){
            $cmsuser_id = $inputData['cmsuser_id'];
            $is_account_profile_set = isset($inputData['is_account_profile_set'])?$inputData['is_account_profile_set']:0;
            $is_first_client_added = isset($inputData['is_first_client_added']) ? $inputData['is_first_client_added']:0;
            $is_credit_report_uploaded = isset($inputData['is_credit_report_uploaded']) ? $inputData['is_credit_report_uploaded']:0;
            $is_send_first_letter = isset($inputData['is_send_first_letter']) ? $inputData['is_send_first_letter']:0;
            $is_automation = isset($inputData['is_automation']) ? $inputData['is_automation']:0;
            $is_personalized_domain = isset($inputData['is_personalized_domain']) ? $inputData['is_personalized_domain']:0;
            $is_agreement = isset($inputData['is_agreement']) ? $inputData['is_agreement']:0;
            
            $cmsuser_status = $this->newEntity();
            $cmsuser_status->cmsuser_id = $cmsuser_id;
            $cmsuser_status->is_account_profile_set = $is_account_profile_set;
            $cmsuser_status->is_first_client_added = $is_first_client_added;
            $cmsuser_status->is_credit_report_uploaded = $is_credit_report_uploaded;
            $cmsuser_status->is_send_first_letter = $is_send_first_letter;
            $cmsuser_status->is_automation = $is_automation;
            $cmsuser_status->is_personalized_domain = $is_personalized_domain;
            $cmsuser_status->is_agreement = $is_agreement;
            $cmsuser_status->created_at = $this->getSystemCurrentTimeStamp();
            
            if($this->save($cmsuser_status)){
                $result = true;
            }
        }
        return $result;
    }
    
    public function getCmsuserStatusByMemberId($member_id){
         $result = [];
         if($member_id > 0){
             $result = $this->find()->where(['cmsuser_id'=>$member_id])->first();
         }  
         return $result;
    }
    
//    public function updateCmsuserStatus($inputData){
//        $result = false;
//        if(!empty($inputData)){
//            $cmsuser_id = $inputData['cmsuser_id'];
//            $is_account_profile_set = isset($inputData['is_account_profile_set'])?$inputData['is_account_profile_set']:0;
//            $is_first_client_added = isset($inputData['is_first_client_added']) ? $inputData['is_first_client_added']:0;
//            $is_credit_report_uploaded = isset($inputData['is_credit_report_uploaded']) ? $inputData['is_credit_report_uploaded']:0;
//            $is_send_first_letter = isset($inputData['is_send_first_letter']) ? $inputData['is_send_first_letter']:0;
//            $is_automation = isset($inputData['is_automation']) ? $inputData['is_automation']:0;
//            $is_personalized_domain = isset($inputData['is_personalized_domain']) ? $inputData['is_personalized_domain']:0;
//            $is_agreement = isset($inputData['is_agreement']) ? $inputData['is_agreement']:0;
//            
//            $conditions['cmsuser_id'] = $cmsuser_id;
//            
//            if($is_account_profile_set > 0){
//                $fields['is_account_profile_set'] = $is_account_profile_set;
//            }
//            
//            if($is_first_client_added > 0){
//                $fields['is_first_client_added'] = $is_first_client_added;
//            }
//            
//            if($is_credit_report_uploaded > 0){
//                $fields['is_credit_report_uploaded'] = $is_credit_report_uploaded;
//            }
//            
//            if($is_send_first_letter > 0){
//                $fields['is_send_first_letter'] = $is_send_first_letter;
//            }
//            
//            if($is_automation > 0){
//                $fields['is_automation'] = $is_automation;
//            }
//            
//            if($is_personalized_domain > 0){
//                $fields['is_personalized_domain'] = $is_personalized_domain;
//            }
//            
//            if($is_agreement > 0){
//                $fields['is_agreement'] = $is_agreement;
//            }
//            $fields['updated_at'] = $this->getSystemCurrentTimeStamp();
//            if($this->updateAll($fields, $conditions)){
//                $result = true;
//            }
//        }
//        return $result;
//    }
    public function upgradeCmsuserStatus($member_id, $colum_name){
        $result = false;
        if($member_id > 0){
            $conditions['cmsuser_id'] = $member_id;
            $fields[$colum_name] = 1;
            $fields['updated_at'] = $this->getSystemCurrentTimeStamp();
            if($this->updateAll($fields, $conditions)){
              $result = true;
            }
        }
        
        return $result;
    }
    
    
    public function deleteCmsuserStatusByMemberId($member_id) {
        $result = false;
        
        if($member_id > 0){
            $conditions['cmsuser_id'] = $member_id;
            $result = $this->deleteAll($conditions);
        }
        
        return $result;
    }
    
    

}
