<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
header("Content-Type: application/json");

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$num = isset($_REQUEST['num']) ? $_REQUEST['num'] : '';
$tablename = isset($_REQUEST['tablename']) ? $_REQUEST['tablename'] : '';
$update_log = isset($_REQUEST['update_log']) ? $_REQUEST['update_log'] : ''; // 추가된 부분

include '_request.php';

if (isset($_POST['recordscreenMid'])) {
    $recordscreenMid_jsondata = json_decode($_POST['recordscreenMid'], true);
} else {
    $recordscreenMid_jsondata = null;
}

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

try {
	$update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";
	// 공백 2개 이상 제거
	$update_log = preg_replace('/\s{2,}/', ' ', $update_log);
    $pdo->beginTransaction();
    
    $sql = "UPDATE $DB.$tablename 
            SET recordscreenMid=?, update_log=? WHERE num=? LIMIT 1";

    $stmh = $pdo->prepare($sql);
    $stmh->bindValue(1, json_encode($recordscreenMid_jsondata), PDO::PARAM_STR);      
    $stmh->bindValue(2, $update_log, PDO::PARAM_STR);      
    $stmh->bindValue(3, $num, PDO::PARAM_STR);

    $stmh->execute();
    $pdo->commit();
    
} catch (PDOException $Exception) {
    error_log("오류: " . $Exception->getMessage());
}

$data = [
    'num' => $num,        
    'is_deleted' => $is_deleted ?? null,
    'recordscreenMid_jsondata' => $recordscreenMid_jsondata
];

echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
