<?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];
$success = false;

$file = "item.php";

$keyfld = "code";
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
//$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];
$usertype = $_SESSION["usertype"];
$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `nameprin` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `size` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `unit` =` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `comp` =` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `nameprin` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `size` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `unit` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `comp` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `name`,code";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `name`,code";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `nameprin`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `size`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `unit`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `comp`";
}



$ordcond = "ASC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

$query = "SELECT * FROM `item` WHERE code != '' $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
if ($usertype == 1) {
    $mdeldisp = "<td align='left' class='deldata'><i class='fa fa-trash'></i></td>";
} else {
    $mdeldisp = "";
}


//AS SAID BY ALIM BHAI

//mysqli_query($conn, "Create temporary table itemstk 
//                                SELECT sum(qty) as tqty,itemname,size FROM `itemtran` group by itemname,size
//                                union all
//                                SELECT sum(opstk) as tqty,itemname,size FROM `openstk` group by itemname,size")or die(mysqli_error($conn));


while ($r = mysqli_fetch_assoc($result)) {
//    $getm = mysqli_query($conn, "SELECT tqty FROM `itemstk` WHERE `itemname`='$r[name]' and size='$r[size]'")or die(mysqli_error($conn));
//    if (!empty($getm)) {
//        $m = mysqli_fetch_array($getm);
//        $stkqty = $m['tqty'];
//        if ($stkqty != 0) {
//            $stqty = $stkqty;
//        } else {
//            $stqty = 0;
//        }
//    } else {
//        $stqty = 0;
//    }
//    $bg = "";
//
//    if ($stkqty > 0) {
//        $bg = "style='color:green;background-color:#f7c094'";
//    }
//    if ($stkqty < 0) {
//        $bg = "style='color:red;background-color:#ECEBA3'";
//    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $r["code"],
        'name' => $r["name"],
        'nameprin' => $r["nameprin"],
        'size' => $r["size"],
        'unit' => $r["unit"],
        'comp' => $r["comp"],
//        'stkqty' => "<td align='left' $bg>$stkqty</td>",
        'action' => "$file?mode=edit&$keyfld=$r[$keyfld]",
        'usertype' => $mdeldisp,
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
?>