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

$formname = 'Dip Conversion';
$maindb = 'dipconmain';
$trandb = 'dipcontran';
$printfile = "dipconv_print.php";
$file = 'dipconv.php';
$trandbexist = 0;
$keyfld = "code";
$focusfield = "name";
//$mgrpcode = '010';
//$keycond = "AND `grpcode`='$mgrpcode'";

////////////////// FB ////////////////////
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
//$usertype = $_SESSION["usertype_" . $dbname];
$usertype = $_SESSION["usertype"];
// browse($usertype);

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


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

    $a = 1;
    
   $tankno = $_POST["tankno"];
   
   $count = $_POST["items"];
//   browse($count);
   
    $mvouch = $qvouch;

//    if ($name == "") {
//        $a = 0;
//        browse("Name Cannot Be Empty");
//        if ($addflag == 1) {
//            header("location:javascript://history.go(-1)");
//        } else {
//            header("refresh: ; $file?mode=last");
//        }
//    }

    if (isset($_POST["tankno"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `tankno`= '$tankno' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Tank No. Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }


    
    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLE `$maindb`,`$trandb` WRITE");
//        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` WHERE $keyfld !='' 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';
            }
            mysqli_query($conn, "INSERT INTO `$maindb`(code,`tankno`)
                                        VALUES($qvouch,'$tankno')") or die(mysqli_error($conn));
//            insmess();

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


            ////////////////FF///////////
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            mysqli_query($conn, "UPDATE `$maindb` SET `tankno`='$tankno' where $keyfld = '$mvouch' ") or die(mysqli_error($conn));
//            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
        
        
//        browse($mvouch);
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE tankno= '$mvouch' ") or die(mysqli_error($conn));
        
        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $height = $_POST["height_$i"];
//            browse($height);
            $capacity = $_POST["capacity_$i"];


            if (!empty($height)) {

                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, `height`, `capacity`, `tankno`)
                                    VALUES('$cnt', '$height', '$capacity','$mvouch')") or die(mysqli_error($conn));


            }
        }        
        
        
        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);

//    $getarea = mysqli_query($conn, "SELECT `code`,`area` FROM `area` WHERE `area`= '$get[area]'") or die(mysqli_error($conn));
//    $larea = mysqli_fetch_array($getarea);
    
    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `tankno` = '$get[code]' ORDER BY `srno`") or die(mysqli_error($conn));
    
    $rd="readonly";
}

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


if (isset($_GET["delete"])) {
//    $getcustdtl = mysqli_query($conn, "SELECT * FROM `actran` WHERE `accode` = '$getvouch'") or die(mysqli_error($conn));
//    if (mysqli_num_rows($getcustdtl) > 0) {
//        browse("Entry Exist in Transaction");
//        header("refresh: 1; $file?mode=last");
//    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "tankno", $_GET[$keyfld], "$file?mode=last");
//    }
}
 
include("includes/linkshead.php");
?>
<!--<script src="MyScript/openbal.js"  type="text/javascript"></script>-->   
<script src="MyScript/dipconv.js" type="text/javascript"></script>
<script src="MyScript/dipconv_search.js" type="text/javascript"></script>
<style>
    .disc{
        width:80px;
    }
    .disc1{
        width:50px;
    }
</style>

</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 pad">
                <div class="panel panel-flat" style="margin-bottom: -11px;">
                    <div class="panel-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <div class="heading-elements">

                        </div>
                        <a class="heading-elements-toggle"><i class="icon-menu"></i></a>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Tank No.</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" required name="tankno" value="<?php Echo ($addflag != 1) ? $get["tankno"] : ""; ?>" <?=$rd ?> autofocus>
                            </div>
                        </div>
                        
                        
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 ">
                <div class="panel panel-default">
<!--                    <div class="panel-heading">
                        <h3 class="panel-title">Tank Details</h3>
                    </div>-->
                    <div class="panel-body">
                        <div class='table-responsive' style="overflow-y:scroll; height: 300px">
                            <table class="table table-striped table-condensed table-bordered" width="100%">
                                <thead  class="mySearchTr">
                                <tr>
                                    <th style="width: 5%"></th>
                                    <th style="width: 10%;text-align: right;">Sr.</th>
                                    <th style="width: 35%;">Height</th>
                                    <th style="width: 40%;">Capacity</th>
                                </tr>
                                </thead>
                                <tbody id="tabledata">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            Echo "<tr>";
                                            Echo "<td align='center' valign='top'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                            Echo "<td><input type='text' value='$tran[srno]'  name='srno_$ctr'  class='form-control form text-right' style='text-align:right;' readonly></td>";
                                            Echo "<td>";
                                            Echo "<input type='text' value='$tran[height]' name='height_$ctr' class='form-control form height'>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' value='$tran[capacity]' name='capacity_$ctr' class='form-control form capacity'>";
                                            Echo "</td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $ctr = 0;
                                        $yctr = 0;
//                                        for ($i = $pctr; $i < 10; $i++) {
                                            $yctr++;
                                            echo "<tr>";
                                            Echo "<td align='center'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                            echo "<td  align='left'><input type='text' size='6' maxlength='6' name='srno_$ctr' class='sno form-control form' style='text-align:right' value='$yctr' readonly></td>";

                                            echo "<td align='center'><input type='text' size=''  name='height_$ctr' class='height form-control form' style='text-align:left;' value='' /></td>";
                                            echo "<td align='center'><input type='text' size=''  name='capacity_$ctr' class='capacity form-control form' style='text-align:left;' value='' /></td>";
                                            echo "</tr>";
                                            $ctr++;
//                                        }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
                            <button type="button" class="btn btn-success btn-square form"  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>
            </div>
                        
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </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">Tank No</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">Tank No</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</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:#1ABC9C;">
                                    <th width="25%">Tank No</th>
                                    <th width="10%">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>
