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

$formname = 'Item Part';
$maindb = 'itempart';
$keyfld = "code";

$file = 'itempart.php';
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
$fseries = $_SESSION["series_" . $dbname];
$stkdb1 = 'tran';
$zroot = $_SESSION["zdir"];

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

if (isset($_POST["submit"])) {
    $a = 1;
    $name = $_POST["name"];
    $mcode = $_POST["code"];

    if ($a == 1) {
        if ($mcode == '') {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` where $keyfld != '' $keycond LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_object($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
            mysqli_query($conn, "INSERT INTO $maindb(`code`, `name`)
                                        VALUES('$mvouch', '$name')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $mcode;
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name' WHERE `code` = '$mvouch'") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

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

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code!='' ORDER BY `code`") or die(mysqli_error($conn));
}

if (isset($_GET["delete"])) {

    $resperf = mysqli_query($conn, "SELECT `itempart` FROM `itemsize` WHERE `itempart`='$getvouch'") or die(mysqli_error($conn));
    $perfet = mysqli_num_rows($resperf);
    if ($perfet > 0) {
        browse("ITEM PART AVAILABLE IN ITEM");
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");
    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/itempart.js" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>

</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-lg-6 col-md-6">
                                <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <a href="#" class="addnew btn mySearchTr" style="color:white">Add New</a>
                                </h3>
                            </div>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row additem" style="display: none">
                            <div class="col-lg-1 col-md-1">
                                <label class="control-label text-right">Name</label>
                            </div>
                            <div class="col-lg-4 col-md-4">
                                <input type="text" step="any" class="form-control name" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>"> 
                                <input type="hidden" step="any" class="form-control code" name="code" value=""> 
                            </div>
                            <div class="col-lg-2 col-md-2">
                                <input type="submit" name="submit" class="form-control mySearchTr btn">
                            </div>
                            <div class="col-lg-2 col-md-2">
                                <a href="itempart.php?mode=last">
                                    <input type="button" name="goback" class="form-control btn" value="Go Back" style="background-color: #ffcc99;">
                                </a>
                            </div>
                        </div>
                        <br><br>
                        <table class="table table-bordered" style="width:80%">
                            <tr class="mySearchTr">
                                <th width="5%">Sr.</th>
                                <th width="20%">Item Part</th>
                                <th width="5%">Edit</th>
                                <th width="5%">Delete</th>
                            </tr>
                            <?php
                            $ctr = 0;
                            while ($get = mysqli_fetch_array($result)) {
                                $ctr++;
                                $code = $get['code'];
                                $name = $get['name'];
                                ?>
                                <tr>
                                    <td><?= $ctr ?></td>
                                    <td><?= $name ?><input type="hidden" name="trcode" class="trcode" value="<?= $code ?>"><input type="hidden" name="trname" class="trname" value="<?= $name ?>"></td>
                                    <td class="text-center"><i class="fa fa-pencil editrec" style="font-size: 20px; color: lightblue"></i></td>
                                    <td class="text-center"><i class="fa fa-trash delrec" style="font-size: 20px; color: lightcoral"></i></td>
                                </tr>
                            <? } ?>
                        </table>   
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
