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

$formname = 'Item Sub Group';
$maindb = 'itemsubgrp';

$printfile = "itsubgrp_print.php";
$file = 'itsubgrp.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$compcd = $_SESSION["comp_" . $dbname];

//$maindb1 = 'apps4rea_msp_1_' . $myyear;
//$maindb2 = 'apps4rea_msp_2_' . $myyear;
//$maindb3 = 'apps4rea_msp_3_' . $myyear;

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

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


    $mvouch = $qvouch;

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

    if ($a == 1) {


        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO $maindb(code, `name`, `itemgrp`)
                                        VALUES($qvouch, '$name', '$itemgrp')") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$maindb1`.$maindb(code, `name`, `itemgrp`)
//                                        VALUES($qvouch, '$name', '$itemgrp')") or die(mysqli_error($conn));
//
//            mysqli_query($conn, "INSERT INTO `$maindb2`.$maindb(code, `name`, `itemgrp`)
//                                        VALUES($qvouch, '$name', '$itemgrp')") or die(mysqli_error($conn));
//
//            mysqli_query($conn, "INSERT INTO `$maindb3`.$maindb(code, `name`, `itemgrp`)
//                                        VALUES($qvouch, '$name', '$itemgrp')") 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 `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));

//            if ($compcd == 1) {
//                mysqli_query($conn, "UPDATE `$maindb2`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE `$maindb3`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 2) {
//                mysqli_query($conn, "UPDATE `$maindb1`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE `$maindb3`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 3) {
//                mysqli_query($conn, "UPDATE `$maindb1`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE `$maindb2`.$maindb SET `name`='$name', `itemgrp`='$itemgrp' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        if ($addflag == 1) {
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            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 $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $finditemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code`='$get[itemgrp]'") or die(mysqli_error($conn));
    $fitemgrp = mysqli_fetch_array($finditemgrp);
}

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


if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
</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'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <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="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Item Sub Group</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                                <label class="label-control marg">Item Group</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control form select2" name="itemgrp" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $fitemgrp['code'] : ""; ?>"><? echo ($addflag != 1) ? $fitemgrp['name'] : ""; ?></option>
                                    <?php
                                    $gitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` ")or die(mysqli_error($conn));
                                    while ($itemgrp = mysqli_fetch_object($gitemgrp)) {
                                        Echo "<option value='$itemgrp->code'>$itemgrp->name</option>";
                                    }
                                    ?>
                                </select>
                            </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" 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">
                                    <th>Item Sub Group</th>
                                    <th>Item Group</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)) {
                                        $mitemgrp = mysqli_query($conn, "SELECT `code`,`name` FROM `itemgrp` WHERE `code`='$row[itemgrp]' ") or die(mysqli_error($conn));
                                        $fitgrp = mysqli_fetch_array($mitemgrp);


                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'>$fitgrp[name]</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>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
