<?php
class User_Model extends CI_Model {
    public function __construct(){
        parent::__construct();
        $this->users = TBL_USERS;
        $this->db->query('SET SESSION sql_mode = ""');
        /*if ((!empty($this->user_data)) && (!empty($this->user_data['time_zone_offset']))) {
            error_log('SET time_zone = "' . $this->user_data['time_zone_offset'] . '"');
            $this->db->query('SET time_zone = "' . $this->session->user_data('time_zone_offset') . '"');
        }
        else if (defined('TIME_ZONE')) {
            $target_time_zone = new DateTimeZone(TIME_ZONE);
            $date_time = new DateTime('now', $target_time_zone);
            $this->db->query('SET time_zone = "' . $date_time->format('P') . '"');
        }
        
        $this->db->query( 'SET time_zone="'. ZONE_DIFF.'"' );*/
    }
    
    public function set_auto_commit($value) {
        $this->db->query('SET autocommit = ' . $value);
    }
    
    /*public function commit() {
        $this->db->query('COMMIT');
        $this->set_auto_commit(1);
    }
    
    public function roll_back() {
        $this->db->query('ROLLBACK');
        $this->set_auto_commit(1);
    }*/
    
    public function list_all() {
        $this->db->select('*, CASE role WHEN 1 THEN "User" WHEN 2 THEN "Administrator" WHEN 3 THEN "Super Admin" ELSE "" END as role_str', FALSE);
        $this->db->where('status != 2');
        $this->db->where('role != 3');
        $this->db->order_by('id', 'desc');
        return $this->db->get($this->users)->result();
    }
    
    public function get_by_id($id) {
        if( empty( $id ) ) :
            $user_data = get_user_data();
            $id = $user_data['user_id'];
        endif;
        $this->db->select('*, CASE role WHEN 1 THEN "User" WHEN 2 THEN "Administrator" WHEN 3 THEN "Super Admin" ELSE "" END as role_str', FALSE);
        $this->db->where('id', $id);
        return $this->db->get($this->users)->row();
    }
    
    public function insert($data) {
        $this->db->insert($this->users, $data);
        return $this->db->insert_id();
    }
    
    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->users, $data);
        return $this->db->affected_rows();
    }
    
    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->users);
        return $this->db->affected_rows();
    }
    
    public function authenticate_user($user_name, $password) {
        $this->db->where('user_name', $user_name);
        $this->db->where('password', $password);
        return $this->db->get($this->users)->row();
    }
    
    public function authenticate_user_by_name($user_name) {
        $this->db->where('user_name', $user_name);
        return $this->db->get($this->users)->row();
    }
    
    /*public function get_user_permission($id) {
        $this->db->where('id', $id);
        return $this->db->get($this->users)->row()->permissions;
    }*/
    
    public function user_name_exists($user_name, $id) {
        if ($id != '') {
            $this->db->where('id !=', $id);
        }
        
        $this->db->where('user_name', $user_name);
        return ($this->db->get($this->users)->num_rows() > 0) ? 1 : 0;
    }
    
    public function get_code() {
        $sql = 'SELECT FLOOR((RAND()* 8001)+1000) AS rand FROM '.TBL_USERS.' WHERE "rand" NOT IN (SELECT pass_code FROM '.TBL_USERS.' WHERE pass_code IS NOT NULL) LIMIT 1';
        $q = $this->db->query($sql);
        return $q->row_array()['rand'];
    }
    
    public function get_pass_code($code = '') {
        return password_hash( $code, PASSWORD_BCRYPT, array('cost' => 12) );
    }
    
    function profile( $user_id ) {
        $post = $this->input->post();
        $args = array(
            'name'          => $post['name'],
            'contact_no'    => $post['contact_no'],
        );   
        
        if( !empty( $post['pass1'] ) ) :
            $args['password'] = $post['pass1'];
        endif;
        
        $this->db->update( TBL_USERS, $args, array( 'id' => $user_id ) );
        set_user_data($this->get_by_id($user_id));
    }
}
?>