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

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

$maindb = 'shipment';
$formname = "Container Port Arrival 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"];
$container = $_POST["container"];

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

$today = date("Y-m-d");

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
    $mdelvdate = $_POST["mdelvdate"];

    for ($i = 0; $i <= $count; $i++) {

        $src = $_POST["src_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $containerno = $_POST["containerno_$i"];
        $selcheck = $_POST["selcheck_$i"];
        $delvdate = $_POST["delvdate_$i"];

        $get_oldsts = mysqli_query($conn, "SELECT status FROM $maindb WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
        $oldsts = mysqli_fetch_assoc($get_oldsts);
        $old_status = $oldsts["status"];

        $status = $old_status;

        if ($mdelvdate != "" && $mdelvdate != "0000-00-00" && $mdelvdate <= $today) {
            $status = 7; 
        } elseif ($delvdate != "" && $delvdate != "0000-00-00") {
            $status = 9; 
        }

        if ($selcheck == 1) {
            // `portrecddate`='$delvdate',
            mysqli_query($conn, "UPDATE `$maindb` SET `arrvport`='$mdelvdate', `status`='$status' WHERE vouchno='$vouchno' AND containerno='$containerno' ") 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($container)) {
    $contcond = " AND `containerno`='$container' ";
}


$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE saildate != '0000-00-00' AND  `arrvport` = '0000-00-00' 
                                 $custcond $suppcond $origcond $destcond $contcond order by vouchdt,vouchno") 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-6 text-right pad">
                        <label class="label-control marg">Date</label>
                    </div>
                    <div class="col-lg-1 pad">
                        <input type='date' class='form-control mdelvdate' value='' name='mdelvdate'>
                    </div>
                    <div class="col-lg-1 pad"></div>
                    <div class="col-lg-2 pad">
                        <input type="submit" name="submit" value="Update" class="btn btn-danger"
                            style="background-color:green; color: white;border-radius:4px">
                    </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'>
                                              <input type='hidden' class='form-control delvdate' value='$pmain[delvdate]'  name='delvdate_$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
}
?>