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

class register_model extends CI_Model {
	function __construct(){
        parent::__construct();
	}

	public function get_voucher($voucher){
		$sql = "SELECT *, item.id as id FROM register
                JOIN item ON item.id = register.id_image
                WHERE register.voucher = '".$voucher."'";
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function check_username($username){
		$sql = "SELECT * FROM register WHERE username = '".$username."'";
		$query = $this->db->query($sql);
		return $query->result();
	}

	// public function get_voucher($voucher){
	// 	$response = false;
	// 	$query = $this->db->get_where('register',array('voucher' => $voucher));
	// 	if($query && $query->num_rows()){
	// 		$response = $query->result_array();
	// 	}
	// 	return $response;
	// }
	
	public function get_all($limit_offset = array()){
		if(!empty($limit_offset)){
			if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
			$query = $this->db->get("register",$limit_offset['limit'],$limit_offset['offset']);
		}else{
			if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
			$query = $this->db->get("register");
		}
		return $query->result();
	}
	public function count_total(){
		if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
		$query = $this->db->get("register");
		return $query->num_rows();
	}
	public function get_all_array($filter = ''){
		if(!empty($filter)) {
			if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
			$query = $this->db->get_where("register",$filter);
		}else{
			if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
			$query = $this->db->get_where("register");
		}
		return $query->result_array();
	}
	public function get_last_id(){
		$this->db->order_by('id', 'DESC');

		$query = $this->db->get("register",1,0);
		return $query->result();
	}
	public function insert($data){
		$this->db->insert('register', $data);
	}
	public function update($id,$data){
		$this->db->where('id', $id);
		$this->db->update('register', $data);
	}
	public function update_voucher($voucher,$data){
		$this->db->where('voucher', $voucher);
		$this->db->update('register', $data);
	}
	public function get_by_id($id){
		$response = false;
		$query = $this->db->get_where('register',array('id' => $id));
		if($query && $query->num_rows()){
			$response = $query->result_array();
		}
		return $response;
	}
	public function delete($id){
		$this->db->delete('register', array('id' => $id));
	}
	public function get_filter($filter = '',$limit_offset = array()){
		if(!empty($filter)){
			$query = $this->db->get_where("register",$filter,$limit_offset['limit'],$limit_offset['offset']);
		}else{
			$query = $this->db->get("register",$limit_offset['limit'],$limit_offset['offset']);
		}
		return $query->result();
	}
	public function count_total_filter($filter = array()){
		if ($_SESSION['username'] !== "admin"){
			$query = $this->db->where("created_by", $_SESSION['username']);
			}
		if(!empty($filter)){
			$query = $this->db->get_where("register",$filter);
		}else{
			$query = $this->db->get("register");
		}
		return $query->num_rows();
	}
}