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

$formname = 'Guage Metric';
$maindb = 'guage';

$grid = '';
$keyfld = "code";


$file = 'guage.php';

$myyear = $_SESSION[$myyear];

$zroot = $_SESSION['zdbroot'];

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

if (isset($_POST["submit"])) {
    $a = 1;
//    $name = $_POST["name"];
    $count = $_POST["items"];

    if ($a == 1) {

        #################################TRAN PART #################################
        mysqli_query($conn,"DELETE FROM `$maindb` ") or die(mysqli_error($conn));
        $ctn = 1;
        for ($i = 0; $i <= $count; $i++) {

            $tsrno = $_POST["srno_$i"];
            $tguage = $_POST["guage_$i"];
            $tmm = $_POST["mm_$i"];



            if ($tguage > 0) {
                $ctn++;
//                checking already exist
//                $getmyitem = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE `fractinch`='$tfractinch' and `deciinch`='$tdeciinch' and `mm`='$tmm'") or die(mysqli_error($conn));
//                $openrows = mysqli_num_rows($getmyitem);
//
//                if ($openrows > 0) {
////                    browse($miname . "  with size  " . $msize . "  Already Exist");
////                    mysqli_query($conn,"UPDATE `$maindb` SET `opstk`='$mopening', `minstk`='$mminimum'
////                                WHERE `itemname` = '$miname' and `size`='$msize'  AND `series` = '$zseries' ") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn,"INSERT INTO `$maindb`(`code`, `srno`,  `fractinch`, `deciinch`,`mm`)
//                                         VALUES('$mvouch', '$tsrno', '$tfractinch', '$tdeciinch','$tmm')") or die(mysqli_error($conn));
//                }

                mysqli_query($conn,"INSERT INTO `$maindb`(`srno`,  `guage`,`mm`)
                                         VALUES( '$tsrno', '$tguage', '$tmm')") or die(mysqli_error($conn));
            }
        }

//############################## MAIN  ######################################################
        if ($addflag == 1) {
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
$get = mysqli_query($conn,"SELECT * FROM `$maindb` ") or die(mysqli_error($conn));
include("includes/linkshead.php");
?>
<script src="MyScript/guage.js?a=1" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color :#05B2EA;"><?php Echo $formname; ?></h3>
                        <a type="button" style="margin-left: 20%; width: 14%; background-color:crimson;" href="gagmet_print.php" class="btn"  id="print" target="_blank">
                            <span class="" style="color:#ffffff; font-weight: bold;" data-toggle='tooltip' title='Print'>PRINT</span>   
                        </a>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive" style="max-height: 1710px;overflow-y: auto;">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 40%">
                                <thead style="background: #77BCFF; color:white;">
                                    <tr>
                                        <th style="width: 3%"></th>
                                        <th style="width: 5%" class='text-right'>Sr.</th>
                                        <th style="width: 10%">Guage</th>
                                        <th style="width: 12%" class='text-right'>MM</th>
                                    </tr>
                                </thead>
                                <tbody class="tabledata" id="data">
                                    <?php
                                    $ctr = 0;
                                    $disnon = '';
                                    if ($addflag != 1) {
                                        $disnon = 'display: none;';
                                    }
                                    while ($tran = mysqli_fetch_array($get)) {
                                        Echo "<tr>";
                                        Echo "<td><i class='fa fa-close remove' style='$disnon'></i></td>";
                                        Echo "<td><input type='text' class='form-control form srno text-right'      name='srno_$ctr' value='$tran[srno]' readonly></td>";
                                        Echo "<td><input type='text' class='form-control form guage'   name='guage_$ctr' value='$tran[guage]'></td>";
                                        Echo "<td><input type='text' class='form-control form mm'         name='mm_$ctr' value='$tran[mm]'></td>";
                                        Echo "</tr>";
                                        $ctr++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
                            <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 : $ctr; ?>" />  
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div style="overflow: auto; ">
                            <table class="table-condensed" style="margin: 0 auto;">
                                <tr>
                                    <td class=" buttonform tooltip-bottom" id="hedit" style="display: 
                                        <? Echo $editdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Edit Record">
                                        <button id='edit' type="button" style="background-color:#F0AD4E; " class="btn btn-pad btn-square">
                                            <i class="fa fa-pencil" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="hexit" style="display: <? Echo $exitdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Exit">
                                        <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:silver; border-color: #CA195A; "  onClick="window.location.href = '<?= 'default.php' ?>'">
                                            <i class="fa fa-power-off" style="color:#CA195A"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: 
                                        <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                                        <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                            <i class="fa fa-check" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="scancel" style="display: 
                                        <? Echo $canceldisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Record">
                                        <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:#39CCCC; "  onClick="window.location.href = '<?= $file . "?mode=last&series=$zseries" ?>'">
                                            <i class="fa fa-times" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
