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

class Report extends MY_Controller {

	/**
	 * 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->load->model("User_model", "user");
		$this->load->model("Activity_model", "activity");
		$this->load->model("Tracking_model", "track");
    }
    public function index()
	{
		$year = date('Y');
		$month = date('m');
		$level = $this->session->userdata('level');
		if($level < 3){
			$this->get_report_of_month($year, $month);
		}else{
			$this->get_user_report_of_month($year, $month);
		}
		
	}
	
	public function get_report_of_month($year, $month, $show_distance = 1){
		$start = $year."-".$month."-01";
		$end   = $year."-".$month."-31";
		$result = array();
		$res = $this->activity->get_activities_between($start, $end);
		if($res["success"] == 1){
			$result_data = $res["data"];
			foreach($result_data as $row){
				if(!isset($result[$row["user_name"]])) {
					$result[$row["user_name"]] = array();
				}

				$from_time = strtotime($row["created_date"]." ".$row["on_time"]);
				$to_time = strtotime($row["created_date"]." ".$row["on_time"]);

				$mins = round(($to_time - $from_time) / 60);
				$hours = $mins / 60;
				$mins = $mins % 60;
				$distance = $this->track->get_distance_of_day($row["created_date"], $row["user_id"]);
				$data_row = array("user_id"=>$row["user_id"], "on_time" => $row["on_time"], "off_time"=>$row["off_time"], "duration" => $hours.":".$mins, "distance" => $distance, "day"=> date('d', strtotime($row["created_date"])));
				array_push($result[$row["user_name"]], $data_row);
			}
		}
		$res_data['view'] = 'pages/report';
		$res_data['data'] = array("result"=>$result, "year"=>$year, "month"=>$month, "show_distance" => $show_distance);
		$this->load->view('layout', $res_data);
	}

	public function get_user_report_of_month($year, $month, $show_distance = 1){
		$user_id = $this->session->userdata('user_id');
		$start = $year."-".$month."-01";
		$end   = $year."-".$month."-31";
		$result = array();
		$res = $this->activity->get_user_activities_between($user_id, $start, $end);
		if($res["success"] == 1){
			$result_data = $res["data"];
			foreach($result_data as $row){
				if(!isset($result[$row["user_name"]])) {
					$result[$row["user_name"]] = array();
				}

				$from_time = strtotime($row["created_date"]." ".$row["on_time"]);
				$to_time = strtotime($row["created_date"]." ".$row["on_time"]);

				$mins = round(($to_time - $from_time) / 60);
				$hours = $mins / 60;
				$mins = $mins % 60;
				$distance = $this->track->get_distance_of_day($row["created_date"], $user_id);
				$data_row = array("user_id"=>$row["user_id"], "on_time" => $row["on_time"], "off_time"=>$row["off_time"], "duration" => $hours.":".$mins, "distance" => $distance, "day"=> date('d', strtotime($row["created_date"])));
				array_push($result[$row["user_name"]], $data_row);
			}
		}
		$res_data['view'] = 'pages/report';
		$res_data['data'] = array("result"=>$result, "year"=>$year, "month"=>$month, "show_distance" => $show_distance);
		$this->load->view('layout', $res_data);
	}
}
