<?php
$search_query       = str_replace( 'a.branch_id', 'created_branch_id', $user_branch_permission_sql_a ) ;

$department_lists   = [] ;
$select_departments = $mysqli->query( "SELECT a.department_id, b.department_desc FROM setting_department a
                                       LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
                                       WHERE a.deleted_at IS NULL AND b.lang = 'en'" ) ;
if ( $select_departments->num_rows > 0 ){
	while ( $row_department = $select_departments->fetch_assoc() ){
    	$department_lists[$row_department['department_id']]  = dataFilter( $row_department['department_desc'] ) ;
    }
}


// reject report
$reject_lists     = [] ;
$select_task      = $mysqli->query( "SELECT COUNT(a.department_id) as total, a.department_id FROM task a
                                     WHERE a.deleted_at IS NULL AND a.status IN ( 'rejected', 'cancelled' ) AND a.updated_at LIKE '%" . $date_time . "%' " . $search_query . "
                                     GROUP BY a.department_id" ) ;
if ( $select_task->num_rows > 0 ){
    while ( $row_task = $select_task->fetch_assoc() ){
    	$reject_lists[] = [
    		'label' => ( $row_task['department_id'] == '0' ? 'Cross Department' : $department_lists[$row_task['department_id']] ),
    		'y'     => floatval( $row_task['total'] )
    	] ;
    }
}

// complete report by difficulty
$difficulty_departments = [] ;
$difficulty_lists       = [] ;
$difficulty_alllists    = [] ;
$select_task            = $mysqli->query( "SELECT COUNT(a.difficulty) as total, a.department_id, a.difficulty FROM task a
                                           WHERE a.deleted_at IS NULL AND a.status IN ( 'approved' ) AND a.confirmed_at LIKE '%" . $date_time . "%' " . $search_query . "
                                           GROUP BY a.department_id, a.difficulty" ) ;
if ( $select_task->num_rows > 0 ){
    while ( $row_task  = $select_task->fetch_assoc() ){
    	$difficulty_lists[$row_task['difficulty']][$row_task['department_id']]= $row_task['total'] ;
    	$difficulty_departments[$row_task['department_id']] = $row_task['department_id'] ;
    }
}

foreach ( $difficulty_lists as $k_lists => $v_lists ){

	$temps 				= [] ;
	foreach ( $difficulty_departments as $k_department => $v_department ){
		$temps[] = [
    		'label' => ( $v_department == '0' ? 'Cross Department' : $department_lists[$v_department] ),
    		'y'     => floatval( checkExists( $v_lists[$v_department], '0' ) )
    	] ;
	}


	$difficulty_alllists[] = [
		'type' 			=> 'bar',
		'showInLegend' 	=> true,
		'name' 			=> ucwords( $k_lists ),
		'dataPoints' 	=> $temps
	] ;
}



// complete report
$complete_lists   = [] ;
$select_task      = $mysqli->query( "SELECT COUNT(a.department_id) as total, a.department_id FROM task a
                                     WHERE a.deleted_at IS NULL AND a.status IN ( 'approved' ) AND a.confirmed_at LIKE '%" . $date_time . "%' " . $search_query . "
                                     GROUP BY a.department_id" ) ;
if ( $select_task->num_rows > 0 ){
    while ( $row_task = $select_task->fetch_assoc() ){
    	$complete_lists[] = [
    		'label' => ( $row_task['department_id'] == '0' ? 'Cross Department' : $department_lists[$row_task['department_id']] ),
    		'y'     => floatval( $row_task['total'] )
    	] ;
    }
}
?>

<script>
window.onload = function() {

	// reject chart
	var rejectchart = new CanvasJS.Chart("rejectContainer", {
		animationEnabled: true,
		title: {
			text: "Total of Reject by Department"
		},
		data: [{
			type: "pie",
			startAngle: 240,
			yValueFormatString: "##0",
			indexLabel: "{label} {y}",
			dataPoints: <?= json_encode($reject_lists) ?>
		}]
	});
	rejectchart.render() ;






	// complete chart
	var completechart = new CanvasJS.Chart("completeContainer", {
		animationEnabled: true,
		title:{
			text: "Total of Completed by Department & Difficulty"
		},
		axisY: {
			title: "Difficulty",
			includeZero: true
		},
		legend: {
			cursor:"pointer",
			itemclick : toggleDataSeries
		},
		toolTip: {
			shared: true,
			content: toolTipFormatter
		},
		data: <?= json_encode($difficulty_alllists) ?>
	});
	completechart.render();

	function toolTipFormatter(e) {
		var str = "";
		var total = 0 ;
		var str3;
		var str2 ;
		for (var i = 0; i < e.entries.length; i++){
			var str1 = "<span style= \"color:"+e.entries[i].dataSeries.color + "\">" + e.entries[i].dataSeries.name + "</span>: <strong>"+  e.entries[i].dataPoint.y + "</strong> <br/>" ;
			total = e.entries[i].dataPoint.y + total;
			str = str.concat(str1);
		}
		str2 = "<strong>" + e.entries[0].dataPoint.label + "</strong> <br/>";
		str3 = "<span style = \"color:Tomato\">Total: </span><strong>" + total + "</strong><br/>";
		return (str2.concat(str)).concat(str3);
	}

	function toggleDataSeries(e) {
		if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
			e.dataSeries.visible = false;
		}
		else {
			e.dataSeries.visible = true;
		}
		completechart.render();
	}




	// 
	var chart = new CanvasJS.Chart("chartContainer", {
		animationEnabled: true,
		theme: "light2",
		title:{
			text: "Total of Completed by Department"
		},
		axisY: {
			title: "Total"
		},
		data: [{        
			type: "column",  
			showInLegend: true, 
			legendMarkerColor: "grey",
			legendText: "Department",
			dataPoints: <?= json_encode($complete_lists) ?>
		}]
	});
	chart.render();
}
</script>


<div class="dashboard_section">
	<div class="row dashboard_chart">
		<div class="col-sm-6">
			<div id="rejectContainer" class="chart_div"></div>
		</div>
		<div class="col-sm-6">
			<div id="completeContainer" class="chart_div"></div>
		</div>
	</div>
</div>

<div class="dashboard_section">
	<div class="row dashboard_chart">
		<div class="col-sm-12">
			<div id="chartContainer" class="chart_div"></div>
		</div>
	</div>
</div>