<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model
{
    protected $_table = 'users';

    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($dataInsert)
    {
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    public function update($id, $dataUpdate)
    {
        $this->db->where('id', $id);
        $this->db->update($this->_table, $dataUpdate);
    }

    public function getById($id)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getByUsername($username, $account_type, $id = '')
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.username', $username);
        $this->db->where($this->_table . '.user_type', $account_type);
        if ($id || $id != '') {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getByFullName($full_name, $account_type, $id = '')
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.full_name', $full_name);
        $this->db->where($this->_table . '.user_type', $account_type);
        if ($id || $id != '') {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getByEmail($email, $account_type, $id = '')
    {  
        // MyHelper::echoPreDie($email);
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.email', $email);
        $this->db->where($this->_table . '.user_type', $account_type);
        if ($id || $id != '') {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Kiểm tra mã dự thưởng xem đã có chưa
     * @param $reward_code
     * @return mixed
     */
    public function getByRewardCode($reward_code)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.reward_code', $reward_code);
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListForSelectBox()
    {
        $this->db->select('id, fullname, username, sid');
        $this->db->from($this->_table);
        $this->db->where('sid !=', '');
        $this->db->order_by($this->_table . '.username', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * @param null $name
     * @return int
     */
    public function cmsCountAll($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('username', $name, 'both');
            $this->db->or_like('email', $name, 'both');
            $this->db->or_like('full_name', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination($record, $start, $name = null)
    {
        
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('username', $name, 'both');
            $this->db->or_like('email', $name, 'both');
            $this->db->or_like('full_name', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        $this->db->order_by($this->_table . '.username', 'asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * Thống kê danh sách top: sắp xếp theo thứ tự ưu tiên thời gian hoàn thành, điểm
     * @param null $name
     * @return int
     */
    public function cmsCountAll2($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table . ' u');
        $this->db->join('game g', 'g.user_id = u.id');
        if ($name != null) {
            $this->db->like('u.fullname', $name, 'both');
            $this->db->or_like('u.username', $name, 'both');
            $this->db->or_like('u.address', $name, 'both');
            $this->db->or_like('u.email', $name, 'both');
            $this->db->or_like('u.mobile', $name, 'both');
            $this->db->or_like('u.reward_code', $name, 'both');
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination2($record, $start, $name = null)
    {
        $this->db->select('u.fullname, u.username, u.reward_code, g.total_time, g.total_point, g.total_game');
        $this->db->from($this->_table . ' u');
        $this->db->join('game g', 'g.user_id = u.id');
        if ($name != null) {
            $this->db->like('u.fullname', $name, 'both');
            $this->db->or_like('u.username', $name, 'both');
            $this->db->or_like('u.address', $name, 'both');
            $this->db->or_like('u.email', $name, 'both');
            $this->db->or_like('u.mobile', $name, 'both');
            $this->db->or_like('u.reward_code', $name, 'both');
        }
        $this->db->order_by('g.total_point desc, g.total_time asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * -----------------------------------------------------------------------------------------------------------------
     * Xuất mã dự thưởng
     * @param null
     * @return int
     */
    public function exportCountAll()
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function exportGetPagination($record, $start)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->order_by($this->_table . '.created_at', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getIdFullname()
    {
        $this->db->select('id, full_name');
        $this->db->from($this->_table);
        $query = $this->db->get();
        $data = $query->result_array();
        $result = [];
        if ($data) {
            foreach ($data as $val) {
                $result[$val['id']] = $val['full_name'];
            }
        }
        return $result;
    }

    function getUser($id = null, $block = true) {
        /*if ($id) {
            $blockIds = ($block) ? $this->blockIds() : '0';
            $query = $this->db->query("SELECT * FROM users WHERE (id=? OR email=? OR username=?) AND id NOT IN ($blockIds) AND active=?", $id,$id,$id, 1);
            if ($query) $user = $query->result_array();
            return $user;
        } else {
            return  $this->authUser;
        }*/

        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();

    }


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */