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

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

header("Content-Type: application/json");  // JSON 응답을 위한 설정

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

include "_request.php";  // 요청 데이터를 변수로 설정

if ($mode == "modify") {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";

    try {
        $pdo->beginTransaction();
        $sql = "UPDATE " . $DB . "." . $tablename . " SET 
            use_date = ?, car_number = ?, department = ?, position = ?, caruser_name = ?, 
            usage_purpose = ?, operation_details = ?, departure = ?, destination = ?, 
            driving_distance = ?, arrival_cumulative_distance = ?, note = ?, 
            update_log = ? 
            WHERE num = ? LIMIT 1";

        $stmh = $pdo->prepare($sql);
        
        // 바인딩
        $stmh->bindValue(1, $use_date, PDO::PARAM_STR);
        $stmh->bindValue(2, $car_number, PDO::PARAM_STR);
        $stmh->bindValue(3, $department, PDO::PARAM_STR);
        $stmh->bindValue(4, $position, PDO::PARAM_STR);
        $stmh->bindValue(5, $caruser_name, PDO::PARAM_STR);
        $stmh->bindValue(6, $usage_purpose, PDO::PARAM_STR);
        $stmh->bindValue(7, $operation_details, PDO::PARAM_STR);
        $stmh->bindValue(8, $departure, PDO::PARAM_STR);
        $stmh->bindValue(9, $destination, PDO::PARAM_STR);
        $stmh->bindValue(10, $driving_distance, PDO::PARAM_STR);
        $stmh->bindValue(11, str_replace(',', '', $arrival_cumulative_distance), PDO::PARAM_STR);
        $stmh->bindValue(12, $note, PDO::PARAM_STR);
        $stmh->bindValue(13, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(14, $num, PDO::PARAM_INT);

        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
} elseif ($mode == "insert" or $mode == "copy" or $mode == "copyDestination") {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";

    try {
        $pdo->beginTransaction();
        $sql = "INSERT INTO " . $DB . "." . $tablename . " (
            use_date, car_number, department, position, caruser_name, 
            usage_purpose, operation_details, departure, destination, 
            driving_distance, arrival_cumulative_distance, note, 
            update_log 
        ) VALUES (?, ?, ?, ?, ?,    ?, ?, ?, ?, ?,    ?, ?, ? )";

        $stmh = $pdo->prepare($sql);
        
        // 바인딩
        $stmh->bindValue(1, $use_date, PDO::PARAM_STR);
        $stmh->bindValue(2, $car_number, PDO::PARAM_STR);
        $stmh->bindValue(3, $department, PDO::PARAM_STR);
        $stmh->bindValue(4, $position, PDO::PARAM_STR);
        $stmh->bindValue(5, $caruser_name, PDO::PARAM_STR);
        $stmh->bindValue(6, $usage_purpose, PDO::PARAM_STR);
        $stmh->bindValue(7, $operation_details, PDO::PARAM_STR);
        $stmh->bindValue(8, $departure, PDO::PARAM_STR);
        $stmh->bindValue(9, $destination, PDO::PARAM_STR);
        $stmh->bindValue(10, $driving_distance, PDO::PARAM_STR);
        $stmh->bindValue(11, str_replace(',', '', $arrival_cumulative_distance), PDO::PARAM_STR);
        $stmh->bindValue(12, $note, PDO::PARAM_STR);
        $stmh->bindValue(13, $update_log, PDO::PARAM_STR);

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

    // 가장 마지막에 삽입된 `num` 가져오기
    $sql = "SELECT num 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();
    }
} elseif ($mode == "delete") {
    try {
        $pdo->beginTransaction();
        $sql = "UPDATE " . $DB . "." . $tablename . " SET is_deleted = '1' WHERE num = ?";  
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_INT);
        $stmh->execute();
        $pdo->commit();
    } catch (Exception $ex) {
        $pdo->rollBack();
        print "오류: " . $ex->getMessage();
    }
}

// 응답 데이터 구성
$data = [   
    'num' => $num,
    'mode' => $mode
]; 

echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
