<?php
/**
 * Manually Developed CI View - accesslogreport_wb.php
 *
 * @package			AIR\views
 * @version			2.7
 * @uses
 * @see
 * @copyright		2015, BizyCorp Internal Systems Development
 * @license			private, All rights reserved
 * @author			Mohamed Roshan <roshan@ekwa.com>
 *
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Manually Developed CI View
 *
 * This is to view access log report wb
 *
 * @package			AIR\views
 * @version			2.7
 * @uses
 * @see
 * @copyright		2015, BizyCorp Internal Systems Development
 * @license			private, All rights reserved
 * @author			Mohamed Roshan <roshan@ekwa.com>
 * @created			Jul 18, 2014
 * @modified
 * @modification
 *
*/
class Accesslogreport_wb{
	// Just a dummy clas for phpdoc to catch the doc header.
}

$user_id = '';
$user_name = '';
$user_type = '';
if(isset($_SESSION['user_name'])){
	$user_id = $_SESSION['user_id'];
	$user_name = $_SESSION['user_name'];
	$user_type = $_SESSION['user_type'];
}
elseif(isset($_COOKIE['user_name'])){
	$user_id = $_COOKIE['user_id'];
	$user_name = $_COOKIE['user_name'];
	$user_type = $_COOKIE['user_type'];
}

/**
 * Common js/css file header file
 */
require_once('js-css-loader.php');

$log_create = new log_object();
$log_create->set_userDetails($user_name, $user_type);
$log_create->set_otherDetails(array('Mode'=>'Access log summary','Data'=>'-'));
$log_create->init();

$startDate 	= $startDate!=''?$startDate:date("Y-m-d",strtotime(date("Y-m-d")."-6 days"));
$endDate 	= $endDate!=''?$endDate:date("Y-m-d");
?>

<style>
body{ margin:0;}
</style>
<div id="divBench" style="width:100%;height:100%;"></div>
<script>

//CORS handling 
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
	switch (event.data){
		case "setSize":
		wb.getWb().setSizes();
		break;
  	}
}
//---------------------------

var wbContainer;
var wb;
var dataGrid;
var rowid;
var downloadFullExcl = null;
var loadDetails = null;

//now initiate work bench and attache it to page layout.
var wb = new workbenchObj();
wb.workBench = document.getElementById("divBench"); //assigning wb container of this page to wb object
wb.url = '<?php echo site_url()?>/accesslog_controller/accessReport/accesslogreport_gridview';
wb.gridreloadurl = '<?php echo site_url()?>/accesslog_controller/get_xml_report/FALSE/0/0/access_log_report_gridfeed/XML/';

//wb.form = myform;
wb.formVar = 'myform';

//can pass properties to manuputate toolbar objects
wb.gridLayoutWidth = 400;
wb.toolBarComponent='grid';
wb.toolBarNotSet=['add','edit','delete'];
wb.toolBarItems=[{id:'downloadFullExcl',type:'button',img:'export_excel.png',imgdis:'download_dis.gif',text:'Download Complete Excel'}];
wb.toolBarVar='gridtoolbar';
wb.defFrmSaveHandling = false; //Save will be handled by callback
wb.toolBarIconpath = '<?php echo base_url('public/img/common/imgs/'),'/';?>';

//form related variables
wb.formBarComponent='form';    
wb.formBarIconpath = '<?php echo base_url('public/img/common/imgs/'),'/';?>'; 
wb.formUrl = '<?php echo site_url()?>/accesslog_controller/accessReport/accesslogdetail_wb';

//following not required for non ajax loading WCD
//wb.formDataUrl = '<?php echo site_url()?>/templates/save';
//wb.formDelUrl = '<?php echo site_url()?>/templates/delete';
wb.formWidth = 800;
wb.searchOnEnter = true;

/*======== Begin callback function list==========*/

var resetButtons = false; //If delete button was pressed 
// callback functions to load in wb 
var doAddForms = function (){
	if (debug) console.log('Add form url:-',wb.formUrl);
  	if (resetButtons) resetSaveBtn();
	myLayout = wb.getFormLayout();
  	myLayout.attachURL(wb.formUrl+'/add'); 
	return true;
};//End of doAddForms

var doSaveForm = function (){
	var myLayout = wb.getFormLayout();
	ifr = myLayout.getFrame();
	var formstatus = ifr.contentWindow.submitForm();
   	return formstatus
};//End of doSaveForm

var doResetForm = function (){
	var returnVal = false; //set returnstatu WCD
  	var myLayout = wb.getFormLayout();
	if (_isIE) {
    	myLayout.getFrame().contentWindow.document.getElementById("departmentForm").reset();
    } else {
    	myLayout.getFrame().contentDocument.getElementById("departmentForm").reset();
    }
	return returnVal;    
};//End of doResetForm 

var doEditForm = function () {
	var row_id = dataGrid.getSelectedRowId();
	if (debug) console.log('Edit grid row id : :-',row_id);
  	
 	if (resetButtons) resetSaveBtn();
	
	var url = wb.formUrl + '/edit/' + row_id;
	if (debug) console.log('Edit form URL :-',url);
	
	var myLayout = wb.getFormLayout();
  	//myLayout.attachURL(url,true); 
	myLayout.attachURL(url); 
	
	return true;		
};//end of doEditForm

var doDeleteForm = function (){ 
	
	var row_id = dataGrid.getSelectedRowId();
  	if (debug) console.log('Delete grid row id : :-',row_id);	
  	//if buttons were changed by view
  	if (resetButtons) resetSaveBtn();
  	
	var myForm = wb.getFormToolbar();
	formBar =myForm.toolbar;
	formBar.setItemImage('save','cancel.png');
  	formBar.setItemText('save','Delete');	
  	formBar.hideItem('reset');	
  	resetButtons = true;
	
	var url = wb.formUrl+'/delete/' + row_id;
	if (debug) console.log('Delete URL :-',url);
	var myLayout = wb.getFormLayout();
	myLayout.attachURL(url);
	
	return true; 
};//End of doDeleteTMForm

/* Function used to reset the save button properties changed by 'doDeleteForm'
** and 'doFormView' functions. 
*/
function resetSaveBtn(){
  	var myForm = wb.getFormToolbar();
	formBar =myForm.toolbar;
	formBar.setItemImage('save','save.gif');
  	formBar.setItemText('save','Save');
  	formBar.showItem('save');
  	formBar.showItem('reset');
  	resetButtons = false ;
} // End resetSaveBtn

function doGridExelDownload() {
	dataGrid.toExcel('http://www.bizydads.com/frameworks/dhtmlx/dhtmlxGrid/codebase/grid-excel-php/generate.php');
};//end of doFormView

var gridreloadurl_tem = wb.gridreloadurl;
var url_tem = wb.url;
function doStartDate() {
	if(dhtmlToggleState){
		dhtmlToggleState = !dhtmlToggleState;
		wb.toolBarVar.setItemImage('dhtmlSearch', 'find.png');
	}
	
	calendar1 = document.getElementById('startDate').value;
	calendar2 = document.getElementById('endDate').value;
	
	qvar = calendar1+'/'+calendar2;
	
	wb.gridreloadurl = gridreloadurl_tem+qvar;
	wb.url = url_tem+qvar;
	dataGrid.clearAndLoad(wb.gridreloadurl);
	
	wb.workBench.progressOn();
};//end of doFormView

function doEndDate() {
	
};//end of doFormView

/*========End of callback function list==========*/

/*
 * following callback function variables must be declared befor passing to wb. 
 * otherwise it will return undifiend error 
 */
 
wb.setGridAddCallback(doAddForms);
wb.setFormSaveCallback(doSaveForm);
wb.setGridEditCallback(doEditForm);
wb.setGridDelCallback(doDeleteForm);
wb.setFormResetCallback(doResetForm);  

//search is active in toolbar
/*wb.searchactive = 'enable';
var mySearchColumns = [  
   {type:"filler", name:'departments/deptid', id:'departments/deptid', placeholder: '',size: '6',index: 1 },
   {type:"text", name: 'departments/name', id: 'departments/name', placeholder: '',size: '23',index: 2 },
   {type:"text", name: 'departments/description', id: 'departments/description', placeholder: '',size: '40',index: 3 },
   {type:"filler", name:'departments/manager', id:'departments/manager', placeholder: '',size: '',index: 4 },
   {type:"filler", name:'manager_full_name', id:'manager_full_name', placeholder: '',size: '23',index: 5 }
];
wb.searchColumns=mySearchColumns;*/
//after setting up all wb properties finallay initiat wb.
wb.init();
wb.toolBarVar.hideItem('search');

var myForm = wb.getFormToolbar();
formBar =myForm.toolbar;
formBar.hideItem('reset');
formBar.hideItem('save');

//Add custom 'For View' button  event handler
//now attache events for new buttons
if (!downloadFullExcl) {
	downloadFullExcl = wb.toolBarVar.attachEvent('onClick', function(buttonid){
  	if(buttonid=='downloadFullExcl') {
    	doGridExelDownload();
    	//wb.workBench.progressOn();
	}   
  });
}
if (debug) console.log('event attache id :-',downloadFullExcl);

wb.toolBarVar.addButton('dhtmlSearch',19,'Search','find.png','find_dis.png');

var dhtmlToggleState = false;
dhtmlSearch = wb.toolBarVar.attachEvent('onClick', function(buttonid){
	if(buttonid=='dhtmlSearch'){
		dhtmlToggleState = !dhtmlToggleState;
		if(dhtmlToggleState){
			dataGrid.detachHeader(1);
			dataGrid.attachHeader(',#text_filter,#text_filter,#text_filter,#text_filter,#text_filter');
			dataGrid.setSizes();
			wb.toolBarVar.setItemImage('dhtmlSearch', 'find_dis.png');
		}
		else{
			dataGrid.detachHeader(1);
			dataGrid.clearAndLoad(wb.gridreloadurl);
			wb.toolBarVar.setItemImage('dhtmlSearch', 'find.png');
			
			var d = new Date();
			ed = d.getFullYear()+'-'+pad(d.getMonth()+1, 2)+'-'+pad(d.getDate(), 2);

			var sd_tem = new Date();
			sd_tem.setDate(d.getDate()-6);
			sd = sd_tem.getFullYear()+'-'+pad(sd_tem.getMonth()+1, 2)+'-'+pad(sd_tem.getDate(), 2);
			
			document.getElementById('startDate').value = sd;
			document.getElementById('endDate').value = ed;
			
			myCalendar1.setSensitiveRange(null,curruntDate);
			myCalendar2.setSensitiveRange(document.getElementById('startDate').value,curruntDate);
			doStartDate();
		}
	}
});
function pad(n, width, z) {
	z = z || '0';
	n = n + '';
	return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
dhtmlRefresh = wb.toolBarVar.attachEvent('onClick', function(buttonid){
	if(buttonid=='refresh'){
		dhtmlToggleState = false;
		wb.toolBarVar.setItemImage('dhtmlSearch', 'find.png');
	}
});

wb.toolBarVar.addText('startDateText', 2, 'Start Date : ');
wb.toolBarVar.addInput('startDate', 3, '<?php echo $startDate;?>', 70);
wb.toolBarVar.addText('endDateText', 4, 'End Date : ');
wb.toolBarVar.addInput('endDate', 5, '<?php echo $endDate;?>', 70);

//gen button
wb.toolBarVar.addButton('generate',6,'Generate Report','generate.png','generate_dis.png');
wb.toolBarVar.addSeparator('sep', 7);
wb.toolBarVar.cont.getElementsByTagName("input")[0].setAttribute("id","startDate");
wb.toolBarVar.cont.getElementsByTagName("input")[0].setAttribute("readonly","readonly");
wb.toolBarVar.cont.getElementsByTagName("input")[1].setAttribute("id","endDate");
wb.toolBarVar.cont.getElementsByTagName("input")[1].setAttribute("readonly","readonly");

myCalendar1 = new dhtmlXCalendarObject('startDate');
myCalendar2 = new dhtmlXCalendarObject('endDate');
myCalendar1.hideTime();myCalendar2.hideTime();

var dateForCal = new Date();
var curruntDate = dateForCal.getFullYear()+'-'+(dateForCal.getMonth()+1)+'-'+dateForCal.getDate();
myCalendar1.setSensitiveRange(null,curruntDate);
myCalendar2.setSensitiveRange(document.getElementById('startDate').value,curruntDate);

//gen button and calender functions
wb.toolBarVar.attachEvent('onClick', function(buttonid){
	if(buttonid=='generate'){ doStartDate(); }
});
var myEvent = myCalendar1.attachEvent("onClick", function (){
    myCalendar2.setSensitiveRange(myCalendar1.getDate(),curruntDate);
});
var myEvent = myCalendar2.attachEvent("onClick", function (){
    myCalendar1.setSensitiveRange(null,myCalendar2.getDate());
});
</script>

<!-- FOR HELP TOUR -->
<link rel="stylesheet" href="http://www.bizydads.com/frameworks/zurb-joyride-1d1cb89/joyride-2.0.3.css">
<script type="text/javascript" src="http://www.bizydads.com/frameworks/zurb-joyride-1d1cb89/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://www.bizydads.com/frameworks/zurb-joyride-1d1cb89/jquery.cookie.js"></script>
<script type="text/javascript" src="http://www.bizydads.com/frameworks/zurb-joyride-1d1cb89/modernizr.mq.js"></script>
<script type="text/javascript" src="http://www.bizydads.com/frameworks/zurb-joyride-1d1cb89/jquery.joyride-2.0.3.js"></script>
<script>
/* Page Help */
wb.toolBarVar.addButton('helpFile',20,'Page Help','help_on.gif','help.gif');//Page Help button
var toolBar = wb.toolBarVar.cont.getElementsByClassName("dhx_toolbar_btn def");//getting toolbar items by class names
//adding ids to toolbar items
for(x=0; x<toolBar.length;x++){
	var toolBarItmeIdName = 'toolTip_'+x;
	toolBar[x].setAttribute("id",toolBarItmeIdName);
}
//Add custom 'Page Help' button  event handler
//now attache events for new buttons
var help = wb.toolBarVar.attachEvent('onClick', function(buttonid){
	var cell_tip = document.getElementsByClassName(" ev_dhx_skyblue");//getting gird rows but class name
	cell_tip[0].setAttribute("id",'gridRowTip_1');//adding id to 1st grid row
  	if(buttonid=='helpFile') {$('#joyRideTipContent_1').joyride({});}//load joyride
});
</script>
<!-- Tip Content -->
<ol id="joyRideTipContent_1" style='display:none;'>
	<li data-id="" data-button="Start the tour" data-options="tipLocation:top"><h2>Introduction</h2><p>Guidelines for Access log summary report.</p></li>
	<li data-id="toolTip_0" data-button="Next" data-options="tipLocation:top"><h2>Refresh</h2><p>Just reloads the grid to the original state.</p></li>
	<li data-id="toolTip_1" data-button="Next" data-options="tipLocation:top"><h2>Start Date</h2><p>Click and select range starting date.</p></li>
	<li data-id="toolTip_2" data-button="Next" data-options="tipLocation:top"><h2>End Date</h2><p>Click and select range ending date.</p></li>
	<li data-id="toolTip_3" data-button="Next" data-options="tipLocation:top"><h2>Generate Report</h2><p>Click to generate report with selected date range.</p></li>
	<li data-id="toolTip_4" data-button="Next" data-options="tipLocation:top"><h2>Download EXCEL</h2><p>Click to export details to excel file.</p></li>
	<li data-id="toolTip_6" data-button="Next" data-options="tipLocation:top"><h2>Search</h2><p>Click to get Search box row.</p></li>
	<li data-id="gridRowTip_1" data-button="Close" data-options="tipLocation:top"><h2>Get More Details</h2><p>"Double Click" on each row to see a detailed view.</p></li>
	
</ol>
<!-- End of tip contents -->