connect_error) { die(json_encode(["error" => "Connection failed: " . $conn->connect_error])); } $action = isset($_GET['action']) ? $_GET['action'] : ''; // --- 1. GET EVERYTHING FOR THE DASHBOARD --- if ($action === 'get_dashboard') { $dashboard = ["bots" => [], "stores" => [], "files" => []]; $res = $conn->query("SELECT * FROM sml_bots ORDER BY id ASC"); while($row = $res->fetch_assoc()) { $dashboard["bots"][] = $row; } $res = $conn->query("SELECT * FROM sml_stores ORDER BY created_at DESC"); while($row = $res->fetch_assoc()) { $dashboard["stores"][] = $row; } $res = $conn->query("SELECT * FROM sml_files"); while($row = $res->fetch_assoc()) { $dashboard["files"][] = $row; } echo json_encode($dashboard); } // --- 2. SAVE A NEW STORE & ITS FILES --- elseif ($action === 'save_store') { $data = json_decode(file_get_contents("php://input"), true); $store_id = $conn->real_escape_string($data['store_id']); $display_name = $conn->real_escape_string($data['display_name']); $files = $data['file_names']; // Array of file names $conn->query("INSERT INTO sml_stores (store_id, display_name) VALUES ('$store_id', '$display_name')"); foreach ($files as $file) { $clean_file = $conn->real_escape_string($file); $conn->query("INSERT INTO sml_files (store_id, file_name) VALUES ('$store_id', '$clean_file')"); } echo json_encode(["success" => true]); } // --- 3. DELETE STORE --- elseif ($action === 'delete_store') { $data = json_decode(file_get_contents("php://input"), true); $store_id = $conn->real_escape_string($data['store_id']); // Because of ON DELETE CASCADE in SQL, deleting the store automatically deletes its files! $conn->query("DELETE FROM sml_stores WHERE store_id='$store_id'"); echo json_encode(["success" => true]); } // --- 4. BOT MANAGEMENT --- elseif ($action === 'save_bot') { $data = json_decode(file_get_contents("php://input"), true); $name = $conn->real_escape_string($data['bot_name']); $persona = $conn->real_escape_string($data['persona']); $store = $conn->real_escape_string($data['store_id']); $color = $conn->real_escape_string($data['theme_color']); $sql = "INSERT INTO sml_bots (bot_name, persona, store_id, theme_color) VALUES ('$name', '$persona', '$store', '$color')"; if ($conn->query($sql) === TRUE) { echo json_encode(["success" => true]); } else { echo json_encode(["error" => $conn->error]); } } elseif ($action === 'update_bot') { $data = json_decode(file_get_contents("php://input"), true); $id = $conn->real_escape_string($data['id']); $name = $conn->real_escape_string($data['bot_name']); $persona = $conn->real_escape_string($data['persona']); $store = $conn->real_escape_string($data['store_id']); $color = $conn->real_escape_string($data['theme_color']); $sql = "UPDATE sml_bots SET bot_name='$name', persona='$persona', store_id='$store', theme_color='$color' WHERE id='$id'"; if ($conn->query($sql) === TRUE) { echo json_encode(["success" => true]); } else { echo json_encode(["error" => $conn->error]); } } elseif ($action === 'delete_bot') { $data = json_decode(file_get_contents("php://input"), true); $id = $conn->real_escape_string($data['id']); $conn->query("DELETE FROM sml_bots WHERE id='$id'"); echo json_encode(["success" => true]); } else { echo json_encode(["error" => "Invalid action"]); } $conn->close(); ?>