<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");

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

$num = isset($_GET['num']) ? $_GET['num'] : '';

$response = ['success' => false];

if ($num) {
    try {
        $pdo = db_connect();

        $sql = "SELECT screenlist FROM chandj.output WHERE num = ?";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_INT);
        $stmh->execute();

        $row = $stmh->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            $response['success'] = true;
            $response['screenlist'] = json_decode($row['screenlist'], true);
        } else {
            $response['message'] = "No data found";
        }
    } catch (PDOException $e) {
        $response['message'] = "Error: " . $e->getMessage();
    }
}

echo json_encode($response);
?>
