<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	class Users_model extends CI_Model
	{
 
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
 
	public function get_users() {
	  return $this->db->get("xin_users");
	}
		
	public function get_all_users() {
	  $query = $this->db->get("xin_users");
	  return $query->result();
	}
	
	// get all provinsi
	public function get_provinsi()
	{
	  $query = $this->db->query("SELECT kode,nama FROM wilayah WHERE CHAR_LENGTH(kode)=2 ORDER BY nama");
  	  return $query->result();
	}
	 
	public function read_users_info($id) {
	
		$sql = 'SELECT * FROM xin_users WHERE user_id = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
	}
	
	public function read_users_info_job($id) {
	
		$sql = 'SELECT b.first_name,b.last_name,c.job_title FROM xin_job_applications a 
		JOIN xin_users b ON b.user_id = a.id_user
		JOIN xin_jobs c ON c.job_id = a.job_id
		WHERE a.application_id = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
	}
	
	// get single record > company | locations
	 public function ajax_kota_ktp_info($id) {
	    $n=strlen($id);
        $m=($n==2?5:($n==5?8:13));
        
		$sql = "SELECT kode,nama FROM wilayah WHERE LEFT(kode,'$n')='$id' AND CHAR_LENGTH(kode)=$m ORDER BY nama";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return null;
		}
	}
	
	// get single record > company | locations
	 public function get_wilayah($id) {
		$sql = "SELECT kode,nama FROM wilayah WHERE kode = '$id'";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return null;
		}
	}
	
	// get single record > company | locations
	 public function ajax_kec_ktp_info($id) {
	    $n=strlen($id);
        $m=($n==2?5:($n==5?8:13));
        
		$sql = "SELECT kode,nama FROM wilayah WHERE LEFT(kode,'$n')='$id' AND CHAR_LENGTH(kode)=$m ORDER BY nama";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return null;
		}
	}
	
	public function read_users_answer($id) {
	
		$sql = 'SELECT * FROM xin_user_answer WHERE user_id = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
	}
	
	public function read_users_question() {
	
		$sql = 'SELECT * FROM xin_user_questions';
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0) {
			return $query->result();
		} else {
			return false;
		}
	}
	// check email address
	public function check_user_email($email) {
	
		$sql = 'SELECT * FROM xin_users WHERE email = ?';
		$binds = array($email);
		$query = $this->db->query($sql, $binds);
		
		return $query;
	}
	
	// Function to add record in table
	public function add($data){
		$this->db->insert('xin_users', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	// Function to add record in table
	public function insert_answer($data){
		$this->db->insert('xin_user_answer', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}
		
	// Function to Delete selected record from table
	public function delete_record($id){
		$this->db->where('user_id', $id);
		$this->db->delete('xin_users');
		
	}
	
	// Function to Delete selected record from table
	public function delete_document($id){
		$this->db->where('id_document', $id);
		if($this->db->delete('xin_user_documents')) {
			return true;
		} else {
			return false;
		}	
	}
	
	// Function to Delete selected record from table
	public function delete_bahasa($id){
		$this->db->where('id_bahasa', $id);
		if($this->db->delete('xin_user_bahasa')) {
			return true;
		} else {
			return false;
		}	
	}
	
	// Function to Delete selected record from table
	public function delete_family($id){
		$this->db->where('id_family', $id);
		if($this->db->delete('xin_user_families')) {
			return true;
		} else {
			return false;
		}	
	}
	
	// Function to Delete selected record from table
	public function delete_pendidikan($id){
		$this->db->where('id_pendidikan', $id);
		if($this->db->delete('xin_user_pendidikan')) {
			return true;
		} else {
			return false;
		}	
	}
	
	// Function to Delete selected record from table
	public function delete_work($id){
		$this->db->where('id_work', $id);
		if($this->db->delete('xin_user_work')) {
			return true;
		} else {
			return false;
		}	
	}
	
	// get documents
	public function set_employer_documents($id) {
	
		$sql = 'SELECT * FROM xin_user_documents WHERE id_user = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
	    return $query;
	}
	
	// get documents
	public function set_employer_family($id) {
	
		$sql = 'SELECT * FROM xin_user_families WHERE id_user = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
	    return $query;
	}
	
	// get documents
	public function set_employer_bahasa($id) {
	
		$sql = 'SELECT * FROM xin_user_bahasa WHERE id_user = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
	    return $query;
	}
	
	// get documents
	public function set_employer_pendidikan($id) {
	
		$sql = 'SELECT * FROM xin_user_pendidikan WHERE id_user = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
	    return $query;
	}
	
	// get documents
	public function set_employer_work($id) {
	
		$sql = 'SELECT * FROM xin_user_work WHERE id_user = ?';
		$binds = array($id);
		$query = $this->db->query($sql, $binds);
		
	    return $query;
	}
	
	// Function to add record in table > document info
	public function document_info_add($data){
		$this->db->insert('xin_user_documents', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to add record in table > document info
	public function work_info_add($data){
		$this->db->insert('xin_user_work', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to add record in table > document info
	public function family_info_add($data){
		$this->db->insert('xin_user_families', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to add record in table > document info
	public function bahasa_info_add($data){
		$this->db->insert('xin_user_bahasa', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to add record in table > document info
	public function pendidikan_info_add($data){
		$this->db->insert('xin_user_pendidikan', $data);
		if ($this->db->affected_rows() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to update record in table
	public function update_record($data, $id){
		$this->db->where('user_id', $id);
		if( $this->db->update('xin_users',$data)) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to update record in table
	public function update_answer($question,$id, $data){
		$this->db->where('id_question', $question);
		$this->db->where('user_id', $id);
		if( $this->db->update('xin_user_answer',$data)) {
			return true;
		} else {
			return false;
		}		
	}
	
	// Function to update record without photo > in table
	public function update_record_no_photo($data, $id){
		$this->db->where('user_id', $id);
		if( $this->db->update('xin_users',$data)) {
			return true;
		} else {
			return false;
		}		
	}
}
?>