<?php

require_once "config.php";

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    $cKode = $_GET['cKode'] ?? '';
    $cKodeMember = $_GET['cKodeMember'] ?? '';
    $cNamaMember = $_GET['cNamaMember'] ?? '';

    $queryBarang = $conn->prepare("SELECT * FROM tbbarang WHERE cKode = ?");
    $queryBarang->bind_param("s", $cKode);
    
    if ($queryBarang->execute()) {
        $result = $queryBarang->get_result();

        if ($result->num_rows > 0) {
            $barang = $result->fetch_assoc();

            $cNamaBrg = $barang['cNama'];
            $nHarga = $barang['nHrgBeli'];
            $nQty = 1;
            $nDisc1 = 0;
            $nDisc2 = 0;
            $nDisc3 = 0;
            $nSTotal = $nHarga * $nQty;

            $queryCheckExisting = $conn->prepare("SELECT * FROM tbkeranjang WHERE cKodeMember = ? AND cKodeBrg = ?");
            $queryCheckExisting->bind_param("ss", $cKodeMember, $cKode);

            if ($queryCheckExisting->execute()) {
                $resultCheckExisting = $queryCheckExisting->get_result();

                if ($resultCheckExisting->num_rows > 0) {
                    $existingItem = $resultCheckExisting->fetch_assoc();
                    $newQty = $existingItem['nQty'] + 1;
                    $newSTotal = $newQty * $nHarga;

                    $queryUpdate = $conn->prepare("UPDATE tbkeranjang SET nQty = ?, nSTotal = ? WHERE cKodeMember = ? AND cKodeBrg = ?");
                    $queryUpdate->bind_param("dsss", $newQty, $newSTotal, $cKodeMember, $cKode);

                    if ($queryUpdate->execute()) {
                        echo json_encode(['success' => 'berhasil ditambahkan ke keranjang']);
                    } else {
                        echo json_encode(['error' => 'Gagal memperbarui keranjang']);
                    }
                    $queryUpdate->close();
                } else {
                    $queryMaxNoID = $conn->prepare("SELECT MAX(CAST(cNoID AS UNSIGNED)) AS newNoID FROM tbkeranjang WHERE cKodeMember = ?");
                    $queryMaxNoID->bind_param("s", $cKodeMember);
                    
                    if ($queryMaxNoID->execute()) {
                        $resultMaxNoID = $queryMaxNoID->get_result();
                        $row = $resultMaxNoID->fetch_assoc();
                        $newNoID = $row['newNoID'] + 1; // Menambahkan 1 pada NoID terakhir
                    
                        // Menggunakan LPAD untuk memastikan ID memiliki 3 digit
                        $newNoID = str_pad($newNoID, 3, '0', STR_PAD_LEFT); 
                    } else {
                        $newNoID = '001'; // Jika tidak ada data, mulai dari 001
                    }



                    $queryInsert = $conn->prepare("INSERT INTO tbkeranjang (cNoID, cKodeMember, cNamaMember, cKodeBrg, cNamaBrg, nQty, nHarga, nDisc1, nDisc2, nDisc3, nSTotal) 
                                                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                    $queryInsert->bind_param("sssssssssss", $newNoID, $cKodeMember, $cNamaMember, $cKode, $cNamaBrg, $nQty, $nHarga, $nDisc1, $nDisc2, $nDisc3, $nSTotal);

                    if ($queryInsert->execute()) {
                        echo json_encode(['success' => 'berhasil ditambahkan ke keranjang']);
                    } else {
                        echo json_encode(['error' => 'Gagal menambahkan data ke keranjang']);
                    }
                    $queryInsert->close();
                }
            } else {
                echo json_encode(['error' => 'Gagal memeriksa apakah item ada di keranjang']);
            }

            $queryCheckExisting->close();
        } else {
            echo json_encode(['error' => 'Produk tidak ditemukan']);
        }
    } else {
        echo json_encode(['error' => 'Gagal mengeksekusi query']);
    }

    $queryBarang->close();
} else {
    echo json_encode(['error' => 'Metode tidak diizinkan']);
}

mysqli_close($conn);

?>
