<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

mysqli_query($conn, "SET NAMES 'utf8'");
mysqli_query($conn, 'SET CHARACTER SET utf8');

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$usertype = $_SESSION["usertype_" . $dbname];
$origin = $_REQUEST["origin"];
$useraccode = $_SESSION["customer_code"];

$maindb = 'shipment';
$trandb = 'shiptran';

$file = 'shiporiginrep.php';
$keyfld = "vouchno";

$keycond = "";

$zroot = $_SESSION["zdir"];
$zdb = $_SESSION["zdbyear"];
mysqli_select_db($conn, $zdb);

if ($origin != "") {

    $query = "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.name as custname, c.name as statusnm
                FROM `$maindb` a
                LEFT JOIN accmast b ON a.customer = b.accode
                LEFT JOIN status c ON a.status = c.code
                WHERE a.origin='$origin' AND a.customer = '$useraccode' ORDER BY a.vouchdt DESC,a.vouchno ";

    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));

    if (mysqli_num_rows($result) > 0) {

        echo '<div class="row g-3" id="shipmentContainer">';

        $ctr = 0;
        while ($r = mysqli_fetch_assoc($result)) {

            $shipment = "GL-" . $r['vouchno'];
            $statusnm = $r['statusnm'];
            $status = $r['status'];
            $bookingdt = $r['mydt'];
            $supplier = $r['supplier'];
            $description = $r['description'];
            $saildate = format_date($r['saildate']);
            $destination = $r['destination'];
            $estimdate = format_date($r['estimdate']);
            $delvdate = format_date($r['delvdate']);
            $containerno = $r['containerno'];
            $nocart = $r['nocart'];
            $pcs = $r['pcs'];
            $unit = $r['unit'];
            $marka = $r['marka'];
            $weight = $r['weight'];
            $totcbm = $r['totcbm'];

            $colr = "";
            $showstatus = "status-origin";

            // Change color based on status
            if ($status == 5) {
                $showstatus = "status-transit"; // light blue
            } elseif ($status == 7) {
                $showstatus = "status-destination"; // light yellow
            } elseif ($status == 9) {
                $showstatus = "status-delivered"; // light green
            }

            if ($status != 9) {
                $delvdate = "Pending";
            }

            $ctr++;

            ?>

            <!-- Shipment Cards -->

            <div class="col-md-4 shipment-item">
                <div class="shipment-card">
                    <div class="shipment-header">
                        <span class="shipment-title">Shipment <?= $shipment ?></span>
                        <span class="skstatus-badge <?= $showstatus ?>"><?= $statusnm ?></span>
                    </div>
                    <div class="info-row">
                        <span><b class="info-label">Booking &nbsp;</b> <?= $bookingdt ?> </span>
                        <span style="float: right;"><b class="info-label">Container &nbsp;</b> <?= $containerno ?></span>
                    </div>
                    <div class="info-row"><span class="info-label">Supplier</span><span class="info-value"> <?= $supplier ?> </span>
                    </div>
                    <div class="info-row"><span class="info-label">Description</span><span class="info-value"> <?= $description ?>
                        </span>
                    </div>
                    <div class="shipment-stats">
                        <div><strong><?= $nocart ?></strong><small>Cartons</small></div>
                        <div><strong><?= $pcs ?></strong><small><?= $unit ?></small></div>
                        <div><strong><?= $weight ?></strong><small>Weight</small></div>
                        <div><strong><?= $totcbm ?></strong><small>Total CBM</small></div>
                    </div>
                    <!-- <div class="info-row"><span class="info-label">Container</span><span
                            class="info-value">CNT-4567</span></div> -->
                    <div class="info-row"><span class="info-label">Sailing</span><span class="info-value"> <?= $saildate ?> </span>
                    </div>
                    <div class="info-row"><span class="info-label">Destination</span><span class="info-value"> <?= $destination ?>
                        </span>
                    </div>
                    <div class="shipment-footer">
                        <span><i class="bi bi-calendar-event"></i> Estimate: <?= $estimdate ?></span>
                        <span><i class="bi bi-box-seam"></i> Delivery: <?= $delvdate ?></span>
                    </div>
                </div>
            </div>
            <?php
        }

        echo '</div>'; // close shipmentContainer row
        ?>

        <!-- Duplicate more .shipment-item here to test pagination -->

        <br>

        <!-- Pagination -->
        <div class="pagination-controls">
            <button type="button" class="btn btn-sm btn-outline-primary" id="prevBtn">Prev</button>
            <button type="button" class="btn btn-sm btn-outline-primary" id="nextBtn">Next</button>
        </div>

        <br>

       
        <?php
    }
}
?>