<?php
$search_query       = str_replace( 'a.branch_id', 'a.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'] ) ;
    }
}


// staff department
$staffdeparments        = [] ;
$select_staffdeparments = $mysqli->query( "SELECT a.staff_id, a.department_id FROM staff_department
 a
                                           WHERE a.deleted_at IS NULL
                                           GROUP BY a.staff_id" ) ;
if ( $select_staffdeparments->num_rows > 0 ){
	while ( $row_staffdeparment = $select_staffdeparments->fetch_assoc() ){
		$staffdeparments[$row_staffdeparment['staff_id']] = $row_staffdeparment['department_id'] ;
	}
}


// reject report
$reject_lists     = [] ;
$select_training  = $mysqli->query( "SELECT 
										COUNT(a.staff_id) as total, a.staff_id 
									 FROM staff_training 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.staff_id" ) ;


if ( $select_training->num_rows > 0 ){
    while ( $row_training = $select_training->fetch_assoc() ){
    	$get_department   = $staffdeparments[$row_training['staff_id']] ;

    	$reject_lists[] = [
    		'label' => ( $get_department == '0' ? 'Cross Department' : $department_lists[$get_department] ),
    		'y'     => floatval( $row_training['total'] )
    	] ;
    }
}



// complete report
$complete_lists   = [] ;
$select_training      = $mysqli->query( "SELECT 
											COUNT(a.staff_id) as total, a.staff_id 
									     FROM staff_training a
                                         WHERE 
                                     		a.deleted_at IS NULL AND a.status IN ( 'confirmed', 'rated' ) AND a.updated_at LIKE '%" . $date_time . "%'
                                     		" . $search_query . "
                                         GROUP BY a.staff_id" ) ;
if ( $select_training->num_rows > 0 ){
    while ( $row_training = $select_training->fetch_assoc() ){
    	$get_department   = $staffdeparments[$row_training['staff_id']] ;

    	$complete_lists[] = [
    		'label' => ( $get_department == '0' ? 'Cross Department' : $department_lists[$get_department] ),
    		'y'     => floatval( $row_training['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() ;


	// 
	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="chartContainer" class="chart_div"></div>
		</div>
	</div>
</div>