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

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

if (isset($_REQUEST['prodcode'])) {
    $prodcode = $_REQUEST['prodcode'];
    $prodqty = $_REQUEST['qty'];
    // browse($prodqty);
    $gettran = mysqli_query($conn, "SELECT * FROM `itemtran` WHERE itemcode = '$prodcode' ORDER BY srno") or die(mysqli_error($conn));
    $count = mysqli_num_rows($gettran);
    $ctr = 0;

    if ($count > 0) {
        $meters = 0;
        $yard = 0;
        $qty = 0;
        while ($tran = mysqli_fetch_array($gettran)) {
            $finditem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[material]'") or die(mysqli_error($conn));
            $fitem = mysqli_fetch_array($finditem);

            $findcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
            $fcolor = mysqli_fetch_array($findcolor);
            if($tran['meters'] > 0){
                $meters = $tran['meters'] * $prodqty;
            }
            if($tran['yard'] > 0){
                $yard = $tran['yard'] * $prodqty;
            }
            if($tran['qty'] > 0){
                $qty = $tran['qty'] * $prodqty;
            }

            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 item select2' name='item_$ctr' style='width:100%'>";
            echo "<option value='$fitem[code]'>$fitem[name]</option>";
            $gitem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` order by name") or die(mysqli_error($conn));
            while ($item = mysqli_fetch_array($gitem)) {
                echo "<option value='$item[code]'>$item[name]</option>";
            }
            echo "</select>";
            echo "</td>";
            echo "<td>";
            echo "<select class='form-control form color select2' name='color_$ctr' style='width:100%'>";
            echo "<option value='$fcolor[code]'>$fcolor[name]</option>";
            $gcolor = mysqli_query($conn, 'SELECT `code`,`name` FROM `color` order by name') or die(mysqli_error($conn));
            while ($color = mysqli_fetch_array($gcolor)) {
                echo "<option value='$color[code]'>$color[name]</option>";
            }
            echo "</select>";
            echo "</td>";
            echo "<td><input type='text' class='form-control form text-right meters' name='meters_$ctr'  value='$meters'></td>";
            echo "<td><input type='text' class='form-control form text-right yard'   name='yard_$ctr'    value='$yard'></td>";
            echo "<td><input type='text' class='form-control form text-right tr_qty'    name='qty_$ctr'     value='$qty'></td>";
            echo "</tr>";
            $ctr++;
        }

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

<?php
}
