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

$admin = $_SESSION["admin_" . $dbname];

$maindb = 'transacn';

$array = '';
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_REQUEST['cmbgrp'])) {
    $cmbgrp = mysqli_real_escape_string($conn, $_REQUEST["cmbgrp"]);
    print $cmbgrp;

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `itemgrp` = '$cmbgrp' AND `source`='OP' ORDER BY `srno`") or die(mysqli_error($conn));
    ?>
    <table id="data" align="center" border="1px" width="100%" class="table table-bordered">

        <tr>
            <td width="60px"  align="center"><label class="control-label">Sr.</label></td>
            <td width="250px" align="center"><label class="control-label">Item Name</label></td>    
            <td width="160px" align="center"><label class="control-label">Colour</label></td>
            <td width="100px" align="center"><label class="control-label">5</label></td>
            <td width="100px" align="center"><label class="control-label">6</label></td>
            <td width="100px" align="center"><label class="control-label">7</label></td>
            <td width="100px" align="center"><label class="control-label">8</label></td>
            <td width="100px" align="center"><label class="control-label">9</label></td>
            <td width="100px" align="center"><label class="control-label">10</label></td>
            <td width="100px" align="center"><label class="control-label">11</label></td>
            <td width="100px" align="center"><label class="control-label">12</label></td>
            <td width="100px" align="center"><label class="control-label">Other</label></td>
            <td width="120px" align="center"><label class="control-label">Total Qty.</label></td>
            <td width="120px" align="center"><label class="control-label">MRP</label></td>
            <td width="120px" align="center"><label class="control-label">Rate</label></td>
         <!--   <td width="120px" align="center"><label class="control-label">Amount</label></td>-->
        </tr>



        <?php
        if (mysqli_num_rows($result) > 0) {
            $ctr = 0;
            while ($row = mysqli_fetch_array($result)) {
                $getitem = mysqli_query($conn, "SELECT  name FROM `item` WHERE code='$row[itemcode]'") or die(mysqli_error($conn));
                $gitem = mysqli_fetch_object($getitem);

                $getcolr = mysqli_query($conn, "SELECT * FROM `color` WHERE code='$row[color]'") or die(mysqli_error($conn));
                $colr = mysqli_fetch_object($getcolr);

                echo "<tr>";
                echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno_$ctr' class='sno form-control' style='height:33px;' value='$row[srno]'/></td>";
                echo "<td align='left'>";
                echo "<input list='item' name='item_$ctr' class='item form-control' value='$gitem->name' style='height:33px;'>";

                echo "<datalist id='item'>";
                echo "<option value=''></option>";
                $mitem = mysqli_query($conn, "SELECT `name` FROM `item` WHERE `group` = '$cmbgrp' order by name") or die(mysqli_error($conn));
                while ($trow = mysqli_fetch_array($mitem)) {
                    echo "<option value=$trow[name]>";
                }
                echo "</datalist>";
                echo "</td>";

                echo "<td align='center' valign='top'>";
                echo "<select name='color_$ctr' style='height:34px;' class='form-control'>";
                echo "<option value='$colr->code'>$colr->name</option>";
                $getcolors = mysqli_query($conn, "SELECT `code`, `name` FROM `color` order by name ") or die(mysqli_error($conn));
                while ($wcol = mysqli_fetch_array($getcolors)) {
                    echo "<option value='$wcol[code]' >$wcol[name]</option>";
                }

                echo "</select>";
                echo "</td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4' name='s5_$ctr'  class='s5 form-control' style='text-align:right;height:33px;' value='$row[s5]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s6_$ctr' class='s6 form-control' style='text-align:right;height:33px;' value='$row[s6]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s7_$ctr' class='s7 form-control'  style='text-align:right;height:33px;' value='$row[s7]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s8_$ctr' class='s8 form-control'  style='text-align:right;height:33px;' value='$row[s8]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s9_$ctr' class='s9 form-control'  style='text-align:right;height:33px;' value='$row[s9]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s10_$ctr' class='s10 form-control'  style='text-align:right;height:33px;' value='$row[s10]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s11_$ctr' class='s11 form-control'  style='text-align:right;height:33px;' value='$row[s11]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s12_$ctr' class='s12 form-control'  style='text-align:right;height:33px;' value='$row[s12]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='soth_$ctr' class='soth form-control'  style='text-align:right;height:33px;' value='$row[soth]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4'  name='qty_$ctr' class='qty form-control'  style='text-align:right;height:33px;' value='$row[qty]' readonly/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' name='mrp_$ctr' class='mrp form-control'  style='text-align:right;height:33px;' value='$row[mrp]'/></td>";
                echo "<td align='center' valign='top'><input type='text' size='4' name='rate_$ctr' class='rate form-control'  style='text-align:right;height:33px;' value='$row[rate]'/>
                                                  <input type='hidden' size='4'  name='amt_$ctr' class='amt form-control'  style='text-align:right;height:33px;' value='$row[amount]' readonly/></td>";

                echo "</tr>";
                $ctr++;
            }
            ?><input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>" />
            <?php
        } else {
            $ctr = 1;
            ?>

            <tr>
                <td align="left" valign="top"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_0" class="sno form-control" style="height:33px;" value="1"/></td>
                <td align="left">
                    <input list="item" name="item_0"  class="item form-control" style="height:33px;">
                    <datalist id="item"> 
                        <option value=""></option>
                        <?php
                        $mitem = mysqli_query($conn, "SELECT `name` FROM `item` WHERE `group` = '$cmbgrp' order by name ") or die(mysqli_error($conn));
                        while ($trow = mysqli_fetch_array($mitem)) {
                            echo "<option value=$trow[name]>";
                        }
                        ?>
                    </datalist>
                </td>

                <td align="center" valign="top">
                    <select name="color_0" class="color form-control" style='height:34px;'> 
                        <option value=""></option>
                        <?php
                        $getcolors = mysqli_query($conn, "SELECT `code`, `name` FROM `color` order by name") or die(mysqli_error($conn));
                        while ($wcol = mysqli_fetch_array($getcolors)) {
                            echo "<option value='$wcol[code]' >$wcol[name]</option>";
                        }
                        ?>
                    </select>
                </td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4" name="s5_0"  class="s5 form-control" style='text-align:right;height:33px;' value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s6_0" class="s6 form-control" style='text-align:right;height:33px;' value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s7_0" class="s7 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s8_0" class="s8 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s9_0" class="s9 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s10_0" class="s10 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s11_0" class="s11 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s12_0" class="s12 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="soth_0" class="soth form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4"  name="qty_0" class="qty form-control"  style="text-align:right;height:33px;" value="0" readonly/></td>
                <td align="center" valign="top"><input type="text" size="4"  name="mrp_0" class="mrp form-control"  style="text-align:right;height:33px;" value="0"/></td>
                <td align="center" valign="top"><input type="text" size="4"  name="rate_0" class="rate form-control"  style="text-align:right;height:33px;" value="0"/>
                    <input type="hidden" size="4"  name="amt_0" class="amt form-control"  style="text-align:right;height:33px;" value="0" readonly />
                </td>

            </tr>
        </table>
        <input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>" />
        <?php
    }
    ?>

    <?php
}
?>
