<?php

require("../includes/check_auth_user.php");
require("../includes/dbconnect.php");
include('../includes/function.php');
header('Content-Type: text/html; charset=utf-8');

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

$prodcode = $_REQUEST["prodcode"];

//echo $prodcode;
$query = "SELECT size FROM  `prodtran` WHERE prodcode = '$prodcode' ORDER BY `size`";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$getcost = mysqli_query($conn, "SELECT cost, cost2 FROM `product` WHERE code = '$prodcode' ORDER BY `name`") or die(mysqli_error($conn));
$fcost = mysqli_fetch_assoc($getcost);

if ($fcost['cost2'] == '') {
    $prodcost = $fcost['cost'];
} else {
    $prodcost = $fcost['cost2'];
}

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

//echo $r[size];
    $getsize = mysqli_query($conn, "SELECT name FROM `size` WHERE code = '$r[size]' ORDER BY `name`") or die(mysqli_error($conn));
    $fsize = mysqli_fetch_assoc($getsize);
//echo $fsize[name];

    $rows[] = [ 'success' => true,
        'code' => $r['size'],
        'name' => $fsize['name'],
        'cost' => $prodcost
    ];
}
header("Content-type: application\json");
echo json_encode($rows);
?>