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

class Dashboard extends MY_Controller {

	public $data = array();

	public function __construct() {

		parent::__construct();

		if(!$this->session->userdata('IsLogin')) {
			redirect($this->globalUrl.'main');	
		}

		$this->load->library('form_validation');
		$this->load->model('dashboard_model');
	}

	public function index()
	{	

		$fromDate = isset($_GET['fromDate']) ? date('Y-m-d 00:00:00', strtotime($this->input->get('fromDate'))) : '2018-01-01 00:00:00';
		$toDate = isset($_GET['toDate']) ? date('Y-m-d 23:59:59', strtotime($this->input->get('toDate'))) : date('Y-m-d 23:59:59');

		if($this->input->post('search')) {
			
			$fromDate = date('Y-m-d 00:00:00', strtotime($this->input->get('fromDate')));
			$toDate = date('Y-m-d 23:59:59', strtotime($this->input->get('toDate')));

		}

		// Query for total click counts
		$countResult = $this->dashboard_model->get_all_click_counts($this->session->userdata('ID'), $fromDate, $toDate);

		// Query for link
		$linkResult = $this->dashboard_model->get_all_links($this->session->userdata('ID'), $fromDate, $toDate);	

		// Check if got size or not
		if(!$linkResult) {
			$linkResult = array();
		}

		// Transfer data to view
		$this->data['linkResult'] = $linkResult;
		$this->data['totalClickCount'] = $countResult->TotalClicks;
		$this->data['fromDate'] = date('d-m-Y', strtotime($fromDate));
		$this->data['toDate'] = date('d-m-Y', strtotime($toDate));
		$this->load->template('dashboard', $this->data);
		
	}
}
