<?php
session_start(); 
include_once("../../../../../lib/konfig.php"); 
include_once("../../../../../lib/function.php"); 

$range = array();
$dtAwal = new DateTime($_POST['dtAwal']);
$dtAkhir = new DateTime($_POST['dtAkhir']);
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($dtAwal, $interval, $dtAkhir);

foreach ($period as $dt) {
    $range[] = $dt->format("F Y");
}
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$MySQLi2 = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$query="SELECT * 
        FROM employee 
        WHERE Level='".$_POST['level']."' AND 
              stsAktif=1 
        "; 
$legend = array();
$data_line = array(); // Inisialisasi array untuk menyimpan data_line
$where="";
$result = $MySQLi->query($query);
while ($row = $result->fetch_array()) 
{
    $data = array(); 
    switch($_POST['level'])
    {
        case 1: $strJabatan="Direksi"; 
                $select="AVG(IF(strMailDIR=strMailMGR, if(strMailDIR=strMailSPV, if(strMailDIR=usrCreate, 0, TIMESTAMPDIFF(DAY, dtCreate, dtSubmitDIR)), TIMESTAMPDIFF(DAY, dtSubmitSPV, dtSubmitDIR)), TIMESTAMPDIFF(DAY, dtSubmitMGR, dtSubmitDIR))) AS iJumlahHari";
                $where="strMailDIR='".$row['Email']."' AND dtSubmitDIR <> '1000-01-01' AND (dtCreate BETWEEN '".$_POST['dtAwal']." 00:00:00' AND '".$_POST['dtAkhir']." 23:59:59')"; 
                $groupby="GROUP BY strMailDIR, MONTH(dtCreate), YEAR(dtCreate)";
                break; 
        case 2: $strJabatan="Manager"; 
                $select="AVG(IF(strMailMGR=strMailSPV, if(strMailMGR=usrCreate, 0, TIMESTAMPDIFF(DAY, dtCreate, dtSubmitMGR)), TIMESTAMPDIFF(DAY, dtSubmitSPV, dtSubmitMGR))) AS iJumlahHari";
                $where="strMailMGR='".$row['Email']."' AND dtSubmitMGR <> '1000-01-01' AND (dtCreate BETWEEN '".$_POST['dtAwal']." 00:00:00' AND '".$_POST['dtAkhir']." 23:59:59')"; 
                $groupby="GROUP BY strMailMGR, MONTH(dtCreate), YEAR(dtCreate)";
                break; 
        case 3: $strJabatan="Supervisor"; 
                $select="AVG(IF(strMailSPV=usrCreate, 0, TIMESTAMPDIFF(DAY, dtCreate, dtSubmitSPV))) AS iJumlahHari";
                $where="strMailSPV='".$row['Email']."' AND dtSubmitSPV <> '1000-01-01' AND (dtCreate BETWEEN '".$_POST['dtAwal']." 00:00:00' AND '".$_POST['dtAkhir']." 23:59:59')"; 
                $groupby="GROUP BY strMailSPV, MONTH(dtCreate), YEAR(dtCreate)";
                break; 
    }
    $legend[] = $row['Email']; 
    $query2="SELECT ".$select."
            FROM tpc_h_lpbj 
            WHERE stsAktif=1 AND ".$where." ".$groupby; 
    $result2=$MySQLi2->query($query2);
    $i=0;
    if($result->num_rows>0)
    {
        while($row2=$result2->fetch_array())
        {
            $data[$i]=floatval($row2['iJumlahHari']);
            $i++;
        }    
    }
    $data_line[$row['Email']] = $data;
}

//echo json_encode($data_line);
        
?>
<figure class="highcharts-figure">
    <div id="container"></div>
    <p class="highcharts-description">Berikut adalah gambar daripada kecepatan rata rata review untuk jabatan : <?php echo $strJabatan;?></p>
</figure>

<script src="assets/js/highcharts.js"></script>
<script src="assets/js/series-label.js"></script>
<script src="assets/js/exporting.js"></script>
<script src="assets/js/export-data.js"></script>
<script src="assets/js/accessibility.js"></script>
<script>
    Highcharts.chart('container', 
    {
        title: 
        {
            text: 'Rata rata kecepatan persetujuan',
            align: 'left'
        },
        subtitle: 
        {
            text: 'Jabatan By <?php echo $strJabatan;?>',
            align: 'left'
        },
        yAxis: {title: {text: 'Jumlah Hari'}},
        xAxis: {categories: <?php print_r(json_encode($range));?>},
        legend: 
        {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },
        plotOptions: {series: {label: {connectorAllowed: false}}},
        series: [
            <?php foreach ($data_line as $name => $data): ?>
                {
                    name: '<?php echo $name; ?>',
                    data: <?php echo json_encode($data); ?>
                },
            <?php endforeach; ?>],

        responsive: 
        {
            rules: 
            [{
                condition: 
                {
                    maxWidth: 500
                },
                chartOptions: 
                {
                    legend: 
                    {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        }

    });
</script>