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

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

$maindb = 'shipment';

$file = "contupdate_fr.php";

$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 = "";

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

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

        $src = $_POST["src_$i"];
        $vouchno = $_POST["vouchno_$i"];
        // $containerno = $_POST["containerno_$i"];
        $saildate = $_POST["saildate_$i"];
        $arrvport = $_POST["arrvport_$i"];
        $delvdate = $_POST["delvdate_$i"];


        $status = 5;

        if ($arrvport != "" && $arrvport != "0000-00-00") {
            $status = 7;
        }

        if ($delvdate != "" && $delvdate != "0000-00-00") {
            $status = 9;
        }

        mysqli_query($conn, "UPDATE `$maindb` SET `saildate`='$saildate', `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($container)) {
    $contcond = " AND `containerno`='$container' ";
}


$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE vouchno != '' and saildate = '0000-00-00'
                                 $custcond $suppcond $origcond $destcond $contcond 
                                 ORDER BY vouchdt,customer,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 () {
            $("body").on("click", ".check", fillSail);

            function fillSail() {
                var tr = $(this).closest("tr");

                if ($(this).is(":checked")) {
                    var saildatemain = $(".saildatemain").val();
                    var contmain = $(".contmain").val();

                    if (contmain) {
                        tr.find(".containerno").val(contmain);
                    }
                    if (saildatemain) {
                        tr.find(".saildate").val(saildatemain);
                    }
                } else {
                    // If unchecked, clear the values in that row
                    // tr.find(".containerno").val('');
                    // tr.find(".saildate").val('');
                }
            }

            $('#gvCandidates_checkAll').on('change', function () {
                // Check if the main checkbox is checked
                var isChecked = this.checked;

                // Toggle all .check checkboxes
                $('.check').prop('checked', isChecked);

                if (isChecked) {
                    // Get values from main fields
                    var saildatemain = $(".saildatemain").val(),
                        contmain = $(".contmain").val();

                    // Set values to all fields
                    if (contmain) {
                        $(".containerno").val(contmain);
                    }
                    if (saildatemain) {
                        $(".saildate").val(saildatemain);
                    }
                } else {
                    // Clear values
                    // $(".containerno").val('');
                    // $(".saildate").val('');
                }
            });


        })
    </script>
    <!--<script src="MyScript/item.js" type="text/javascript"></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-5 text-right pad">
                        <!-- <label class="label-control marg"><b>Container No.</b></label> -->
                    </div>
                    <!-- <div class="col-lg-1 pad">
                        <input type='text' class='form-control contmain' value=''
                            style="border: 1px solid #9e9e9e;border-radius:4px" maxlength="15">
                    </div> -->
                    <div class="col-lg-1 text-right pad">
                        <label class="label-control marg"><b>Sailing Date</b></label>
                    </div>
                    <div class="col-lg-1 pad">
                        <input type='date' class='form-control saildatemain' value=''>
                    </div>
                    <div class="col-lg-1 pad"></div>
                    <div class="col-lg-1 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">
                        <!--<input type="submit" name="submit" value="Update" class="btn btn-danger" style="background-color:green; color: white; float:right; margin-right: 5%;width: 10%">-->

                        <table style="background: white;width: 100%;" class=" table-condensed table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width: 3%;">Sr.</th>
                                    <th style="width: 5%;">Voucher</th>
                                    <th style="width: 6%;">Date</th>
                                    <th style="width: 15%;">Customer</th>
                                    <th style="width: 3%;">No. of Cartons</th>
                                    <th style="width: 15%;">Supplier</th>
                                    <th style="width: 10%;">Garment Description</th>
                                    <th style="width: 4%;">Quantity (Pcs)</th>
                                    <th style="width: 4%;">Unit</th>
                                    <th style="width: 10%;">Marka</th>
                                    <th style="width: 6%;" class="text-right">Total CBM</th>
                                    <th style="width: 5%;" class="text-right">Weight</th>
                                    <th style="width: 6%;">Origin</th>
                                    <th style="width: 6%;">
                                        <input id="gvCandidates_checkAll" type="checkbox" name="gvCandidates$ctl01$checkAll"
                                            onclick="CheckAllEmp(this);" style="position: static;height: 25px;width: 25px;">
                                    </th>
                                    <th style="width: 6%;">Container No.</th>
                                    <th style="width: 5%;">Sailing Date</th>
                                    <th style="width: 6%;">Destination</th>
                                    <th style="width: 8%;">Estimated Date</th>
                                    <th style="width: 10%;">Delivery Date</th>
                                    <th style="width: 6%;">Mobile</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 class='text-right'>$pmain[totcbm]</td>";
                                    echo "<td class='text-right'>$pmain[weight]</td>";
                                    echo "<td>$pmain[origin]</td>";
                                    echo "<td><input type='checkbox' name='selcheck_$ctr' value='1' class='form-control check' style='position: static;height: 25px;width: 25px;'></td>";
                                    // echo "<td><input type='text' class='form-control containerno' value='$pmain[containerno]' name='containerno_$ctr' maxlength='15'> </td>";
                                    echo "<td>$pmain[containerno]</td>";
                                    echo "<td><input type='date' class='form-control saildate' value='$pmain[saildate]' name='saildate_$ctr'> </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 arrvport' value='$pmain[arrvport]'  name='arrvport_$ctr'>
                                          <input type='hidden' class='form-control delvdate' value='$pmain[delvdate]'  name='delvdate_$ctr'>";
                                    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
}
?>