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

$formname = 'Group Master';
$maindb = 'groupmast';
$trandb = 'grouptran';
$printfile = "groupmast_print.php";
$file = 'groupmast.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";

$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"];


    $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) {

        mysqli_query($conn,"DELETE from `$trandb` where `code`='$mvouch'") or die(mysqli_error($conn));

        for ($i = 0; $i <= $count; $i++) {
            $srno = $_POST["srno_" . $i];
            $memcode = $_POST["memcode_" . $i];
            $brno = $_POST["brno_" . $i];
            if ($memcode != '') {

                mysqli_query($conn,"INSERT INTO `$trandb`(`srno`, `code`, `partgrp`, `memcode`, `brno`)
                                        VALUES('$srno', '$qvouch', '$qvouch', '$memcode', '$brno')") or die(mysqli_error($conn));
            }
        }


        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(code, `name`)
                                        VALUES($qvouch, '$name')") 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' 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 $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

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

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

    $resperf = mysqli_query($conn,"SELECT `grpmast` FROM `member` WHERE `grpmast`='$getvouch'") or die(mysqli_error($conn));
    $perfet = mysqli_num_rows($resperf);
    if ($perfet > 0) {
        browse("GROUP MASTER AVAILABLE IN MEMBER");
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
}
//if (isset($_GET["delete"])) {
//    $resperf = mysqli_query($conn,"SELECT `itemgrp` FROM `item` WHERE `itemgrp`='$getvouch'") or die(mysqli_error($conn));
//    $perfet = mysqli_num_rows($resperf);
//    if ($getvouch == 1) {
//        $resperf1 = mysqli_query($conn,"SELECT `code`,`name` FROM `$maindb` WHERE `code`='$getvouch'") or die(mysqli_error($conn));
//        $perfet1 = mysqli_num_rows($resperf1);
//    }
//    browse($perfet1);
//    if ($perfet1 > 0) {
//        browse("ITEM GROUP - PERFUME IS FIXED CANNOT BE DELETED");
//        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
//    } else if ($perfet > 0) {
//        browse("ITEM GROUP AVAILABLE IN ITEM");
//        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
//    } else {
//        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    }
//}

include("includes/linkshead.php");
?>
<script>

    $(document).ready(function () {
        $("body").on("change", ".member", getbrno);
        $("body").off('click').on("click", '.remove', remove);


        function getbrno() {
            var tr = $(this).closest("tr");
            var member = tr.find('.member').val();
            $.post("getgroup.php", {member: member}, function (data) {
                tr.find('.brno').val(data.brno);
            }, 'json');
        }

        $('#addnew').click(function () {
            AddRows();
        });

        function remove() {
            $(this).closest("tr").remove();
            calMain();
            calDetail();
            caltran();
        }

        function AddRows() {
            var ctr = $('#items').val();
            var prodtype = $('.prodtype').val();
            var hide_check = "";
            if ($("#del_all").prop("checked")) {
                hide_check = 0;
            } else {
                hide_check = 1;
            }
        var detail = ".fetchpart_" + ctr;
            var focusFld = ".focusFld_" + ctr;
            $.post('groupmast_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
                $(data).appendTo('.tbody');
                ctr++;
                $('#items').val(ctr);
            $(detail).select2();
                $(focusFld).focus();
            });
        }}
    );




</script>
<!--<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="form-group">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</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>
                    <!--<div class="panel-body">-->
                    <div class="row">
                        <div class="table-responsive" style="margin-left: 23px;">
                            <table class="table-striped table-condensed table-bordered table-padd" width="95%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="60%">Member Name</th>
                                        <th width="15%">Br. No.</th>

                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn,"SELECT * FROM `$trandb` where `partgrp`= '$get[code]' order by srno")or die(mysqli_error($conn));

                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $finditem = mysqli_query($conn,"SELECT accode,name FROM `member` WHERE `accode`='$tran[memcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);
//                                            $findunit = mysqli_query($conn,"SELECT * FROM `unit` WHERE `code`='$tran[unit]'") or die(mysqli_error($conn));
//                                            $funit = mysqli_fetch_array($findunit);

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form fetchpart_$ctr member' name='memcode_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[accode]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn,'SELECT accode, name FROM `member` order by name')or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[accode]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form brno'       name='brno_$ctr'         value='$tran[brno]'></td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                            <td>
                                                <select class="form-control form member select2 " name="memcode_<?= $pctr ?>" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn,"SELECT accode, name FROM `member` order by name")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[accode]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form brno"     name="brno_<?= $pctr ?>"        value=""></td>

                                        </tr>  
                                        <?
                                        $pctr++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad" style="padding-left: 5%">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  
                            <input type="hidden" class="mandatoryId" name="manId" value="">  
                        </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" style="background-color: #B79EC6;">
                                    <th>Name</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)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[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>
