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

$formname = 'Dia Master';
$maindb = 'diamast';
$grid = '';
$keyfld = 'srno';
$file = 'diamaster.php';
$myyear = $_SESSION[$myyear];


$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"];
    browse($count);
    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"];
//            $tfractinch = $_POST["fractinch_$i"];
//            $tdeciinch = $_POST["deciinch_$i"];
//            $tmm = $_POST["mm_$i"];
            $tdia = $_POST["dia_$i"];
            $tshape = $_POST["shape_$i"];
            $tgrp = $_POST["grp_$i"];
//            browse($tsrno);
//            browse($tdia);
//            browse($tshape);

            if ($tdia > 0) {
                $ctn++;

                mysqli_query($conn,"INSERT INTO `$maindb`(`srno`,  `name`, `shape`, `grp`)
                                         VALUES( '$tsrno', '$tdia', '$tshape', '$tgrp')") or die(mysqli_error($conn));
            }
        }

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

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/diamaster.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: 13%; width: 12%; background-color:#DC143C;"  href="dia_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="col-lg">-->
                        <div class="table-responsive" style="max-height: 1710px;">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 38%">
                                <thead style="background: #77BCFF; color:white;">
                                    <tr>
                                        <th style="width: 3%"></th>
                                        <th style="width: 5%" class='text-right'>Sr.</th>
                                        <th style="width: 15%">Dia</th>
                                        <th style="width: 15%" class='text-right'>Shape</th>
                                        <th style="width: 12%" class='text-right'>Group By</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 dia' name='dia_$ctr' value='$tran[name]' ></td>";
                                        Echo "<td><input type='text' class='form-control form shape ' name='shape_$ctr' value='$tran[shape]'></td>";
                                        Echo "<td><input type='text' class='form-control form grp 'name='grp_$ctr' value='$tran[grp]'></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>
