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

$formname = 'Opening Stock';
$maindb = 'transacn';
$source = "OP";
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);



$dcode = mysqli_real_escape_string($conn, $_GET["code"]);
$colorcode = "#FF8C00";
$result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `group` = '$dcode' AND source='$source' ") or die(mysqli_error($conn));
?>

<div id="sortableTable" class="body collapse in">
    <table class="table table-bordered ">
        <thead>
            <tr>
                <th>SR.
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th style="width: 370px;">NAME
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>5
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>6
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>7
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>8
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>9
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>10
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>11
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>12
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>OTHER
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>TOTAL 
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
                <th>RATE
                    <i class="icon-sort"></i>
                    <i class="icon-sort-down"></i>
                    <i class="icon-sort-up"></i>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php
            $ctr = 0;
            $mctr = 1;

            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_object($result)) {
                    $ctr++;
                    echo "<tr>";
                    echo "<td>";
                    echo $ctr;
                    echo "</td>";
                    echo "<td>";
                    echo "$row->name";
                    echo "<input type='hidden' name='code_$mctr' value='$row->code' class='form-control' >";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s5_$mctr' value='$row->s5' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s6_$mctr' value='$row->s6' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s7_$mctr' value='$row->s7' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s8_$mctr' value='$row->s8' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s9_$mctr' value='$row->s9' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s10_$mctr' value='$row->s10' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s11_$mctr' value='$row->s11' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='s12_$mctr' value='$row->s12' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='soth_$mctr' value='$row->soth' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='qty_$mctr' value='$row->qty' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "<td>";
                    echo "<input type='text' name='rate_$mctr' value='$row->rate' class='form-control' style='text-align:right'>";
                    echo "</td>";
                    echo "</tr>";
                    $mctr++;
                }
                echo "<input type='hidden' name='count' value='$ctr'>";
            } else {

                echo "<tr>";
                echo "<td>";
                echo $ctr;
                echo "</td>";
                echo "<td>";
                echo "<input list='item' name='item_$ctr' class='item form-control' value='$row[item]' style='height:33px;'>";
                echo "<datalist id='item'>";
                echo "<option value=''></option>";
                $mitem = mysqli_query($conn, "SELECT `name` FROM `item`") or die(mysqli_error($conn));
                while ($trow = mysqli_fetch_array($mitem)) {
                    echo "<option value=$trow[name]>";
                }
                echo "</datalist>";

                echo "<input type='hidden' name='code_$mctr' value='' class='form-control' >";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s5_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s6_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s7_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s8_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s9_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s10_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s11_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='s12_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='soth_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='qty_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' name='rate_$mctr' value='' class='form-control' style='text-align:right'>";
                echo "</td>";
                echo "</tr>";
            }

            echo "</table>";
            ?>
