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

$formname = 'Shipment Entry';
$maindb = 'shipment';
$trandb = 'shiptran';
$accdb = 'actran';

$printfile = "shipment_pdf.php";
$file = 'shipment.php';
$trandbexist = 1;
$keyfld = "vouchno";
$focusfield = "name";

$source = 'SHP';
$salcode = '14';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

function sendWhatsappMessage($mobileno, $message, $instid, $accstkn)
{
    if ($mobileno == '' || $instid == '' || $accstkn == '')
        return;

    $msg = urlencode($message);
    $logFile = "/tmp/whatsapp_text_" . time() . ".log";

    // Run in background so it doesn't delay page load
    $cmd = "curl -s -X POST " .
        "\"https://web.secureleads.in/api/send?number=$mobileno&type=text&message=$msg&instance_id=$instid&access_token=$accstkn\" " .
        "-H 'Cookie: stackpost_session=m1ip03t51dk5rlgaa5blakqmva57k3nd' " .
        "> $logFile 2>&1 &";

    exec($cmd);
}


if (isset($_POST["submit"])) {

    $a = 1;

    $qvouch = $_POST["vouchno"];
    $voucher = "GC" . $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $customer = $_POST["customer"];
    $description = $_POST["description"];
    $nocart = $_POST["nocart"];
    $supplier = $_POST["supplier"];
    $origin = $_POST["origin"];
    $pcs = $_POST["pcs"];
    $unit = $_POST["unit"];
    $marka = $_POST["marka"];
    $totcbm = $_POST["totcbm"];
    $weight = $_POST["weight"];
    $estimdate = $_POST["estimdate"];
    $containerno = $_POST["containerno"];
    $saildate = $_POST["saildate"];
    $delvdate = $_POST["delvdate"];
    $destination = $_POST["destination"];
    $mobile = $_POST["mobile"];
    $arrvport = $_POST["arrvport"];
    $amount = $_POST["amount"];
    $overflow = $_POST["overflow"];
    $overflowrem = $_POST["overflowrem"];
    $cbmdetail = $_POST["cbmdetail"];

    $party = $customer;
    $count = $_POST["items"];
    $mvouch = $qvouch;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `accmast` WRITE, `$accdb` WRITE ") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `$maindb` where vouchno != '' $keycond LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));
        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $len = $_POST["len_$i"];
            $brea = $_POST["brea_$i"];
            $heig = $_POST["heig_$i"];
            $cartcount = $_POST["cartcount_$i"];
            $cbm = $_POST["cbm_$i"];

            $cnt++;
            $group = $source . $mvouch;
            if (!empty($cbm)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`,  `len`, `brea`, `heig`, `cbm`, `cartcount`)
                                                  VALUES('$mvouch', '$vouchdt', '$cnt', '$len', '$brea', '$heig', '$cbm','$cartcount')") or die(mysqli_error($conn));
            }
        }


        // ✅ Get customer mobile
        $getpty = mysqli_query($conn, "SELECT mobile FROM accmast WHERE accode='$customer'") or die(mysqli_error($conn));
        $fetpty = mysqli_fetch_array($getpty);
        $mobileno = $fetpty['mobile'];

        // ✅ Session data
        $instid = $_SESSION["instid"];
        $accstkn = $_SESSION["accstkn"];

        // ✅ 1. Get old status from DB
        $get_oldsts = mysqli_query($conn, "SELECT status FROM $maindb WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
        $oldsts = mysqli_fetch_assoc($get_oldsts);
        $old_status = $oldsts["status"];

        // ✅ 2. Determine new status
        $status = 1;
        $statusText = "";
        // Get today's date
        $today = date("Y-m-d");

        // use elseif chain so only one status applies
        if ($delvdate != "" && $delvdate != "0000-00-00") {
            $status = 9;
            $statusText = "Delivered";
        } elseif ($arrvport != "" && $arrvport != "0000-00-00" && $arrvport <= $today) {
            $status = 7;
            $statusText = "Arrived at Port";
        } elseif ($saildate != "" && $saildate != "0000-00-00") {
            $status = 5;
            $statusText = "In Transit";
        }


        // ✅ 3. Send WhatsApp only if status CHANGED and > 1
        if ($status != $old_status && $status > 1) {
            $message = "📦 Current Status: $statusText\n"
                     . "🚢 Shipment #GC-$mvouch Dated: " . format_date($vouchdt) . "\n"
                     . "🛫 Origin: $origin\n"
                     . "🛬 Destination: $destination";

            // Include ETA only if provided
            if (!empty($estimdate) && $estimdate != "0000-00-00") {
                $message .= "\n⏰ ETA: " . format_date($estimdate);
            }
            sendWhatsappMessage($mobileno, $message, $instid, $accstkn);
        }



        ######################## ACCOUNTS PART ######################################

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$mvouch' AND `source`='$source'") or die(mysqli_error($conn));

        if ($amount > 0) {
            $minus_amt = ($amount * -1);

            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, init, accode, grpcode, amount)
                          VALUES ('$vouchdt','$mvouch', '$recpno', '$source', '001', '', '$salcode','005', '$amount') ") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, init, accode, grpcode, amount)
                    VALUES ('$vouchdt','$mvouch', '$recpno', '$source', '002', '', '$party', '010', '$minus_amt' )") or die(mysqli_error($conn));
        }


        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `voucher`, `vouchdt`, `customer`, `description`, `nocart`, `amount`, 
                                                       `supplier`, `origin`, `pcs`, `unit`, `marka`, `totcbm`, `estimdate`,`arrvport`,
                                                       `containerno`, `saildate`, `delvdate`, `destination`, `mobile`, `weight`, `status`,
                                                       `overflow`, `overflowrem`, `cbmdetail`, `adduser`, `addtime`)
                                                VALUES('$qvouch', '$voucher', '$vouchdt', '$customer', '$description', '$nocart', '$amount',
                                                       '$supplier', '$origin', '$pcs', '$unit', '$marka', '$totcbm', '$estimdate','$arrvport',
                                                       '$containerno', '$saildate', '$delvdate', '$destination', '$mobile', '$weight', '$status',
                                                       '$overflow', '$overflowrem', '$cbmdetail', '$adduser', '$addtime')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `customer`='$customer', `description`='$description',
                                                      `nocart`='$nocart', `supplier`='$supplier', `origin`='$origin', `pcs`='$pcs', `amount`='$amount',       
                                                      `unit`='$unit', `marka`='$marka', `totcbm`='$totcbm', `estimdate`='$estimdate', `arrvport`='$arrvport',
                                                      `containerno`='$containerno', `saildate`='$saildate', `delvdate`='$delvdate', `destination`='$destination',
                                                      `weight`='$weight', `mobile`='$mobile', `status`='$status', overflow='$overflow', overflowrem='$overflowrem',
                                                      `cbmdetail`='$cbmdetail', `edituser`='$adduser', `edittime`='$addtime'
                                                      WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getcust = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode='$get[customer]'") or die(mysqli_error($conn));
    $fetcust = mysqli_fetch_array($getcust);

    $getstatnm = mysqli_query($conn, "SELECT name FROM `status` WHERE code='$get[status]'") or die(mysqli_error($conn));
    $fetstatnm = mysqli_fetch_array($getstatnm);
}

// $printdisabled = "none";

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]'";
    $delcond2 = " vouchno = '$_GET[vouchno]'";

    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond2", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");

}

include("includes/linkshead.php");
?>
<style type="text/css">
    .fontnew {
        font-weight: bold;
        color: #00bbf9;
    }
</style>
<script src="MyScript/shipment.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shipment_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-left pad">
                                <label class="label-control marg">
                                    <h3 class="panel-title"><?php echo $formname; ?></h3>
                                </label>
                            </div>

                            <div class="col-lg-8 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="font-size: 15px;font-weight:bold">Status :
                                    <span style="color:red"><?= $fetstatnm["name"] ?></span></label>
                            </div>
                        </div>
                    </div>

                    <div class="panel-body">
                        <!-- Voucher GL-1 Form -->
                        <!-- Date -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <!-- <input type="text" class="form-control vouchno" name="vouchno" required value="<?php echo ($addflag != 1) ? $qvouch : $qvouch; ?>" readonly> -->
                                <input type="text" class="form-control form" name=""
                                    value="GC-<?php echo ($addflag != 1) ? $get["vouchno"] : "NEW"; ?>" readonly>
                                <input type="hidden" class="form-control form vouchno" name="vouchno"
                                    value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required
                                    readonly>
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt" required
                                    value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date("Y-m-d"); ?>">
                            </div>
                        </div>

                        <!-- Customer Name / Code -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Customer</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="customer" name="customer" class="form-control form select2 customer"
                                    required>
                                    <option value="<?php echo ($addflag != 1) ? $get['customer'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $fetcust['name'] : ""; ?>
                                    </option>
                                    <?php
                                    $pparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='010' order by name") or die(mysqli_error($conn));
                                    while ($mparty = mysqli_fetch_array($pparty)) {
                                        $aname = $mparty['name'];
                                        echo "<option value='$mparty[accode]'>$aname</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <!-- CBM Detail -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">CBM Detail</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <select id="cbmdetail" name="cbmdetail" class="form-control form select2 cbmdetail">
                                    <?php
                                    $paidbyOptions = [
                                        'Y' => 'YES',
                                        'N' => 'NO'
                                    ];

                                    foreach ($paidbyOptions as $value => $label) {
                                        $selected = ($get['cbmdetail'] == $value) ? 'selected' : '';
                                        echo "<option value=\"$value\" $selected>$label</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <!-- Marker -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-xs-3 col-sm-3 text-right pad">
                                <label class="label-control marg fontnew">Marka</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-xs-2 col-sm-2 pad">
                                <input type="text" class="form-control form fontnew marka" name="marka"
                                    value="<?php echo ($addflag != 1) ? $get['marka'] : ""; ?>" maxlength="20"
                                    style="font-size:15px">
                            </div>
                        </div>

                        <div class="row">
                            <!-- No of Cartons -->
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg fontnew cartlabel">No.of Cartons</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" class="form-control form nocart text-right fontnew" name="nocart"
                                    value="<?php echo ($addflag != 1) ? $get['nocart'] : ""; ?>" style="font-size:15px"
                                    readonly>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: red;"><b>Overflow</b></label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" class="form-control form overflow text-right fontnew"
                                    name="overflow" id="overflowInput"
                                    value="<?php echo ($addflag != 1) ? $get['overflow'] : ""; ?>"
                                    style="font-size:15px;color:red">
                            </div>
                        </div>

                        <!-- Hidden remarks div -->
                        <div class="row" id="remarksDiv" style="display:none;">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color: red;"><b>Remarks</b></label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad"
                                style="border:2px solid red;border-radius:7px">
                                <input type="text" class="form-control form overflowrem" name="overflowrem"
                                    maxlength="50" id="remarksInput"
                                    value="<?php echo ($addflag != 1) ? $get['overflowrem'] : ""; ?>">
                            </div>
                        </div>

                        <!-- Supplier Name / Code -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Supplier</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-7 col-xs-7 pad">
                                <input type="text" class="form-control form supplier" maxlength="50" name="supplier"
                                    value="<?php echo ($addflag != 1) ? $get['supplier'] : ""; ?>">
                            </div>
                        </div>

                        <!-- Garment Description -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Description</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-7 col-xs-7 pad">
                                <input list="garments" class="form-control form" name="description" maxlength="50"
                                    value="<?php echo ($addflag != 1) ? $get['description'] : ""; ?>" required>
                                <datalist id="garments">
                                    <?php
                                    $pparty1 = mysqli_query($conn, "SELECT `description` FROM `$maindb` group by `description` order by `description`") or die(mysqli_error($conn));
                                    while ($mparty1 = mysqli_fetch_array($pparty1)) {
                                        $aname1 = $mparty1['description'];
                                        echo "<option value='$aname1'>";
                                    }
                                    ?>
                                </datalist>
                            </div>
                        </div>

                        <!-- Pcs -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg fontnew">Pcs</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" class="form-control form  text-right fontnew" name="pcs"
                                    value="<?php echo ($addflag != 1) ? $get['pcs'] : ""; ?>" style="font-size:15px">
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Unit</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="unit" maxlength="10"
                                    value="<?php echo ($addflag != 1) ? $get['unit'] : ""; ?>">
                            </div>
                        </div>



                        <!-- GSM -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg fontnew">Total CBM</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form totcbm text-right fontnew" name="totcbm"
                                    value="<?php echo ($addflag != 1) ? $get['totcbm'] : ""; ?>" readonly tabindex="-1"
                                    style="font-size:15px">
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Weight</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" step="any" class="form-control form  text-right" name="weight"
                                    value="<?php echo ($addflag != 1) ? $get['weight'] : ""; ?>">
                            </div>
                        </div>

                        <!-- Buyer -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Origin</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad">
                                <select id="origin" name="origin" class="form-control form select2 origin" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['origin'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $get['origin'] : ""; ?>
                                    </option>
                                    <?php
                                    $getOrigin = mysqli_query($conn, "SELECT `code`,`name` FROM `origin` order by name") or die(mysqli_error($conn));
                                    while ($fetOrigin = mysqli_fetch_array($getOrigin)) {
                                        echo "<option value='$fetOrigin[name]'>$fetOrigin[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <!-- Container No. -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Container No.</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form containerno" name="containerno"
                                    maxlength="30" value="<?php echo ($addflag != 1) ? $get['containerno'] : ""; ?>"
                                    required>
                            </div>
                        </div>
                        <!-- Delivery Date -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sailing Date</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form" name="saildate"
                                    value="<?php echo ($addflag != 1) ? $get['saildate'] : ""; ?>">
                            </div>
                        </div>
                        <!-- Division -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Destination</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select id="destination" name="destination"
                                    class="form-control form select2 destination" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['destination'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $get['destination'] : ""; ?>
                                    </option>
                                    <?php
                                    $getDestination = mysqli_query($conn, "SELECT `code`,`name` FROM `destination` order by name") or die(mysqli_error($conn));
                                    while ($fetDestination = mysqli_fetch_array($getDestination)) {
                                        echo "<option value='$fetDestination[name]'>$fetDestination[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>


                        <!-- Estd. Date -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Estimated Date</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form" name="estimdate"
                                    value="<?php echo ($addflag != 1) ? $get['estimdate'] : ""; ?>">
                            </div>
                        </div>
                        <!-- Estd. Date -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Port Arrival Date</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form" name="arrvport"
                                    value="<?php echo ($addflag != 1) ? $get['arrvport'] : ""; ?>">
                            </div>

                            <!-- Delivery Date -->
                            <div class="row">
                                <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg" style="color: red;">Delivery Date</label>
                                </div>
                                <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                    <input type="date" class="form-control form" name="delvdate"
                                        value="<?php echo ($addflag != 1) ? $get['delvdate'] : ""; ?>"
                                        style="color: red" readonly>
                                </div>
                            </div>
                        </div>


                        <!-- Middle -->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form mobileno" name="mobile"
                                    value="<?php echo ($addflag != 1) ? $get['mobile'] : ""; ?>">
                            </div>

                            <!-- AMOUNT -->
                            <div class="row">
                                <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg" style="color: red;">Amount</label>
                                </div>
                                <div class="col-lg-2 col-md-4 col-sm-4 col-xs-4 pad">
                                    <input type="text" class="form-control form numeric text-right" name="amount"
                                        value="<?php echo ($addflag != 1) ? $get['amount'] : ""; ?>" style="color: red">
                                </div>
                            </div>
                        </div>

                    </div>

                    <div class="panel-footer">
                        <div class="col-lg-1 pad"></div>
                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 text-center pad">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                        <div class="col-lg-1 text-left pad">
                            <?php if ($addflag != 1) { ?>
                                <a id='whats' class="btn btn-pad btn-square "
                                    style="background-color:green;margin-top: 5px;"
                                    href="shipment_pdf.php?vouchno=<?= $qvouch ?>&para=whats" target="_blank">
                                    <i class="fa fa-whatsapp" style="color:#ffffff"></i>
                                </a>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade cmbModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal"><span>&times;</span></button>
                        <h4 class="modal-title" id="myLargeModalLabel">Cartons Details</h4>
                    </div>
                    <div class="modal-body">
                        <!-- Size Table -->
                        <div class="row">
                            <div class="col-lg-12 pad">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr class="mySearchTr">
                                            <th class="text-right"></th>
                                            <th class="text-right">Sr.</th>
                                            <th class="text-right">L (cm)</th>
                                            <th class="text-right">W (cm)</th>
                                            <th class="text-right">H (cm)</th>
                                            <th class="text-right">#</th>
                                            <th class="text-right">CBM</th>
                                        </tr>
                                    </thead>
                                    <tbody class="detbody">
                                        <?php
                                        if ($addflag != 1) {
                                            $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$vouchno' ORDER BY srno") or die(mysqli_error($conn));

                                            if (mysqli_num_rows($qry) > 0) {
                                                $tcart = $nocart - mysqli_num_rows($qry);
                                                while ($row = mysqli_fetch_array($qry)) {
                                                    $mctr++;
                                                    echo "<tr>";
                                                    echo "<td><i class='fa fa-close remove'></i></td>";
                                                    echo "<td><input type='text' name='srno_$ctr' class='srno form-control text-right formins'  value='$mctr' readonly tabindex='-1'></td>";
                                                    echo "<td><input type='number' step='any' name='len_$ctr' class='len form-control text-right formins' value='$row[len]' ></td>";
                                                    echo "<td><input type='number' step='any' name='brea_$ctr' class='brea form-control text-right formins' value='$row[brea]' ></td>";
                                                    echo "<td><input type='number' step='any' name='heig_$ctr' class='heig form-control text-right formins' value='$row[heig]' ></td>";
                                                    echo "<td><input type='number' name='cartcount_$ctr' class='cartcount form-control text-right formins' value='$row[cartcount]' ></td>";
                                                    echo "<td><input type='number' step='any' name='cbm_$ctr' class='cbm form-control text-right formins' value='$row[cbm]' readonly   tabindex='-1'></td>";
                                                    echo "</tr>";
                                                    $ctr++;
                                                }
                                            }
                                        }
                                        ?>
                                        <input type="hidden" id="items" name="items" value="<?= $ctr ?>">
                                    </tbody>
                                </table>
                            </div>
                            <div class="row">
                                <div class="col-lg-9 text-right">
                                    <label class="label-control marg">Total Cartons&nbsp;&nbsp;<span
                                            class='tcart text-right' style='font-size:16px;'></span></label>
                                </div>
                                <div class="col-lg-2 text-right">
                                    <label class="label-control marg">Total CBM&nbsp;&nbsp;<span class='tcbm text-right'
                                            style='font-size:16px;'></span></label>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <div class="col-lg-1">
                            <button type="button" class="btn btn-primary btn-square form" id="addnew"
                                style="background-color:green;color:white">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>
                            </button>
                        </div>
                        <div class="col-lg-9">
                        </div>
                        <div class="col-lg-10">
                            <input type="button" class="btn btn-primary closeModal" value="Close">
                        </div>

                    </div>

                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Vouchno</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</option>
                                <option value="4">Supplier</option>
                                <option value="5">Marka</option>
                                <option value="6">Origin</option>
                                <option value="7">Container No</option>
                                <option value="8">Destination</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Vouchno</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</option>
                                <option value="4">Supplier</option>
                                <option value="5">Marka</option>
                                <option value="6">Origin</option>
                                <option value="7">Container No</option>
                                <option value="8">Destination</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#a98467;">
                                    <th style="color: white;" width="3%">Voucher</th>
                                    <th style="color: white;" width="8%">Date</th>
                                    <th style="color: white;" width="15%">Customer</th>
                                    <th style="color: white;" width="15%">Supplier</th>
                                    <th style="color: white;" width="7%">Marka</th>
                                    <th style="color: white;" width="7%">Origin</th>
                                    <th style="color: white;" width="10%">Container No</th>
                                    <th style="color: white;" width="8%">Destination</th>
                                    <th style="color: white;" width="5%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>