<?php
header("Content-Type: application/json");
require_once "../config.php";

$action = $_POST['action'] ?? $_GET['action'] ?? '';

try {
    switch ($action) {

        // 👉 CREATE rack
        case 'insert':
            $rackNo  = $_POST['rack_no']  ?? '';
            $shelfNo = $_POST['shelf_no'] ?? '';
            $floor   = $_POST['floor']    ?? '';

            if ($rackNo !== '' && $shelfNo !== '' && $floor !== '') {
                $sql = "INSERT INTO tbl_racks (rack_no, shelf_no, floor) 
                        VALUES (:rack, :shelf, :floor)";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':rack', $rackNo);
                $stmt->bindParam(':shelf', $shelfNo);
                $stmt->bindParam(':floor', $floor);
                $stmt->execute();

                echo json_encode(["status" => "success", "message" => "Rack inserted"]);
            } else {
                echo json_encode(["status" => "error", "message" => "Missing fields"]);
            }
            break;

        // 👉 READ all racks
        case 'view':
        default:
            $stmt = $dbh->query("SELECT * FROM tbl_racks ORDER BY id DESC");
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

            if ($data) {
                echo json_encode(["status" => "success", "data" => $data]);
            } else {
                echo json_encode(["status" => "error", "message" => "No racks found"]);
            }
            break;

        // 👉 UPDATE rack
        case 'update':
            $id      = $_POST['id']       ?? '';
            $rackNo  = $_POST['rack_no']  ?? '';
            $shelfNo = $_POST['shelf_no'] ?? '';
            $floor   = $_POST['floor']    ?? '';

            if ($id !== '' && $rackNo !== '' && $shelfNo !== '' && $floor !== '') {
                $sql = "UPDATE tbl_racks 
                        SET rack_no = :rack,
                            shelf_no = :shelf,
                            floor = :floor
                        WHERE id = :id";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':rack', $rackNo);
                $stmt->bindParam(':shelf', $shelfNo);
                $stmt->bindParam(':floor', $floor);
                $stmt->bindParam(':id', $id, PDO::PARAM_INT);
                $stmt->execute();

                echo json_encode(["status" => "success", "message" => "Rack updated"]);
            } else {
                echo json_encode(["status" => "error", "message" => "Missing data"]);
            }
            break;

        // 👉 DELETE rack
        case 'delete':
            $id = $_POST['id'] ?? '';
            if ($id !== '') {
                $sql = "DELETE FROM tbl_racks WHERE id = :id";
                $stmt = $dbh->prepare($sql);
                $stmt->bindParam(':id', $id, PDO::PARAM_INT);
                $stmt->execute();

                echo json_encode(["status" => "success", "message" => "Rack deleted"]);
            } else {
                echo json_encode(["status" => "error", "message" => "Missing id"]);
            }
            break;
    }
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
