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

class Admin_model extends CI_Model {
    protected $_table='admin';

    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.'.*, group.name as groupName, group.description as groupDescription, group.group_type as groupType');
        $this->db->from($this->_table);
        $this->db->join('group', 'group.id = admin.group_id');
        $this->db->where($this->_table.'.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getFullnameById($id){
        $this->db->select($this->_table.'.fullname');
        $this->db->from($this->_table);
        $this->db->where($this->_table.'.id', $id);
        $this->db->limit(1);
        $query = $this->db->get();
        $arr = array();
        $arr = $query->result_array();
        return $arr[0]['fullname'];

    }

    /**
     * @param bool $is_active: có lấy is_active hay không
     * @param null $admin_id: lấy theo id của admin
     * @param bool $filter: =true nghĩa là ko lấy dữ liệu
     * @return array
     */
    public function getListSID($is_active=true, $admin_id=null, $filter=false){
        $this->db->select('sid');
        $this->db->from($this->_table);
        if($is_active == true){
            $this->db->where('is_active', 1);
        }
        if($admin_id != null){
            $this->db->where('id', $admin_id);
        }else{
            if($filter == true){
                $this->db->where('id', $admin_id);
            }
        }
        $query = $this->db->get();
        $arr = $query->result_array();

        $temp = array();
        $sid = array();
        foreach($arr as $val){
            $temp = explode(',', $val['sid']);
            foreach($temp as $valTemp){
                if(trim($valTemp) > 0){
                    $sid[] = trim($valTemp);
                }
            }
        }

        return $sid;
    }

    public function getListSIdBySMS($is_active=true, $admin_id=null, $filter=false){
        $this->db->select('sms_code');
        $this->db->from($this->_table);
        if($is_active == true){
            $this->db->where('is_active', 1);
        }
        if($admin_id != null){
            $this->db->where('id', $admin_id);
        }else{
            if($filter == true){
                $this->db->where('id', $admin_id);
            }
        }
        $query = $this->db->get();
        $arr = $query->result_array();

        $temp = array();
        $sid_sms = array();
        foreach($arr as $val){
            $temp = explode(',', $val['sms_code']);
            if(count($temp) > 0){
                foreach($temp as $valTemp){
                    if(trim($valTemp) != '' && $valTemp != null){
                        $sid_sms[] = trim($valTemp);
                    }
                }
            }

        }

        return $sid_sms;
    }

    public function countAll($name=null){
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if($name != null){
            $this->db->like('name', $name, 'both');
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if($result){
            return $result[0]['totalResults'];
        }else{
            return 0;
        }
    }

    public function getPagination($record, $start, $name=null){
        $this->db->select($this->_table.'.*, group.name as groupName, group.description as groupDescription, group.group_type as groupType');
        $this->db->from($this->_table);
        $this->db->join('group', 'group.id = admin.group_id');
        if($name != null){
            $this->db->like($this->_table.'.name', $name, 'both');
        }
        $this->db->order_by($this->_table.'.username', 'asc');
        $this->db->limit($record, $start);
        $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();
    }

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

        $temp = array();
        if($add == false){
            $temp[''] = '-- Chọn --';
        }
        foreach($arr as $val){
            $temp[$val['id']] = $val['username'].' ('.$val['fullname'].')';
        }

        return $temp;
    }

    public function getUserAdminLogin($username, $password){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);
        $this->db->join('group', 'group.id = admin.group_id');
        $this->db->where($this->_table.'.username', $username);
        $this->db->where($this->_table.'.password', $password);
        $this->db->where('group.is_active', 1);
        $this->db->where($this->_table.'.is_active', 1);
        $this->db->limit(1, 0);
        $query = $this->db->get();
        return $query->result_array();
    }

}

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