<?php

require_once "config.php";

header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        $query = $conn->prepare("SELECT t1.*
                                 FROM tbbarang t1 
                                 INNER JOIN tbbarangbestseller t2 ON t1.cKode = t2.cKode");
        $query->execute();
        $result = $query->get_result();
        $data = $result->fetch_all(MYSQLI_ASSOC); 
        
        foreach ($data as &$row) {
            $row['nIsi'] = intval(floatval($row['nIsi']));
            $row['nIsi2'] = intval(floatval($row['nIsi2']));
            $row['nHrgJualPerPcs'] = intval(floatval($row['nHrgJualPerPcs']));
            $row['nMinEcer'] = intval(floatval($row['nMinEcer']));
            $row['nberat'] = intval(floatval($row['nberat']));
            $row['npanjang'] = intval(floatval($row['npanjang']));
            $row['nlebar'] = intval(floatval($row['nlebar']));
            $row['ntinggi'] = intval(floatval($row['ntinggi']));
        }

        echo json_encode($data);
        break;

    default:
        echo json_encode(array('error' => 'Invalid request method.'));
        break;
}

mysqli_close($conn);
?>
