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

$formname = 'item';
$maindb = 'item';
$trandb = 'itemtran';
$file = 'item.php';
$grid = '';
$keyfld = "code";
$trandbexist = 0;
$focusfld = "name";


$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       = trim($_POST["name"]);
    $itemgrp    = trim($_POST["itemgrp"]);
    $unit       = trim($_POST["unit"]);
    $rate       = trim($_POST["rate"]);
    $cost       = trim($_POST["cost"]);
    $mini       = trim($_POST["mini"]);
    
    
    mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$qvouch'") or die(mysqli_error($conn));
    for ($i = 0; $i < $count; $i++) {
        $msrno      = $_POST["srno_$i"];
        $mlocation  = $_POST["location_$i"];
        $mrate      = $_POST["qty_$i"];

        if (!empty($msrno)) {
            mysqli_query($conn, "INSERT INTO `$trandb`(vouchno, srno, location,  qty)
                                       VALUES('$qvouch', '$msrno', '$mlocation', '$mqty')") or die(mysqli_error($conn));
             header("refresh: 1; $file?$keyfld=$mvouch");
        }
    }

    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(code, `name`, `itemgrp`, `unit`, mini, cost, rate )
                           VALUES('$qvouch', '$name', '$itemgrp', '$unit', '$rate', '$cost', '$mini')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
             header("refresh: 1; $file?$keyfld=$mvouch");
        } else {
            $mvouch = $qvouch;
            
        
            
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name',`unit`='$unit', `itemgrp`='$itemgrp', `rate`='$rate', `cost`='$cost', 
                                     `mini`='$mini' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch");
        }
    }
}

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);
}



/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
include("includes/files.php");
?>

<script type='text/javascript'>
    $(document).ready(function() {
   
    function addrows() {
            var ctr = $('#items').val();
            var hide_check = "";
            if ($("#del_all").prop("checked")) {
                hide_check = 0;
            } else {
                hide_check = 1;
            }
            $.post('item_add.php', {ctr: ctr, hide: hide_check}, function(data) {
                $(data).appendTo('#data');
                ctr++;
                $('#items').val(ctr);
            });
        }

        $('#addnew').click(function() {
            addrows();
        });
    });
</script>
</head>
<form class="form-horizontal" action='' method='POST'>
    <div class="row">
        <div class="col-lg-12">
            <div class="box">
                <header>
                    <div class="icons">
                        <i class="fa fa-edit"></i>
                    </div>
                    <h5><?= $formname ?></h5>
                    <div class="toolbar">
                        <ul class="nav pull-right">
                            <li>
                                <?php include("includes/buttons.php"); ?>
                            </li>
                            <li>
                                <a class="minimize-box" data-toggle="collapse" href="#div-4">
                                    <i class="fa fa-chevron-up"></i>
                                </a>
                            </li>
                        </ul>
                    </div><!-- /.toolbar -->
                </header>
                <div id="div-4" class="accordion-body collapse in body">

                    <div class="form-group">
                        <label class="control-label col-lg-1">Item Name</label>
                        <div class="col-lg-5">
                            <div class="input-group">
                                <input type="text" size="40"  id="name" name="name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>"  class="form-control form" /> 
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-lg-1">Unit</label>
                        <div class="col-lg-8" style="width:150px;"> 
                            
                            <select id="unit" name="unit" class="form-control form" style="height: 33px;">
                                            <option value="<?php echo ($addflag == 0) ? $getsi['code'] : " "; ?>"><?php echo ($addflag == 0) ? $getsi['code'] : " "; ?></option>
                                            <?php
                                            $munit = mysqli_query($conn, "SELECT code, `name` FROM `unit` ") or die(mysqli_error($conn));
                                             while ($trow = mysqli_fetch_array($munit)) {
                                                echo "<option value=$trow[code]>$trow[name]</option>";
                                            }
                                            ?>
                                        </select>
                            <!--<input type="text" size="10" maxlength="6" id="unit" name="unit" value="<?php echo ($addflag == 0) ? $get['unit'] : ""; ?>" class="form-control" required tabindex="1" style ="width:60px;"/>-->                                       
                        </div> 
                    </div>
<!--                    <div class="form-group">
                        <label class="control-label col-lg-1">Item Group</label>
                        <div class="col-lg-8" style="width: 350px"> 
                            <select data-placeholder="Item" style="height:33px;" id="itemgrp" name="itemgrp" class="form-control form" >
                                <option value="<?php echo ($addflag == 0) ? $getiname['code'] : " "; ?>"><?php echo ($addflag == 0) ? $getiname["name"] : ""; ?></option>
                                <?php
                                $mown = mysqli_query($conn, "SELECT code, `name` FROM `itemgrp`") or die(mysqli_error($conn));
                                while ($trow = mysqli_fetch_array($mown)) {
                                    echo "<option value=$trow[code]>$trow[name]</option>";
                                }
                                ?>
                            </select>                                       
                        </div> 
                    </div>-->
<div class="form-group">
    <label class="control-label col-lg-1">Item Group</label>
    <div class="col-lg-8"> 
        <select id="itemgrp" name="itemgrp" class="form-control form" style="height: 33px;">
            <option value="<?php echo ($addflag == 0) ? $getp['code'] : " "; ?>"><?php echo ($addflag == 0) ? $getp['name'] : " "; ?></option>
            <?php
            $mown = mysqli_query($conn, "SELECT code, `name` FROM `itemgrp` ") or die(mysqli_error($conn));
            while ($trow = mysqli_fetch_array($mown)) {
                echo "<option value=$trow[code]>$trow[name]</option>";
            }
            ?>
        </select>
    </div>
</div>
                    <div class="form-group">
                        <label class="control-label col-lg-1">Cost Price</label>
                            <div class="col-lg-8" style="width:150px;">
                                <input type="text" size="40"  id="cost" name="cost" value="<?php echo ($addflag != 1) ? $get['cost'] : ''; ?>"  class="form-control form" /> 
                            </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-lg-1">Rate</label>
                            <div class="col-lg-8" style="width:150px;">
                                <input type="text" size="40"  id="rate" name="rate" value="<?php echo ($addflag != 1) ? $get['rate'] : ''; ?>"  class="form-control form" /> 
                            </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-lg-1">Minimum Stock</label>
                            <div class="col-lg-8" style="width:150px;">
                                <input type="text" size="40"  id="mini" name="mini" value="<?php echo ($addflag != 1) ? $get['mini'] : ''; ?>"  class="form-control form" /> 
                            </div>
                    </div>
                    
                    <br>
                    <table id="data" border="1px" width="100%" class="col-lg-6">

                        <tr>
                            <td width="40px" align="center"><label class="control-label">Sr No.</label></td>
                            <td width="250px" align="center"><label class="control-label">Location</label></td>
                            <td width="90px" align="center"><label class="control-label">Qty.</label></td>
                        </tr>
                        <?php
                        if ($addflag == 0) {
                            $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));

                            $ctr = 0;
                            while ($row = mysqli_fetch_array($qry)) {

                                $getloc = mysqli_query($conn, "SELECT * FROM `location` WHERE code='$row[name]'") or die(mysqli_error($conn));
                                $loc = mysqli_fetch_object($getitem);
                                echo "<tr>";
                                /* echo '<td align="center" valign="top"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>'; */
                                echo "<td align='left'><input type='text' size='6' maxlength='6' maxlength='6' name='srno_$ctr' class='sno form-control form' value='$row[srno]' style='height:33px;'/></td>";
                                echo "<td align='left'>";
                                echo "<select  name='name_$ctr' class='form-control form' style='height:33px;'>";
                                echo "<option value='$item->code'>$loc->name</option>";
                                $cc = mysqli_query($conn, "SELECT code, `name` FROM `location` WHERE  ORDER BY `name`") or die(mysqli_error($conn));
                                while ($trow = mysqli_fetch_array($cc)) {
                                    echo "<option value=$trow[code]>$trow[name]</option>";
                                }
                                echo "</select>";
                                echo "</td>";
                                echo "<td align='center' valign='top'><input type='text' name='qty_$ctr' class='form-control form' value='$row[rate]' style='text-align:right;height:33px;'/></td>";
                                echo"</tr>";
                                $ctr++;
                            }
                            //browse($ctr);
                        } else {
                            ?>
                            <tr>
    <!--                                                            <td align="center" valign="top"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>-->
                                <td align="left" valign="top"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_0" class="sno form-control form" style="height:33px;" value="1"/></td>
                                <td align="left">
                                    <select name="name_0" class="form-control form" style="height:33px;">
                                        <option value=""></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT code, `name` FROM `location`") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[code]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </td>

                                <td align="center" valign="top"><input type="text" size="6" maxlength="9" name="qty_0" class="form-control form" style='text-align:right;height:33px;'/></td>
                            </tr>
                        <?php } ?>
                    </table>
                    
                    <table border='0' width='100%'>
                        <tr>
                            <td width="150px">
                                <input type="button" id="addnew" class="btn btn-success btn-lg" name="addnew" value="+" />
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag == 0) ? $ctr : 1; ?>" />  
                            </td><td width="240px"></td><td width="140px"></td><td width="200px" align="right"><label for=""><font color="black" size="3px"></font><span></span></label></td>
                            <td width="200px"></td>
                        </tr>
                    </table>
                </div>
                
                
                
            </div>
            <!--SEARCH PART-->

            <div class="modal fade in slacker-modal"  role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
                <div class="modal-dialog modal-slacker">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myLargeModalLabel"></h4>
                        </div>
                        <div class="modal-body" style="height: 100%;white-space: nowrap;">
                            <div class="row">

                                <div class="col-md-12">

                                    <div class="col-lg-12">

                                        <div class="box">
                                            <div id="collapse4" class="body">
                                                <table id="dataTable" class="table table-bordered table-condensed table-hover table-striped">
                                                    <thead>
                                                        <tr>
                                                            <th align='left'>Name</th>
                                                            <th align='left'>Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!=''") or die(mysqli_error($conn));
                                                        while ($row = mysqli_fetch_array($srchresult)) {
                                                            echo "<tr>";
                                                            echo "<td align='left'>$row[name]</td>";
                                                            echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><img src='images/edit.png'></a></td>";
                                                            echo "</tr>";
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                                <script>
                                                    $(function() {
                                                        metisTable();
                                                        metisSortable();
                                                    });
                                                </script>

                                            </div>

                                        </div>

                                    </div>

                                </div><!-- /.row -->

                            </div>
                        </div>  
                        <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                            <!--"This is where i want to load my salemain.php"-->
                        </div>
                    </div>
                </div>
            </div>            
        </div>
    </div>
</form>
<?php
include("includes/files_footer.php");
?>
<script>
    $(function() {
        formGeneral();
    });
</script>