<?php

if(!defined('BASEPATH'))
{
    exit('No direct script access allowed');
}

/**
 * Description of visitorcontroller
 *
 * @author https://roytuts.com
 */
class VisitorController extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('visitormodel');
        $this->load->library('session');

        $this->load->model('SiteDataModel');
        // $this->SiteDataModel->insert_row();
    }

    public function index()
    {
        $site_statics_today            = $this->visitormodel->get_site_data_for_today();
        $site_statics_last_week        = $this->visitormodel->get_site_data_for_last_week();
        $data['visits_today']          = isset($site_statics_today['visits']) ? $site_statics_today['visits'] : 0;
        $data['visits_last_week']      = isset($site_statics_last_week['visits']) ? $site_statics_last_week['visits'] : 0;
        $data['chart_data_today']      = $this->visitormodel->get_chart_data_for_today();
        $data['chart_data_curr_month'] = $this->visitormodel->get_chart_data_for_month_year();

        $this->load->view('visitor', $data);
    }

    function get_chart_data()
    {
        if(isset($_POST))
        {
            if(isset($_POST['month']) && strlen($_POST['month']) && isset($_POST['year']) && strlen($_POST['year']))
            {
                $month = $_POST['month'];
                $year  = $_POST['year'];
                $data  = $this->visitormodel->get_chart_data_for_month_year($month, $year);
                if($data !== NULL)
                {
                    foreach($data as $value)
                    {
                        echo $value->day . "t" . $value->visits . "n";
                    }
                }
                else
                {
                    $timestamp = mktime(0, 0, 0, $month);
                    $label     = date("F", $timestamp);
                    echo '<div style="width:600px;position:relative;font-weight:bold;top:100px;margin-left:auto;margin-left:auto;color:red;">No data found for the "' . $label . '-' . $year . '"</div>';
                }
            }
            else if(isset($_POST['month']) && strlen($_POST['month']))
            {
                $month = $_POST['month'];
                $data  = $this->visitormodel->get_chart_data_for_month_year($month);
                if($data !== NULL)
                {
                    foreach($data as $value)
                    {
                        echo $value->day . "t" . $value->visits . "n";
                    }
                }
                else
                {
                    $timestamp = mktime(0, 0, 0, $month);
                    $label     = date("F", $timestamp);
                    echo '<div style="width:600px;position:relative;font-weight:bold;top:100px;margin-left:auto;margin-left:auto;color:red;">No data found for the "' . $label . '"</div>';
                }
            }
            else if(isset($_POST['year']) && strlen($_POST['year']))
            {
                $year = $_POST['year'];
                $data = $this->visitormodel->get_chart_data_for_month_year(0, $year);
                if($data !== NULL)
                {
                    foreach($data as $value)
                    {
                        echo $value->day . "t" . $value->visits . "n";
                    }
                }
                else
                {
                    echo '<div style="width:600px;position:relative;font-weight:bold;top:100px;margin-left:auto;margin-left:auto;color:red;">No data found for the "' . $year . '"</div>';
                }
            }
            else
            {
                $data = $this->visitormodel->get_chart_data_for_month_year();
                if($data !== NULL)
                {
                    foreach($data as $value)
                    {
                        echo $value->day . "t" . $value->visits . "n";
                    }
                }
                else
                {
                    echo '<div style="width:600px;position:relative;font-weight:bold;top:100px;margin-left:auto;margin-left:auto;color:red;">No data found!</div>';
                }
            }
        }
    }

    private function _log_activity($page = 'default_page')
    {
        $user_ip = $this->getUserIP();
        $sql     = "INSERT INTO log_table (user_ip, page, request_date) VALUES($user_ip, $page, NOW())";
        $this->db->query($sql);
    }

}

/* End of file visitorcontroller.php */
/* Location: ./application/controllers/visitorcontroller.php */