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


// reject report
$reject_lists      = [] ;
$select_visitor    = $mysqli->query( "SELECT 
										COUNT(a.category) as total,
										a.category
									  FROM visitor a
                                      WHERE a.deleted_at IS NULL AND a.updated_at LIKE '%" . $date_time . "%'
                                      		AND a.status IN ( 'rejected', 'tested-rejected' )
                                      		" . $search_query . "
                                      GROUP BY a.category" ) ;

if ( $select_visitor->num_rows > 0 ){
    while ( $row_visitor = $select_visitor->fetch_assoc() ){
    	$category_id 		= $row_visitor['category'] ;
    	$category_id 		= dataFilter( $category_id != '' ? $category_id : 'Unknown' ) ;

    	$reject_lists[] = [
    		'label' => $category_id,
    		'y'     => floatval( $row_visitor['total'] )
    	] ;
    }
}


// complete report
$complete_lists    = [] ;
$select_visitor    = $mysqli->query( "SELECT 
										COUNT(a.category) as total,
										a.category
									  FROM visitor a
                                      WHERE a.deleted_at IS NULL AND a.updated_at LIKE '%" . $date_time . "%'
                                      		AND a.status IN ( 'approved', 'tested-approved', 'visited' )
                                      		" . $search_query . "
                                      GROUP BY a.category" ) ;
if ( $select_visitor->num_rows > 0 ){
    while ( $row_visitor = $select_visitor->fetch_assoc() ){
    	$category_id 		= $row_visitor['category'] ;
    	$category_id 		= dataFilter( $category_id != '' ? $category_id : 'Unknown' ) ;

    	$complete_lists[] = [
    		'label' => $category_id,
    		'y'     => floatval( $row_visitor['total'] )
    	] ;
    }
}
?>

<script>
window.onload = function() {

	// reject chart
	var rejectchart = new CanvasJS.Chart("rejectContainer", {
		animationEnabled: true,
		title: {
			text: "Total of Rejected Visitor by Category"
		},
		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 Visitor by Category"
		},
		data: [{
			type: "pie",
			startAngle: 240,
			yValueFormatString: "##0",
			indexLabel: "{label} {y}",
			dataPoints: <?= json_encode($complete_lists) ?>
		}]
	});
	completechart.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>