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

$tablename = isset($_REQUEST['tablename']) ? $_REQUEST['tablename'] : '';  
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';  
$recordsheet = isset($_REQUEST['recordsheet']) ? $_REQUEST['recordsheet'] : '{}'; // recordsheet 데이터 추가
$update_log = isset($_REQUEST['update_log']) ? $_REQUEST['update_log'] : ''; // update_log 추가
$num = isset($_REQUEST['num']) ? $_REQUEST['num'] : ''; // update_log 추가

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

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

$recordsheet_jsondata = json_decode($recordsheet, true); // JSON 데이터로 변환

$update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";
try {
	$pdo->beginTransaction();
	$sql = "UPDATE " . $DB . "." . $tablename . " SET recordsheet = ?, update_log = ? WHERE num = ? LIMIT 1";

	$stmh = $pdo->prepare($sql);
	$stmh->bindValue(1, json_encode($recordsheet_jsondata), PDO::PARAM_STR); // recordsheet 저장
	$stmh->bindValue(2, $update_log, PDO::PARAM_STR); // update_log 저장
	$stmh->bindValue(3, $num, PDO::PARAM_STR);

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

// 최종 결과 데이터 반환
$data = [   
 'num' => $num,
 'mode' => $mode,
 'recordsheet' => $recordsheet_jsondata,
 'update_log' => $update_log
]; 
 
echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
