
<?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");

$num = isset($_REQUEST['num']) ? $_REQUEST['num'] : '';
$tablename = isset($_REQUEST['tablename']) ? $_REQUEST['tablename'] : '';
$tablename_sub = 'output_extra';
isset($_REQUEST["timekey"])  ? $timekey=$_REQUEST["timekey"] :  $timekey='';   // 신규데이터에 생성할때 임시저장키  

include '_request.php';

if (isset($_POST['accountList'])) {
    $accountList_jsondata = json_decode($_POST['accountList'], true);
} else {
    // Error handling or fallback
    $accountList_jsondata = null;
}

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

try {
   
        $pdo->beginTransaction();   
        // output_extra update
        $sql2 = "UPDATE $DB.$tablename_sub SET accountDate=?, accountList=?, ET_unapproved=?, ET_total=? WHERE parent_num = ? ";
        $stmh2 = $pdo->prepare($sql2);
        $stmh2->execute([            
            $accountDate, json_encode($accountList_jsondata), intval(str_replace(',', '', $ET_unapproved)), intval(str_replace(',', '', $ET_total)), $num
        ]);
        $pdo->commit();        
    $data = [
        'num' => $num        
    ];

    echo json_encode($data, JSON_UNESCAPED_UNICODE);
} catch (PDOException $Exception) {
    error_log("오류: " . $Exception->getMessage());
    http_response_code(500);
    echo json_encode(['error' => $Exception->getMessage()]);
} catch (Exception $e) {
    error_log("오류: " . $e->getMessage());
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>
