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

$formname = 'Challan';
$maindb = 'chalmain';
$trandb = 'chaltran';
// $accdb = 'actran';

$printfile = "challan_pdf.php";
$file = 'challan_new.php';
$trandbexist = 1;
$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];

$formname = 'Challan / Bill';
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$par = $_GET["par"];
$zyear = right($zdb, 4);
$comp = $_SESSION["comp_" . $dbname];

$zStartDate = getzdates($zyear, 'S', $conn);
$zEndDate = getzdates($zyear, 'E', $conn);

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $remark = trim($_POST["remark"]);
    $remark2 = trim($_POST["remark2"]);
    $note1 = trim($_POST["note1"]);
    $note2 = trim($_POST["note2"]);
    $party = trim($_POST["party"]);
    $vessel = $_POST["vessel"];
    $manualno = $_POST["manualno"];
    $timesheetno = $_POST["timesheetno"];
    $ordby = $_POST["ordby"];
    $anchor = $_POST["anchor"];
    $chalamt = $_POST["chalamt"];
    $purpose = $_POST["purpose"];
    $purpose2 = $_POST["purpose2"];
    $purpose3 = $_POST["purpose3"];
    $purpose4 = $_POST["purpose4"];
    $otamt = $_POST["otamt"];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;
    

    if ($a == 1) {

        #####################IMAGE#####################
        $path = "images/" . $myyear . "/" . $comp . "/";
        if (!is_dir($path)) {
            mkdir($path);
        }
        $filename1 = $mvouch . ".jpg";
        $img_file1 = $path . ($filename1);
        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);
        $size = $_FILES["file1"]["size"];

        if ($size > 20480000) {
            $uploadOk = 0;
            $a = 0;
        } else {
            $uploadOk = 1;
            $a = 1;
        }

        if ($uploadOk == 1) {
            move_uploaded_file($_FILES["file1"]["tmp_name"], $img_file1);
        } else {
            browse("Sorry, there was an error uploading your file.(Check Size)");
            header("refresh: 1; $file?mode=last");
        }

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE,`$trandb` WRITE ") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `$maindb` where vouchno != ''  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;
        }
        #################################TRAN PART #################################

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        // mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' and `source`='$source'") or die(mysqli_error($conn));
        //        mysqli_query($conn, "DELETE FROM $zroot.$combstkdb WHERE `$keyfld`='$mvouch' AND `source`='$source' and `compcode`='$compcd'")or die(mysqli_error($conn));


        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno    = $_POST["srno_$i"];
            $launch  = $_POST["launch_$i"];
            $type    = $_POST["type_$i"];
            $frmdt   = $_POST["frmdt_$i"];
            $frmtime = $_POST["frmtime_$i"];
            $todt    = $_POST["todt_$i"];
            $totime  = $_POST["totime_$i"];
            $nohrs   = $_POST["nohrs_$i"];
            $nomin   = $_POST["nomin_$i"];
            $rate    = $_POST["rate_$i"];
            $otamt   = $_POST["otamt_$i"];
            $amount  = $_POST["amount_$i"];
            $calcamt  = $_POST["calcamt_$i"];


            if (!empty($launch)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, `vouchno`, `vouchdt`, `launch`, `type`, `frmdt`, `frmtime`, `todt`, `totime`,
                                                        `nohrs`,`nomin`, `rate`, `otamt`,`amount`,`calcamt`)
                                        VALUES( '$srl', '$qvouch', '$vouchdt', '$launch', '$type', '$frmdt', '$frmtime', '$todt', '$totime',
                                         '$nohrs','$nomin','$rate','$otamt','$amount','$calcamt')") or die(mysqli_error($conn));

                ##########sTOCK pART########
                // mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`,color,`godown`)
                //                         VALUES( '$srl', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount','$color','$godown')") or die(mysqli_error($conn));

                //                ##########Combine Stock Part########
                //                mysqli_query($conn, "INSERT INTO $zroot.$combstkdb(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`)
                //                                        VALUES( '$srl', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount')") or die(mysqli_error($conn));
            }
        }

        #################################TRAN PART END #################################
        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `anchor`, `ordby`, `vessel`,`manualno`, `timesheetno`,`party`,
                                                     `purpose`,`purpose2`,`purpose3`, `purpose4`,  `imgpath`, `amount`, `remark`, `remark2`)
                                     VALUES ($qvouch, '$vouchdt', '$anchor', '$ordby','$vessel', '$manualno','$timesheetno',
                                     '$party', '$purpose','$purpose2', '$purpose3','$purpose4',
                                     '$img_file1', '$chalamt','$remark', '$remark2')") or die(mysqli_error($conn));

            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt',`anchor`='$anchor', `ordby`='$ordby', `vessel`='$vessel',
            `manualno`='$manualno', `timesheetno`='$timesheetno',`party`='$party', `purpose`='$purpose', `purpose2`='$purpose2',
            `purpose3`='$purpose3', `purpose4`='$purpose4',`imgpath`='$img_file1', `amount`='$chalamt',
            `remark`='$remark',`remark2`='$remark2'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) {
    $gendt = mysqli_query($conn, "SELECT vouchdt FROM $maindb order by vouchdt desc limit 1") or die(mysqli_error($conn));
        $fetdt = mysqli_fetch_assoc($gendt);
        $prevdt = $fetdt['vouchdt'];
}

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);
    $imgpath = $get['imgpath'];
    $gparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`= '$get[party]'") or die(mysqli_error($conn));
    $lparty = mysqli_fetch_array($gparty);

    $getanch = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$get[anchor]'") or die(mysqli_error($conn));
    $fanch = mysqli_fetch_array($getanch);

    $getlaunch = mysqli_query($conn, "SELECT `name` FROM `launch` WHERE `code`= '$get[launch]'") or die(mysqli_error($conn));
    $flaunch = mysqli_fetch_array($getlaunch);

    $gordby = mysqli_query($conn, "SELECT `name` FROM `ordby` WHERE `code`= '$get[ordby]'") or die(mysqli_error($conn));
    $fordby = mysqli_fetch_array($gordby);

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

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    unlink($imgpath);
    // $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    // delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<script src="MyScript/challan_new.js" type="text/javascript"></script>
<style>
    /* Modal Styles */
    .modal {
        display: none;
        position: fixed;
        /* z-index: 1; */
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
    }

    .modal-content {
        background-color: #fefefe;
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 100%;
    }

    .modal-img {
        background-color: #fefefe;
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 50%;
    }

    .close-btn {
        color: #C1121f;
        font-size: 28px;
        font-weight: bold;
        position: absolute;
        top: 0;
        right: 15px;
        cursor: pointer;
    }
</style>
</head>

<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php echo $formname; ?></h3>
                        <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                        <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="hidden" class="form-control addflag" value="<?php echo $addflag ?>">
                                <input type="text" class="form-control text-size form" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" class="form-control focusfield form vouchdt" name="vouchdt" tabindex="1" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : $prevdt; ?>" required autofocus>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-1 col-xs-2 text-right pad">
                                <label class="label-control marg">Purpose</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form purpose" name="purpose" tabindex="8" value="<?php echo ($addflag != 1) ? $get["purpose"] : ""; ?>">
                            </div>
                            <div class="col-lg-2 col-xs-3 text-center pad " id="openModal">
                                <?php
                                if ($imgpath != '') {
                                ?>
                                    <img id="preview" src="<?php echo $imgpath; ?>" style="height: 110px;width:130px; margin-bottom: -84px" alt="your image" />
                                <?php } ?>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Man. No</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="manualno" class="form-control form manualno" tabindex="2" id="manualno" value="<?php echo ($addflag != 1) ? $get['manualno'] : "" ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Timesheet#</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form timesheetno" name="timesheetno" tabindex="3" value="<?php echo ($addflag != 1) ? $get["timesheetno"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form purpose2" name="purpose2" tabindex="9" value="<?php echo ($addflag != 1) ? $get["purpose2"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                                <select class="form-control form party select2" name="party" tabindex="4" style="width: 100%" required>
                                    <option style="font-weight:bold;" value="<?php echo ($addflag != 1) ? $get["party"] : ""; ?>"><?php echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`!='' ORDER by name") or die(mysqli_error($conn));
                                    while ($fparty = mysqli_fetch_array($gparty)) {
                                        echo "<option style='font-weight:bold;' value='$fparty[accode]'>$fparty[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form purpose3" name="purpose3" tabindex="10" value="<?php echo ($addflag != 1) ? $get["purpose3"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Vessel</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                                <select class="form-control form  vessel select2" id="vessel" tabindex="5" name="vessel" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $get["vessel"] : ""; ?>"><?php echo ($addflag != 1) ? $fvessel["name"] : ""; ?></option>
                                    <?php
                                    $gvessel = mysqli_query($conn, "SELECT `code`,`name` FROM `vessel` WHERE `code`!='' order by name") or die(mysqli_error($conn));
                                    while ($fetvessel = mysqli_fetch_array($gvessel)) {
                                        echo "<option value='$fetvessel[code]'>$fetvessel[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-2 text-right">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form purpose4" name="purpose4" tabindex="11" value="<?php echo ($addflag != 1) ? $get["purpose4"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-2 text-right pad">
                                <label class="label-control marg">Ord. By</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                                <select class="form-control form  vessel select2" id="ordby" tabindex="6" name="ordby" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $get["ordby"] : ""; ?>"><?php echo ($addflag != 1) ? $fordby["name"] : ""; ?></option>
                                    <?php
                                    $getordby = mysqli_query($conn, "SELECT `code`,`name` FROM `ordby` WHERE `code`!='' order by name") or die(mysqli_error($conn));
                                    while ($fetordby = mysqli_fetch_array($getordby)) {
                                        echo "<option value='$fetordby[code]'>$fetordby[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <input type="file" name="file1" id="file1" class="form file1" tabindex="12">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-1 col-xs-2 text-right pad">
                                <label class="label-control marg">Anchor</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                                <select class="form-control form  anchor select2" id="anchor" tabindex="7" name="anchor" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $get["anchor"] : $get['anchor']; ?>"><?php echo ($addflag != 1) ? $fanch["name"] : $fanch['name']; ?></option>
                                    <?php
                                    $ganch = mysqli_query($conn, "SELECT `code`,`name` FROM `port` WHERE `code`!='' order by name") or die(mysqli_error($conn));
                                    while ($fetanch = mysqli_fetch_array($ganch)) {
                                        echo "<option value='$fetanch[code]'>$fetanch[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead style="" class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="15%">Launch</th>
                                        <th width="15%">Type</th>
                                        <th width="8%">From</th>
                                        <th width="4%">Time</th>
                                        <th width="8%">To </th>
                                        <th width="4%">Time</th>
                                        <th width="7%" class='text-right'>Hours</th>
                                        <th width="7%" class='text-right'>Mins</th>
                                        <th width="9%" class='text-right'>Rate</th>
                                        <th width="9%" class='text-right'>Crew O/T</th>
                                        <th width="9%" class='text-right'>Amount</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $glaunch = mysqli_query($conn, "SELECT `name` FROM `launch` WHERE code = $tran[launch]") or die(mysqli_error($conn));
                                            $flaunch = mysqli_fetch_array($glaunch);
                                            if ($tran['type'] == 'H') {
                                                $type = "Hours";
                                            } else {
                                                $type = "Lumpsum";
                                            }
                                            echo "<tr>";
                                            echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            echo "<td><input type='text' class='form-control form text-right srno'  name='srno_$ctr'        value='$tran[srno]'></td>";
                                            echo "<td><select class='form-control form select2 launch' name='launch_$ctr' style='width:100%'>";
                                            echo "<option value='$tran[launch]'>$flaunch[name]</option>";
                                            echo "</select></td>";
                                            echo "<td><select class='form-control form select2 type' name='type_$ctr' style='width:100%'>";
                                            echo "<option value='$tran[type]'>$type</option>";
                                            echo "</select></td>";
                                            echo "<td><input type='date' class='form-control form frmdt' name='frmdt_$ctr' value='$tran[frmdt]' required></td>";
                                            echo "<td><input type='number' maxlength='4' class='form-control form text-right frmtime' name='frmtime_$ctr' value='$tran[frmtime]'></td>";
                                            echo "<td><input type='date' class='form-control form todt' name='todt_$ctr' value='$tran[todt]' required></td>";
                                            echo "<td><input type='number' maxlength='4' class='form-control form text-right totime' name='totime_$ctr' value='$tran[totime]'></td>";
                                            echo "<td><input type='number' class='form-control text-right form nohrs' name='nohrs_$ctr' value='$tran[nohrs]' tabindex='-1' readonly></td>";
                                            echo "<td><input type='number' class='form-control text-right form nomin' name='nomin_$ctr' value='$tran[nomin]' tabindex='-1' readonly></td>";
                                            echo "<td><input type='number' class='form-control form text-right rate'   name='rate_$ctr'        value='$tran[rate]'>
                                            <input type='hidden' class='form-control form text-right calcamt'   name='calcamt_$ctr'        value='$tran[calcamt]'>   </td>";
                                            echo "<td><input type='number' class='form-control form text-right otamt'  name='otamt_$ctr'       value='$tran[otamt]'>  </td>";
                                            echo "<td><input type='number' class='form-control form text-right amount' name='amount_$ctr'      value='$tran[amount]' readonly> </td>";
                                            echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        $yctr = 1;
                                    ?>
                                        <tr>
                                            <td><i class="fa fa-close remove" style="display:none"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                            <td><select name="launch_<?= $pctr ?>" class="form-control form select2 launch" tabindex="13" style="width: 100%">
                                                    <option value=""></option>
                                                    <? $glaunch = mysqli_query($conn, "SELECT `code`,`name` FROM `launch` WHERE `code`!='' order by name") or die(mysqli_error($conn));
                                                    while ($fetlaunch = mysqli_fetch_array($glaunch)) {
                                                        echo "<option value='$fetlaunch[code]'>$fetlaunch[name]</option>";
                                                    } ?>
                                                </select></td>
                                            <td><select name="type_<?= $pctr ?>" class="form-control form select2 type" tabindex="14" style="width: 100%">
                                                    <option value="H">Hours</option>
                                                    <option value="L">Lumpsum</option>
                                                    <option value="H">Hours</option>
                                                </select></td>
                                            <td><input type="date" class="form-control form frmdt" tabindex="15" name="frmdt_<?= $pctr ?>" value="" required></td>
                                            <td><input type="number" class="form-control text-right form frmtime" tabindex="16" name="frmtime_<?= $pctr ?>" value=""></td>
                                            <td><input type="date" class="form-control form todt" name="todt_<?= $pctr ?>" tabindex="17" value="" required></td>
                                            <td><input type="number" class="form-control text-right form totime" tabindex="18" name="totime_<?= $pctr ?>" value=""></td>
                                            <td><input type="number" class="form-control text-right form nohrs" tabindex="19" name="nohrs_<?= $pctr ?>" value="0" tabindex="-1" readonly></td>
                                            <td><input type="number" class="form-control text-right form nomin" tabindex="20" name="nomin_<?= $pctr ?>" value="0" tabindex="-1" readonly></td>
                                            <td><input type="number" class="form-control form text-right rate" tabindex="21" name="rate_<?= $pctr ?>" value="0">
                                            <input type="hidden" class="form-control form text-right calcamt" name="calcamt_<?= $pctr ?>" value=""></td>
                                            <td><input type="number" class="form-control form text-right otamt" tabindex="22" name="otamt_<?= $pctr ?>" value="0"></td>
                                            <td><input type="number" class="form-control form text-right amount" tabindex="23" name="amount_<?= $pctr ?>" value="0" readonly></td>
                                        </tr>
                                    <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>

                        </div>

                        <div class="row">
                            <div class="col-lg-1">
                                <button type="button" class="btn btn-square form" style="background-color: #78909C" id="addnew">
                                    <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-10 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <label class="label-control marg">Total</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form text-right chalamt" name="chalamt" value="<?php echo ($addflag != 1) ? $get['amount'] : ""; ?>" readonly>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php echo ($addflag != 1) ? $get["remark"] : ""; ?>">
                                <input type="text" class="form-control form " name="remark2" value="<?php echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" 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=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr class="mySearchTr" style="background-color: #78909C;">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Manual No</th>
                                    <th>Timesheet No</th>
                                    <th>Party</th>
                                    <th>Vessel</th>
                                    <th>Ord. By</th>
                                    <th>Anchor</th>
                                    <th>Total</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`") or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {

                                        $getparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`= '$get[party]'") or die(mysqli_error($conn));
                                        $letparty = mysqli_fetch_array($getparty);
                                    
                                        $ganch = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$get[anchor]'") or die(mysqli_error($conn));
                                        $fetanch = mysqli_fetch_array($ganch);
                                    
                                        $getlaunch = mysqli_query($conn, "SELECT `name` FROM `launch` WHERE `code`= '$get[launch]'") or die(mysqli_error($conn));
                                        $flaunch = mysqli_fetch_array($getlaunch);
                                    
                                        $getordby = mysqli_query($conn, "SELECT `name` FROM `ordby` WHERE `code`= '$get[ordby]'") or die(mysqli_error($conn));
                                        $fetordby = mysqli_fetch_array($getordby);
                                    
                                        $gvessel = mysqli_query($conn, "SELECT `name` FROM `vessel` WHERE `code`= '$get[vessel]'") or die(mysqli_error($conn));
                                        $fetvessel = mysqli_fetch_array($gvessel);
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[manualno]</td>";
                                        echo "<td align='left'>$row[timesheetno]</td>";
                                        echo "<td align='left'>$letparty[name]</td>";
                                        echo "<td align='left'>$fetvessel[name]</td>";
                                        echo "<td align='left'>$fetordby[name]</td>";
                                        echo "<td align='left'>$fetanch[name]</td>";
                                        echo "<td align='left'>$row[total]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <div id="myModal" class="modal">
        <div class="modal-img">
            <span class="close-btn">&times;</span>
            <img id="preview" src="<?php echo $imgpath; ?>" style="height: 500px;width:100%" alt="your image" />
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>