<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 * contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of User_model
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class User_model extends CI_Model {

    const TABLE_NAME = 'users';

    private function _template() {
        $this->db->select('t0.userid, '
                . 't0.username, '
                . 't0.userpassword, '
                . 't0.userauth, '
                . 't0.useremail, '
                . 't1.profilename, '
                . 't1.profileaddress, '
                . 't1.profilemobile, '
                . 't1.profilepath, '
                . 't1.profilepathtitle, '
                . 't1.profilepathalt, '
                . 't3.roleid, '
                . 't3.rolename');
        $this->db->from(self::TABLE_NAME . ' `t0`');
        $this->db->join('profiles `t1`', 't0.userid = t1.userid', 'left');
        $this->db->join('userrole `t2`', 't0.userid = t2.userid', 'left');
        $this->db->join('roles `t3`', 't2.roleid = t3.roleid', 'left');
    }

    public function session_template($user) {
        return array(
            'userid' => $user['userid'],
            'userauth' => $user['userauth'],
            'roleid' => $user['roleid'],
            'rolename' => $user['rolename'],
            'profilename' => $user['profilename'],
            'profileaddress' => $user['profileaddress'],
            'useremail' => $user['useremail'],
            'profilemobile' => $user['profilemobile'],
            'profilepath' => $user['profilepath'],
            'profilepathtitle' => $user['profilepathtitle'],
            'profilepathalt' => $user['profilepathalt']
        );
    }

    public function get_id($id) {
        $this->_template();
        $this->db->where('t0.userid', $id);
        $this->db->where('t0.userstatus', 1);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_login($username, $password) {
        $this->_template();
        $this->db->where('t0.username', $username);
        $this->db->where('t0.userstatus', 1);
        $query = $this->db->get();
        if (($result = $query->row_array())) {
            if (read_data($result['userpassword']) == $password) {
                return $result;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function get_role($id) {
        
    }

    public function get_email_login($email, $password) {
        $this->_template();
        $this->db->where('t0.useremail', $email);
        $this->db->where('t0.userstatus', 1);
        $query = $this->db->get();
        if (($result = $query->row_array())) {
            if (read_data($result['userpassword']) == $password) {
                return $result;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    public function get_email($email) {
        $this->_template();
        $this->db->where('t0.useremail', $email);
        $this->db->where('t0.userstatus', 1);
        $query = $this->db->get();
        return $query->row_array();
    }
    public function get_auth($auth) {
        $this->_template();
        $this->db->where('t0.userauth', $auth);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_code($code) {
        $this->_template();
        $this->db->where('t0.usercode', $code);
        $query = $this->db->get();
        return $query->row_array();
    }
    public function get_counter(){
        $counter = array();
        $this->db->from(self::TABLE_NAME .' `t0`');
        $this->db->join('userrole `t1`', 't0.userid = t1.userid', 'left');
        $this->db->where('t1.roleid', 1);
        $counter['admin'] = $this->db->count_all_results();
        $this->db->from(self::TABLE_NAME .' `t0`');
        $this->db->join('userrole `t1`', 't0.userid = t1.userid', 'left');
        $this->db->where_in('t1.roleid', array(2,3,4));
        $counter['user'] = $this->db->count_all_results();
//        $this->db->select('SUM(visitorscount) as total');
//        $this->db->from('visitors');
//        $query=$this->db->get();
//        $result=$query->row_array();
//        $counter['visitors'] = ($result['total']) ? $result['total'] : 0;
//        $this->db->select('visitorscount, created');
//        $this->db->from('visitors');
//        $query1=$this->db->get();
//        $result1=$query1->result_array();
//        $counter['visitors_detail'] = $result1;
        /* $this->db->from('timeline');
        $counter['timeline'] = $this->db->count_all_results(); */
        return $counter;
    }
    public function check_password($userid, $password) {
        $this->db->select('userpassword');
        $this->db->from(self::TABLE_NAME);
        $this->db->where('userid', $userid);
        $query = $this->db->get();
        $result = $query->row_array();
        if(!$result || $password != read_data($result['userpassword'])){
            return false;
        }
        return true;
    }
    public function check_id($id){
        $this->db->from(self::TABLE_NAME);
        $this->db->where('userid', $id);
        return $this->db->count_all_results();
    }

    public function update_auth($auth, $id) {
        $this->db->set(array('userauth' => $auth));
        $this->db->where('userid', $id);
        $this->db->update(self::TABLE_NAME);
    }

    public function update_code($code, $id) {
        $this->db->set(array('usercode' => $code));
        $this->db->where('userid', $id);
        $this->db->update(self::TABLE_NAME);
    }
    public function update_password($password, $id){
        $this->db->set(array('userpassword' => hash_data($password)));
        $this->db->where('userid', $id);
        $this->db->update(self::TABLE_NAME);
    }
}
