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


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

$success = false;

$maindb = 'item';
$file = 'item.php';
$keyfld = "code";

$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];

$cond = "";
if ($operator == "=") {
     if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `prodname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `size` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `brand` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `quality` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `code` = '$searchTerm'";
    }else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `gst` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `prodname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `size` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `brand` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `quality` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `code` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `gst` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `prodname`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `prodname` ";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `size`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `brand`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `quality`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `code`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `gst`";
} 

$ordcond = "DESC";
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;
while ($r = mysqli_fetch_array($result)) {
    $rows[] = ['success' => true,
        'value' => $ctr,
        'prodname' => $r['prodname'],
        'size' => $r['size'],
        'brand' => $r['brand'],
        'pcsinunit' => $r['pcsinunit'],
        'hsncode' => $r['hsncode'],
        'quality' => $r['quality'],
        'itemcode' => $r['code'],
        'gst' => $r['gst'],
        'action' => "$file?mode=edit&$keyfld=$r[$keyfld]",
    ];
    $ctr++;
}
//browse($rows);
header("Content-type: application\json");
echo json_encode($rows);
