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

$tablename = $_REQUEST['tablename'] ?? '';
$mode = $_REQUEST['mode'] ?? '';

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

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

// 필요한 변수만 받아옵니다.
include '_request.php';

if ($mode == "update") {
    // 수정 시 업데이트 로그에 현재 시간과 세션의 이름을 추가합니다.
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10;";
    try {
        $pdo->beginTransaction();
        $sql  = "UPDATE " . $DB . "." . $tablename . " SET ";
        $sql .= "registdate = ?, groupCode = ?, groupName = ?, searchtag = ?, update_log = ?, connectedNum = ? ";
        $sql .= "WHERE num = ? LIMIT 1";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $registdate, PDO::PARAM_STR);
        $stmh->bindValue(2, $groupCode, PDO::PARAM_STR);
        $stmh->bindValue(3, $groupName, PDO::PARAM_STR);
        $stmh->bindValue(4, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(5, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(6, $connectedNum, PDO::PARAM_STR);
        $stmh->bindValue(7, $num, PDO::PARAM_STR);
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
}

if ($mode == "insert") {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10;";
    try {
        $pdo->beginTransaction();
        $sql  = "INSERT INTO " . $DB . "." . $tablename . " ";
        $sql .= "(registdate, groupCode, groupName, searchtag, update_log, connectedNum) ";
        $sql .= "VALUES (?, ?, ?, ?, ?, ?)";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $registdate, PDO::PARAM_STR);
        $stmh->bindValue(2, $groupCode, PDO::PARAM_STR);
        $stmh->bindValue(3, $groupName, PDO::PARAM_STR);
        $stmh->bindValue(4, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(5, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(6, $connectedNum, PDO::PARAM_STR);
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
    
    // 신규 입력된 레코드의 num 값을 가져옵니다.
    $sql = "SELECT * FROM " . $DB . "." . $tablename . " ORDER BY num DESC LIMIT 1";
    try {
        $stmh = $pdo->query($sql);
        $row = $stmh->fetch(PDO::FETCH_ASSOC);
        $num = $row["num"];
    } catch (PDOException $Exception) {
        print "오류: " . $Exception->getMessage();
    }
}

if ($mode == "delete") {
    try {
        $pdo->beginTransaction();
        // 레코드를 완전 삭제하는 대신, is_deleted 컬럼의 값을 1로 설정합니다.
        $sql = "UPDATE " . $DB . "." . $tablename . " SET is_deleted = '1' WHERE num = ?";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_STR);
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
}


$data = [
    'num'  => $num,
    'mode' => $mode
];

echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
