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

class Uuid_model extends MY_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct(){
		parent::__construct();
		$this->tb_name = "tbl_uuid";
	}

	public function save($uuid){
		$query = $this->db->select("id")
						  ->from($this->tb_name)
						  ->where('uuid', $uuid)
						  ->get();
		if($query->num_rows() > 0){
			$row = $query->row_array();
			return $row['id'];
		}else{
            $row = array('uuid'=>$uuid, 'assigned' =>0);
			$id = $this->insert($row);
			return $id;
		}
	}

	public function unassigned_uuid(){
		$query = $this->db->from($this->tb_name)
						  ->where('assigned', 0)
						  ->get();
		if($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	public function set_assign($uuid){
		$this->db->where('uuid', $uuid)
			     ->delete($this->tb_name);
	}
}
