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

$brand      = $_REQUEST["brand"];
$itemgrp    = $_REQUEST["itemgrp"];

if ($brand != "") {
    $grpcond = "";
    if($itemgrp !=""){
        $grpcond = " AND `itemgrp`='$itemgrp'";
    }
    
    $query = "SELECT * FROM `item` WHERE `brand`='$brand' $grpcond  ORDER BY `code` DESC";
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    
    if(mysqli_num_rows($result) > "0"){
        $success = true;
    }

    $rows = array();
    $ctr = 1;
    while ($r = mysqli_fetch_assoc($result)) {
        
        $rows[] = [ 'success' => $success,
            'ctr' => $ctr,
            'code' => $r[code],
            'name' => $r[name],
            'brand' => $r[brand],
            'hsncode' => $r[hsncode],
            'gstperc' => $r[gstperc],
            'cost' => $r[cost],
            'selling' => $r[selling],
        ];
        $ctr++;
    }
    header("Content-type: application\json");
    echo json_encode($rows);
}