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

class Activity_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_activity";
	}

    public function get_activities_between($start, $end){
        $query = $this->db->select("a.created_date as created_date, a.on_time as on_time, a.off_time as off_time, b.user_name as user_name, b.id as user_id")
                          ->from($this->tb_name." as a")
                          ->join("tbl_user as  b", "a.user_id = b.id")
                          ->where("created_date between '{$start}' and '{$end}'")
                          ->order_by('user_id', "asc")
                          ->order_by('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_user_activities_between($user_id, $start, $end){
        $query = $this->db->select("a.created_date as created_date, a.on_time as on_time, a.off_time as off_time, b.user_name as user_name, b.id as user_id")
                          ->from($this->tb_name." as a")
                          ->join("tbl_user as  b", "a.user_id = b.id")
                          ->where("created_date between '{$start}' and '{$end}'")
                          ->where('user_id', $user_id)
                          ->order_by('user_id', "asc")
                          ->order_by('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";
        }
    }

    public function delete_data_between($start, $end){
        $result = $this->db->where("created_date between '{$start}' and '{$end}'")
                          ->delete($this->tb_name);
        return $this->db->affected_rows();
    }
    
    public function save_or_update(array $data) {
        foreach($data as $row) {
            $query = $this->db->where('created_date', $row['created_date'])
                              ->from($this->tb_name)
                              ->get();
            if($query->num_rows() > 0) {
                $this->db->where('created_date' , $row["created_date"])
                         ->update($this->tb_name, $row);
            }else{
                $this->insert($row);
            }
        }
    }
}
