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

class Pins_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_pins";
    }
    
    public function get_data($count = 50){
        $query = $this->db->select("*")
                        ->from($this->tb_name." as a")
                        ->join("tbl_user as b", "a.user_id = b.id")
                        ->order_by('a.id', 'desc')
                        ->limit($count)
                        ->get();
        if($query->num_rows()>0){
            return array("success"=>TRUE, "data" => $query->result_array());
        }else{
            return array("success"=>FALSE, "message" => "No data to display!", "data" => array());
        }
    }

    public function get_pins_of_user($user_id){
        $query = $this->db->select("*")
                          ->from($this->tb_name." as a")
                          ->join("tbl_user as b", "a.user_id = b.id")
                          ->where("user_id", $user_id)
                          ->order_by('a.created_date', 'asc')
                          ->get();
        if($query->num_rows()>0){
            return array("success"=>TRUE, "data" => $query->result_array());
        }else{
            return array("success"=>FALSE, "message" => "No data to display!", "data" => array());
        }
    }

    public function get_pins_of_period($user_id, $start, $end) {
        $query = $this->db->select("*")
                          ->from($this->tb_name." as a")
                          ->join("tbl_user as b", "a.user_id = b.id")
                          ->where("a.user_id", $user_id)
                          ->where("a.created_date between '{$start}' and '{$end}'")  
                          ->order_by('a.created_date', 'asc')
                          ->get();
        if($query->num_rows()>0){
            return array("success"=>TRUE, "data" => $query->result_array());
        }else{
            return array("success"=>FALSE, "message" => "No data to display!", "data" => array());
        }
    }

    public function get_last_date($user_id){
        $query = $this->db->select("*")
                          ->from($this->tb_name)
                          ->where('user_id', $user_id)
                          ->order_by("created_date", "desc")
                          ->limit(1)
                          ->get();
        if($query->num_rows()>0){
            $row = $query->row_array();
            return $row["created_date"];
        }else{
            return "2018-01-01 00:00:00";
        }
    }
    
    public function delete_data_between($start, $end)
    {
        $start_time = $start . " 00:00:00";
        $end_time = $end . " 23:59:59";
        $result = $this->db->where("created_date between '{$start}' and '{$end}'")
                           ->delete($this->tb_name);
        return $result;
    }
    
}
