<?php
require_once('includes/load.php');
page_require_level(10);

// Ambil tanggal saat ini  
$current_date = date('Y-m-d');

$redisKey = 'scan_results_cache';
$cachedData = redisGet($redisKey);

if (!$cachedData) {

    // Mengambil total jumlah scan in untuk tanggal saat ini  
    $sql_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in FROM awb WHERE DATE(scan_in) = '{$current_date}'";
    $result_scan_in = $db->query($sql_scan_in);
    $total_scan_in = 0;
    if ($row = $result_scan_in->fetch_assoc()) {
        $total_scan_in = $row['total_scan_in'];
    }

    // Mengambil total jumlah scan status untuk tanggal saat ini  
    $sql_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status FROM awb WHERE DATE(scan_status) = '{$current_date}'";
    $result_scan_status = $db->query($sql_scan_status);
    $total_scan_status = 0;
    if ($row = $result_scan_status->fetch_assoc()) {
        $total_scan_status = $row['total_scan_status'];
    }

    // Mengambil total jumlah scan in dan scan status untuk Surabaya (loc is NULL)  
    $sql_surabaya_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in_surabaya FROM awb WHERE loc IS NULL AND DATE(scan_in) = '{$current_date}'";
    $result_surabaya_scan_in = $db->query($sql_surabaya_scan_in);
    $total_scan_in_surabaya = 0;
    if ($row = $result_surabaya_scan_in->fetch_assoc()) {
        $total_scan_in_surabaya = $row['total_scan_in_surabaya'];
    }

    $sql_surabaya_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status_surabaya FROM awb WHERE loc IS NULL AND DATE(scan_status) = '{$current_date}'";
    $result_surabaya_scan_status = $db->query($sql_surabaya_scan_status);
    $total_scan_status_surabaya = 0;
    if ($row = $result_surabaya_scan_status->fetch_assoc()) {
        $total_scan_status_surabaya = $row['total_scan_status_surabaya'];
    }

    // Mengambil total jumlah scan in dan scan status untuk Jakarta (loc = 'jakarta')  
    $sql_jakarta_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in_jakarta FROM awb WHERE loc = 'jakarta' AND DATE(scan_in) = '{$current_date}'";
    $result_jakarta_scan_in = $db->query($sql_jakarta_scan_in);
    $total_scan_in_jakarta = 0;
    if ($row = $result_jakarta_scan_in->fetch_assoc()) {
        $total_scan_in_jakarta = $row['total_scan_in_jakarta'];
    }

    $sql_jakarta_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status_jakarta FROM awb WHERE loc = 'jakarta' AND DATE(scan_status) = '{$current_date}'";
    $result_jakarta_scan_status = $db->query($sql_jakarta_scan_status);
    $total_scan_status_jakarta = 0;
    if ($row = $result_jakarta_scan_status->fetch_assoc()) {
        $total_scan_status_jakarta = $row['total_scan_status_jakarta'];
    }

    // Mengambil total jumlah scan in dan scan status untuk Makassar (loc = 'makassar')  
    $sql_makassar_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in_makassar FROM awb WHERE loc = 'makassar' AND DATE(scan_in) = '{$current_date}'";
    $result_makassar_scan_in = $db->query($sql_makassar_scan_in);
    $total_scan_in_makassar = 0;
    if ($row = $result_makassar_scan_in->fetch_assoc()) {
        $total_scan_in_makassar = $row['total_scan_in_makassar'];
    }

    $sql_makassar_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status_makassar FROM awb WHERE loc = 'makassar' AND DATE(scan_status) = '{$current_date}'";
    $result_makassar_scan_status = $db->query($sql_makassar_scan_status);
    $total_scan_status_makassar = 0;
    if ($row = $result_makassar_scan_status->fetch_assoc()) {
        $total_scan_status_makassar = $row['total_scan_status_makassar'];
    }

    // Mengambil total jumlah scan in dan scan status untuk Bali (loc = 'bali')  
    $sql_bali_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in_bali FROM awb WHERE loc = 'bali' AND DATE(scan_in) = '{$current_date}'";
    $result_bali_scan_in = $db->query($sql_bali_scan_in);
    $total_scan_in_bali = 0;
    if ($row = $result_bali_scan_in->fetch_assoc()) {
        $total_scan_in_bali = $row['total_scan_in_bali'];
    }

    $sql_bali_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status_bali FROM awb WHERE loc = 'bali' AND DATE(scan_status) = '{$current_date}'";
    $result_bali_scan_status = $db->query($sql_bali_scan_status);
    $total_scan_status_bali = 0;
    if ($row = $result_bali_scan_status->fetch_assoc()) {
        $total_scan_status_bali = $row['total_scan_status_bali'];
    }

    // Mengambil total jumlah scan in dan scan status untuk Semarang (loc = 'semarang')  
    $sql_semarang_scan_in = "SELECT COUNT(DISTINCT resi) as total_scan_in_semarang FROM awb WHERE loc = 'semarang' AND DATE(scan_in) = '{$current_date}'";
    $result_semarang_scan_in = $db->query($sql_semarang_scan_in);
    $total_scan_in_semarang = 0;
    if ($row = $result_semarang_scan_in->fetch_assoc()) {
        $total_scan_in_semarang = $row['total_scan_in_semarang'];
    }

    $sql_semarang_scan_status = "SELECT COUNT(DISTINCT resi) as total_scan_status_semarang FROM awb WHERE loc = 'semarang' AND DATE(scan_status) = '{$current_date}'";
    $result_semarang_scan_status = $db->query($sql_semarang_scan_status);
    $total_scan_status_semarang = 0;
    if ($row = $result_semarang_scan_status->fetch_assoc()) {
        $total_scan_status_semarang = $row['total_scan_status_semarang'];
    }

    // Menghitung persentase progres  
    $progress_percentage = ($total_scan_in > 0) ? ($total_scan_status / $total_scan_in) * 100 : 0;
    $progress_percentage_surabaya = ($total_scan_in_surabaya > 0) ? ($total_scan_status_surabaya / $total_scan_in_surabaya) * 100 : 0;
    $progress_percentage_jakarta = ($total_scan_in_jakarta > 0) ? ($total_scan_status_jakarta / $total_scan_in_jakarta) * 100 : 0;
    $progress_percentage_makassar = ($total_scan_in_makassar > 0) ? ($total_scan_status_makassar / $total_scan_in_makassar) * 100 : 0;
    $progress_percentage_bali = ($total_scan_in_bali > 0) ? ($total_scan_status_bali / $total_scan_in_bali) * 100 : 0;
    $progress_percentage_semarang = ($total_scan_in_semarang > 0) ? ($total_scan_status_semarang / $total_scan_in_semarang) * 100 : 0;

    // Menyiapkan data untuk dikembalikan dalam format JSON  
    $response = array(
        'total_scan_in' => $total_scan_in,
        'total_scan_status' => $total_scan_status,
        'total_scan_in_surabaya' => $total_scan_in_surabaya,
        'total_scan_status_surabaya' => $total_scan_status_surabaya,
        'total_scan_in_jakarta' => $total_scan_in_jakarta,
        'total_scan_status_jakarta' => $total_scan_status_jakarta,
        'total_scan_in_makassar' => $total_scan_in_makassar,
        'total_scan_status_makassar' => $total_scan_status_makassar,
        'total_scan_in_bali' => $total_scan_in_bali,
        'total_scan_status_bali' => $total_scan_status_bali,
        'total_scan_in_semarang' => $total_scan_in_semarang,
        'total_scan_status_semarang' => $total_scan_status_semarang,
        'progress_percentage' => round($progress_percentage, 2),
        'progress_percentage_surabaya' => round($progress_percentage_surabaya, 2),
        'progress_percentage_jakarta' => round($progress_percentage_jakarta, 2),
        'progress_percentage_makassar' => round($progress_percentage_makassar, 2),
        'progress_percentage_bali' => round($progress_percentage_bali, 2),
        'progress_percentage_semarang' => round($progress_percentage_semarang, 2)
    );
    redisSet($redisKey, $response, 300);
    header('Content-Type: application/json');
    echo json_encode($response);
} else {
    header('Content-Type: application/json');
    echo json_encode($cachedData);
}
