<?php
$page_title = 'Test Scan AWB';
require_once('includes/load.php');
page_require_level(3);

// Start time
$start_time = microtime(true);

// Set default timezone
date_default_timezone_set('Asia/Jakarta'); // Sesuaikan dengan zona waktu Anda

// Check if scan_in is already filled for the AWB
function is_scan_completed($awb_resi) {
    global $db;
    $result = find_by_resi($awb_resi);
    return !empty($result['scan_in']);
}

// Handle form submission
if (isset($_POST['scan'])) {
    $awb_resi = $_POST['awb_resi'];
    
    // Check if AWB exists in the database
    $awb = find_by_resi($awb_resi); // Implement find_by_resi function to check if AWB exists
    if ($awb) {
        // Check if scan_in is already filled
        if (is_scan_completed($awb_resi)) {
            $session->msg('w', "AWB DOUBLE SCAN!!");
        } else {
            $scan_in = date("Y-m-d H:i:s"); // Current date and time
            
            // Update scan_in in the database
            $sql = "UPDATE awb SET scan_in = '{$scan_in}' WHERE resi = '{$awb_resi}'";
            if ($db->query($sql)) {
                $session->msg('s', "AWB BERHASIL SCAN !!");
            } else {
                $session->msg('d', "Failed to update scan status for AWB barcode.");
            }
        }
        redirect('test_scan_in.php', false);
    } else {
        $session->msg('w', "AWB Tidak Ada !!");
        redirect('test_scan_in.php', false);
    }
}

// End time
$end_time = microtime(true);

// Calculate execution time
$execution_time = $end_time - $start_time;
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-8">
        <!-- Scan AWB Form Section -->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Scan AWB Barcode</h3>
            </div>
            <div class="panel-body">
                <!-- Scan AWB Form -->
                <form id="scanForm" method="post" action="test_scan_in.php" class="form-horizontal">
                    <div class="form-group">
                        <label for="awb_resi" class="col-sm-3 control-label">AWB Resi</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control input-lg" name="awb_resi" id="awb_resi" placeholder="AWB Resi" autofocus>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-9">
                            <button type="submit" name="scan" id="scanBtn" class="btn btn-primary btn-lg">Scan</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <!-- Execution Time Section -->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Execution Time</h3>
            </div>
            <div class="panel-body">
                <p>Execution time: <?php echo $execution_time; ?> seconds</p>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <!-- Display Messages -->
        <div class="large-message">
            <?php echo display_msg($msg); ?>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>

<style>
    /* Remove any animation-related properties */
    * {
        animation: none !important;
        transition: none !important;
    }

    /* Ensure default styles for form elements */
    .form-control.input-lg {
        font-size: 18px;
        padding: 10px;
    }

    .form-horizontal .control-label {
        font-size: 18px;
    }

    .form-horizontal .btn.btn-lg {
        font-size: 18px;
        padding: 10px 20px;
    }

    .large-message {
        font-size: 50px; /* Ukuran font pesan diperbesar */
        font-weight: bold;
        color: #ff0000; /* Warna merah untuk pesan error */
        text-align: center;
        margin-top: 20px;
    }
</style>
