<?php

class Executive_view extends CI_controller
{
    public function __construct()
    {
        parent::__construct();

        // Load url helper for redirect to work
        $this->load->helper('url');

        // Load form helper library
        $this->load->helper('form');

        // Load form validation library
        $this->load->library('form_validation');

        // Load session library
        $this->load->library('session');

        // Set timezone to Philippines
        date_default_timezone_set('Asia/Manila');
    }

    public function generateTransID($mmddyyDate)
    {
        $dateFormated = DateTime::createFromFormat('m/d/Y', $mmddyyDate);
		return $this->session->user["branch"] . $dateFormated->format('Ymd');
    }

    public function generateyyyyddmmFormat($mmddyyDate)
    {
        $dateFormated = DateTime::createFromFormat('m/d/Y', $mmddyyDate);
		return $dateFormated->format('Ymd');
    }

    public function dashboard()
    {
        if(!$this->isLogged())
            redirect('exec/login', 'refresh');

        $data["selectedDate"] = date('m/d/Y');
        $data["htmlTableList"] = "";
        $this->load->model("StoreTransaction");

        if($this->input->get("transDate", true) != null)
            $data["selectedDate"] = $this->input->get("transDate", true);

        $transDate = $this->generateyyyyddmmFormat($data["selectedDate"]);
        $result = $this->StoreTransaction->getStoreTransactionSummaryByTransDate($transDate);

        foreach($result->result() as $row)
            $data["htmlTableList"] .= '<tr><td>'.$row->brCode.'</td><td>'.number_format($row->TotalSales, 2).'</td><td><a class="btn btn-default" href="http://host2048.temp.domains/~karatwor/applications/webFunctions/cimsReports/salesReport.php?keyrequest=4c674b4c417766106df70e7a0b93ef67&transID=' . $row->brCode . $transDate . '&brCode=' . $row->brCode . '&transDate=' . $this->input->get("transDate", true) . '" target="_blank">View</a></td><td><a class="btn btn-default" href="http://host2048.temp.domains/~karatwor/applications/webFunctions/cimsReports/inventoryReport.php?keyrequest=4c674b4c417766106df70e7a0b93ef67&transID=' . $row->brCode . $transDate . '&brCode=' . $row->brCode . '&transDate=' . $this->input->get("transDate", true) . '" target="_blank">View</a></td></tr>';

        $this->load->view('executiveview/dashboard', $data);
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect('exec/login', 'refresh');
    }

    public function login()
    {
        $this->load->view('executiveview/login');
    }

    public function isLogged()
    {
        if(isset($this->session->user) && $this->session->user["sessid"] == $this->input->cookie("sessid"))
            return true;
        else
            return false;
    }

    public function authenticate_user()
    {
        $this->form_validation->set_rules('txtUsername', 'Username', 'trim|alpha_dash|required');
        $this->form_validation->set_rules('txtPass', 'Password', 'trim|alpha_dash|required');
        $this->form_validation->set_rules('btnSubmit', 'Submit', 'callback_validate_user');
    
        if($this->form_validation->run() == false)
          $this->login();
        else
          redirect('/exec', 'refresh');
    }

    public function validate_user()
    {
        if($this->input->post('txtUsername') =="" || $this->input->post('txtPass') == "")
        {
            $this->form_validation->set_message('validate_user', 'Username or Password is empty.');
            return false;
        }

        $this->load->model('UserInfo');
        
        $result = $this->UserInfo->verifyUser($this->input->post('txtUsername'), $this->input->post('txtPass'));
    
        if($result == true)
        {
            if($this->UserInfo->userlvl == 0 ||
            $this->UserInfo->userlvl == 1 )
            {
                $this->session->user = array('username' => $this->UserInfo->username, 
                'fullName' => $this->UserInfo->fullName, 
                'userlvl' => $this->UserInfo->userlvl,
                'branch' => $this->UserInfo->branch,
                'sessid' => $this->session->session_id);  

                $this->input->set_cookie("sessid", $this->session->user["sessid"], 60000);

                return true;
            }  
            else
            $this->form_validation->set_message('validate_user', 'Your user level is not allowed to access this page.');
            return false;          
        }
        else
        {
            $this->form_validation->set_message('validate_user', 'Invalid username or password. ');
            return false;
        }
    }
}