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

$formname = 'Change of Rates';
$maindb = 'product';
$file = 'openstock.php';
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];


$itemtype = $_GET['itemtype'];
$pouchflag = $_GET['pouchflag'];


$itemtypecond = "";
$pouchcond = "";

if (!empty($itemtype)) {
    $itemtypecond = " and `type`='$itemtype' ";
}

if (!empty($pouchflag)) {
    if ($pouchflag == 1) {
        $pouchcond = " and `pouchflag`='1' ";
    } else {
        $pouchcond = " and `pouchflag`!='1' ";
    }
}
//PRINT "SELECT * FROM `item` where `code`!='' $itemtypecond $subgrpcond  ORDER BY name";
$colorcode = "#FF8C00";
$result = mysqli_query($conn, "SELECT * FROM `$maindb` where `code`!='' $itemtypecond $pouchcond ORDER BY `name` ") or die(mysqli_error($conn));
$ont = mysqli_num_rows($result);

$flag = "";

$ctr = 0;
?>
<!--<table class="table-condensed" style="margin: 0 auto;">
    <tr>
        <td>
            <input id="submit" type='button' name='submit'  value='Save   ' class="btn btn-success">
        </td>
        <td>
            <input class="btn btn-danger" type="button" onClick="window.location.href = '<?= $file ?>'" value="Exit   " />
        </td>
    </tr>
</table>-->
<?php
echo '<table id="openstock" border="1" width="90%" align="center">';
?>
<tr class="mySearchTr">
    <th width="5%" align="center"><label class="control-label"><b>Sr.</b></label></th>
    <th width="10%" align="center"><label class="control-label"><b>Barcode</b></label></th>
    <th width="25%" align="center"><label class="control-label"><b>Name</b></label></th>
    <th width="10%" align="center"><label class="control-label"><b>Type</b></label></th>
    <th width="10%" align="center"><label class="control-label"><b>Stock Unit</b></label></th>
    <th width="10%" align="center"><label class="control-label"><b>Min. Stock</b></label></th>
    <th width="10%" style="text-align:right"><label class="control-label"><b>Cost</b></label></th>
    <th width="10%" style="text-align:right"><label class="control-label"><b>Retail Rate</b></label></th>
    <th width="10%" style="text-align:right"><label class="control-label"><b>Online Rate</b></label></th>
    <!--    <td width="10%" align="center"><label class="control-label"><b>MSP</b></label></td>
    <td width="10%" align="center"><label class="control-label"><b>MRP</b></label></td>-->
    <!--<td width="10%" align="center"><label class="control-label"><b>Opening</b></label></td>-->
</tr>
<?php
$mctr = 0;
while ($row = mysqli_fetch_object($result)) {
    
    $gunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE `code`='$row->stkunit'") or die(mysqli_error($conn));
    $funit = mysqli_fetch_array($gunit);

    $ctr++;
    echo "<tr>";
    echo "<td align='center'>";
    echo $ctr;
    echo "</td>";
    echo "<td>";
    echo "<input type='text' class='form-control' name='barcode_$mctr' value='$row->barcode' style='text-align:left;' readonly tabindex='-1'>";
    echo "</td>";
    echo "<td  align='center'>";
    echo "<input type='text' class='form-control' name='name_$mctr' value='$row->name' style='text-align:left;' readonly tabindex='-1'>";
    echo "</td>";
    echo "<td  align='center'>";
    echo "<input type='text' class='form-control' name='type_$mctr' value='$row->type' style='text-align:left;' readonly tabindex='-1'>";
    echo "</td>";
    echo "<td  align='center'>";
    echo "<select class='form-control form type select2' name='stkunit_$mctr'>";
    echo "<option value='$funit[code]'>$funit[name]</option>";
    $getunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE `code`!=''") or die(mysqli_error($conn));
    while ($fetunit = mysqli_fetch_array($getunit)) {
        echo "<option value='$fetunit[code]'>$fetunit[name]</option>";
    }
    echo "</select>";
    echo "</td>";
    echo "<td  align='center'>";
    echo "<input type='text' class='form-control' name='minstk_$mctr' value='$row->minstk' style='text-align:left;'>";
    echo "</td>";
    echo "<td>";
    echo "<input type='text' class='form-control cost' name='cost_$mctr' value='$row->cost' style='text-align:right;'>";
    echo "</td>";
    echo "<td>";
    echo "<input type='text' class='form-control rate' name='selling_$mctr' value='$row->rate' style='text-align:right;'>";
    echo "</td>";
    echo "<td>";
    echo "<input type='text' class='form-control' name='mrp_$mctr' value='$row->mrp' style='text-align:right;'>";
    echo "</td>";
    ////    echo "<td>";
////    echo "<input type='text' class='form-control' name='msp_$mctr' value='$row->msp' style='text-align:right;'>";
////    echo "</td>";
////    echo "<td>";
//    echo "<input type='text' class='form-control' name='mrp_$mctr' value='$row->mrp' style='text-align:right;'>";
//    echo "</td>";
//    echo "<td>";
//    echo "<input type='text' class='form-control' name='opening_$mctr' value='$row->opening' style='text-align:right;'>";
//    echo "</td>";
    echo "</tr>";

    $mctr++;
}
echo "<input type='hidden' name='count' value='$mctr'>";
echo "</table>";
?>