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

$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'shipment';
$formname = "Shipment Delivery Update";

$cust = 'accmast';
$custdb = $cust;

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$customer = $_POST["customer"];
$supplier = $_POST["supplier"];
$origin = $_POST["origin"];
$destination = $_POST["destination"];
$containerno = $_POST["containerno"];
$shipno = $_POST["shipno"];

$custcond = "";
$suppcond = "";
$origcond = "";
$destcond = "";
$contcond = "";
$shipcond = "";

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
    $delvdate = date("Y-m-d");
    
    for ($i = 0; $i <= $count; $i++) {
        $src = $_POST["src_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $containerno = $_POST["containerno_$i"];
        $selcheck = $_POST["selcheck_$i"];

        $status = 9;

        if ($selcheck == 1) {
            mysqli_query($conn, "UPDATE `$maindb` SET `delvdate`='$delvdate', `status`='$status' WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
        }
    }

    browse("Record Updated Succesfully!");
    echo "<script>window.close();</script>";
}

if (!empty($customer)) {
    $custcond = " AND `customer`='$customer' ";
}
if (!empty($supplier)) {
    $suppcond = " AND `supplier` LIKE '%$supplier%' ";
}
if (!empty($origin)) {
    $origcond = " AND `origin`='$origin' ";
}
if (!empty($destination)) {
    $destcond = " AND `destination`='$destination' ";
}
if (!empty($containerno)) {
    $contcond = " AND `containerno`='$containerno' ";
}
// if (!empty($shipno)) {
//     $shipcond = " AND `vouchno`='$shipno' ";
// }
if (!empty($shipno)) {
    // Remove everything before the dash
    $shipnum = preg_replace('/^[^\d]*/', '', $shipno);
    $shipcond = " AND CAST(SUBSTRING_INDEX(`vouchno`, '-', -1) AS UNSIGNED) = '$shipnum' ";
}


$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `arrvport` != '0000-00-00' and `delvdate`='0000-00-00'
                                 $shipcond $custcond $suppcond $origcond $destcond $contcond order by vouchdt") or die(mysqli_error($conn));
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold;
        }

        @media print{
            .noPrint{
                display: none;
            }
            table thead th{
                font-size:10px;
                font-weight:bold;
            }
            table tr td{
                font-size:9px;
            }
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#gvCandidates_checkAll').on('change', function () {
                $('.check').prop('checked', this.checked);
            });
        })
    </script>
    </head>
    <form method="POST">

        <body>
            <div class="container" style="width:100%">
                <?php
//            if ($scope != "") {
                include("includes/head_rep.php");
                ?>

                <div class="row">
                    <div class="col-lg-8 text-right pad">
                        <input type="submit" name="submit" value="Deliver" class="btn btn-danger" style="background-color:green; color: white;">
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12 pad">

                        <table style="background: white;width: 100%" class=" table-condensed table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th>Sr.</th>
                                    <th>Voucher No</th>
                                    <th>Date</th>
                                    <th>Customer</th>
                                    <th>No. of Cartons</th>
                                    <th>Supplier</th>
                                    <th>Garment Description</th>
                                    <th>Quantity (Pcs)</th>
                                    <th>Unit</th>
                                    <th>Marka</th>
                                    <th>Total CBM</th>
                                    <th>Weight</th>
                                    <th>Origin</th>
                                    <th>Container No.</th>
                                    <th>Destination</th>
                                    <th>Estimated Date</th>
                                    <th>Delivery Date</th>
                                    <th>Mobile</th>
                                    <th>          
                                        <input id="gvCandidates_checkAll" type="checkbox" name="gvCandidates$ctl01$checkAll" onclick="CheckAllEmp(this);" style="position: static;height: 25px;width: 25px;">
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $nctr = 1;
                                $totamt = 0;
                                $totpack = 0;
                                $totbill = 0;
                                $totitmpr = 0;
                                $totiqtty = 0;
                                $linstyle = '';
                                $status = '';

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `$custdb` WHERE `accode`='$pmain[customer]'") or die(mysqli_error($conn));
                                    $fparty = mysqli_fetch_array($getparty);
                             

                                    $nvouchdt = mydt($pmain['vouchdt']);
                                    $estdate = mydt($pmain['estimdate']);
                                    $delvdate = mydt($pmain['delvdate']);

                                    Echo "<tr class='clickable-row2' id='boldfont' style='$linstyle' data-target='_blank' data-href='shipment.php?vouchno=$pmain[vouchno]&mode=edit'>";
                                    Echo "<td>$nctr</td>";
                                    Echo "<td>GC-$pmain[vouchno]</td>";
                                    Echo "<td>$nvouchdt</td>";
                                    Echo "<td>$fparty[name]</td>";
                                    Echo "<td>$pmain[nocart]</td>";
                                    Echo "<td>$pmain[supplier]</td>";
                                    Echo "<td>$pmain[description]</td>";
                                    Echo "<td>$pmain[pcs]</td>";
                                    Echo "<td>$pmain[unit]</td>";
                                    Echo "<td>$pmain[marka]</td>";
                                    Echo "<td>$pmain[totcbm]</td>";
                                    Echo "<td>$pmain[weight]</td>";
                                    Echo "<td>$pmain[origin]</td>";
                                    Echo "<td>$pmain[containerno]</td>";
                                    Echo "<td>$pmain[destination]</td>";
                                    Echo "<td>$estdate</td>";
                                    Echo "<td>$delvdate</td>";
                                    Echo "<td>$pmain[mobile]"
                                    . "<input type='hidden' class='form-control  ' value='$pmain[vouchno]'  name='vouchno_$ctr'>"
                                    . "<input type='hidden' class='form-control  ' value='$pmain[containerno]'  name='containerno_$ctr'>";
                                    Echo "<td><input type='checkbox' name='selcheck_$ctr' value='1' class='form-control check' style='position: static;height: 25px;width: 25px;'></td>";
                                    Echo "</tr>";
                                    $ctr++;
                                    $nctr++;
                                }
                                echo "<input type='hidden' name='items' value='$ctr'>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php
//            }
                ?>
            </div>

            <?php include("includes/linksfoot.php");
            ?>
        </body>
    </form>

    </html>
    <?php
}
?>