<?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['salevcno'])) {
    $salevcno = $_REQUEST['salevcno'];


    $qry = mysqli_query($conn, "SELECT * FROM  `saletran` WHERE vouchno='$salevcno' ORDER BY srno") or die(mysqli_error($conn));
//    print table($qry);
    $ctr = 0;
    while ($row = mysqli_fetch_array($qry)) {
        if ($row['calby'] == "P") {
            $calname = "Pcs";
        } elseif ($row['calby'] == "S") {
            $calname = "Sq.ft";
        } elseif ($row['calby'] == "B") {
            $calname = "Box";
        } else {
            $calname = "";
        }

        Echo "<tr>";
        Echo "<td><i class='fa fa-close remove' style=''></i></td>";
        Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$row[srno]'></td>";
        Echo "<td>";
        echo "<input list='itemname' name='itemname_$ctr' class='itemname form-control form ' autocomplete='off' value='$row[itemname]'> ";
        echo "<datalist id='itemname' char='#'>";
        echo"<option>";

        $msize = mysqli_query($conn, "SELECT `name` FROM `product` group by name") or die(mysqli_error($conn));

        while ($tsize = mysqli_fetch_array($msize)) {
            $sizename = $tsize['name'];
            echo "<option value='$sizename'>";
        }
        echo "</datalist>";
        echo "</td>";
        Echo "<td>";
        echo "<input list='size' name='size_$ctr' class='size form-control form ' autocomplete='off' value='$row[size]'> ";
        echo "<datalist id='size' char='#'>";
        echo"<option>";

        $msi = mysqli_query($conn, "SELECT `name` FROM `size` group by name") or die(mysqli_error($conn));

        while ($tsi = mysqli_fetch_array($msi)) {
            $szname = $tsi['name'];
            echo "<option value='$szname'>";
        }
        echo "</datalist>";
        echo "</td>";
//                                            Echo "<td><input type='text' class='form-control form size'            name='size_$ctr'  value='$row[size]'></td>";
        Echo "<td><input type='text' class='form-control form text-right qty'     name='qty_$ctr' value='$row[qty]'></td>";

        Echo "<td>";
        echo "<input list='unit' name='unit_$ctr' class='unit form-control form' autocomplete='off' value='$row[unit]'> ";
        echo "<datalist id='unit' char='#'>";
        echo"<option>";

        $mun = mysqli_query($conn, "SELECT `unit` FROM `product` group by unit") or die(mysqli_error($conn));

        while ($tun = mysqli_fetch_array($mun)) {
            $unname = $tun['name'];
            echo "<option value='$unname'>";
        }
        echo "</datalist>";
        echo "</td>";
        Echo "<td><input type='text' class='form-control form text-right sqft'  name='sqft_$ctr' value='$row[sqft]'></td>";

        Echo "<td>";
        Echo "<select name='calby_$ctr' class='form-control form calby'>";
        Echo "<option value='$row[calby]'>$calname</option>";
        Echo "<option value='P'>Pcs</option>";
        Echo "<option value='S'>Sq.ft</option>";
        Echo "<option value='B'>Box</option>";
        echo "</select>";
        echo "</td>";
//                                            Echo "<td><input type='text' class='form-control form unit'               name='unit_$ctr'  value='$row[unit]'></td>";
        Echo "<td><input type='text' class='form-control form text-right rate'    name='rate_$ctr'  value='0'></td>";
        Echo "<td><input type='text' class='form-control form text-right amount'  name='amount_$ctr'  value='0' ></td>";
//                                            Echo "<td><input type='date' class='form-control form delivery_date'  name='delivery_date_$ctr'  value='$row[delivery_date]' ></td>";
//        Echo "<td><button type='button' class='btn btn-primary purchase form' style='margin-left: 20px;' id='btn-purchase'>Settlement</button></td>";
        Echo "</tr>";
        $ctr++;
    }
    ?>
    <input type="hidden" id="items" name="items" value="<?php Echo $ctr; ?>">  
    <?php
}