<?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;


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

$cond = "";
if ($_REQUEST["sfield"] != "") {
    if ($operator == "=") {
         if ($_REQUEST["sfield"] == "1") {
            $cond = "AND `barcode` = '$searchTerm'";
        } 
    } else {
         if ($_REQUEST["sfield"] == "1") {
            $cond = "AND `barcode`  LIKE '%$searchTerm%'";
        }
    }
}


if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `barcode`";
}

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

$itemgrp    = $_REQUEST["itemgrp"];

    if($itemgrp !=""){
    
    $query = "SELECT * FROM `product` WHERE `prodgrp`='$itemgrp' $cond  ORDER BY `code`  $ordcond";
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    

$rows = array();
$ctr = 1;
while ($r = mysqli_fetch_assoc($result)) {
    

         $rows[] = [ 'success' => $success,
            'ctr' => $ctr,
            'name' => $r[name],
            'barcode' => $r[barcode],
//            'gram' => $r[gram],
//            'packing' => $r[packing],
//            'colour' => $r[colour],
//            'unit' => $r[unit],
//            'gstperc' => $r[gstperc],
            'cost' => $r[cost],
            'rate' => $r[rate],
//            'mrp' => $r[mrp],
//            'msp' => $r[msp]
        ];
        $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
}