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

$tablename = isset($_REQUEST['tablename']) ? $_REQUEST['tablename'] : 'bottombar';
$selectWork = isset($_REQUEST['selectWork']) ? $_REQUEST['selectWork'] : '';
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$num = isset($_REQUEST['num']) ? $_REQUEST['num'] : '';

header("Content-Type: application/json");
 
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

// 전개도 데이터 처리인 경우
if ($selectWork == 'unfold') {    
    $bending_components = isset($_REQUEST['bending_components']) ? $_REQUEST['bending_components'] : '';    
    $material_summary = isset($_REQUEST['material_summary']) ? $_REQUEST['material_summary'] : '';
} else {
    // 기존 코드
    include "_request.php"; // num, registration_date, ..., bending_components 변수 할당
}

if ($selectWork == 'unfold') {
    try {
        $pdo->beginTransaction();
                
        $sql = "UPDATE {$DB}.{$tablename} SET                     
                    bending_components = :bending_components,                    
                    material_summary = :material_summary 
                WHERE num = :num LIMIT 1";

        $stmh = $pdo->prepare($sql);

        // 파라미터 바인딩        
        $stmh->bindValue(':bending_components', $bending_components); // 스냅샷 JSON 저장        
        $stmh->bindValue(':material_summary', $material_summary); // 자재요약 JSON 저장        
        $stmh->bindValue(':num', $num, PDO::PARAM_INT);

        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        echo json_encode(['error' => $Exception->getMessage()]);
        exit;
    }
}
else
{
    // --- [수정] ---
    if ($mode == "modify") {
        $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . (isset($update_log) ? $update_log : '') . "&#10";
        
        // search_tag 생성
        $search_tag = $registration_date . ' ' .
                     $author . ' ' .
                     $remark . ' ' .
                     $model_name . ' ' .
                     $bar_width . ' ' .
                     $bar_height . ' ' .
                     $finishing_type . ' ' .
                     $firstitem . ' ' .
                     $model_UA . ' ' .
                     $search_keyword;
        
        try {
            $pdo->beginTransaction();
            
            $sql = "UPDATE {$DB}.{$tablename} SET 
                        registration_date = :registration_date, 
                        firstitem = :firstitem,
                        model_name = :model_name, 
                        model_UA = :model_UA,
                        author = :author, 
                        remark = :remark, 
                        update_log = :update_log,
                        search_tag = :search_tag,
                        search_keyword = :search_keyword,
                        bar_width = :bar_width, 
                        bar_height = :bar_height, 
                        finishing_type = :finishing_type,
                        bending_components = :bending_components,
                        material_summary = :material_summary
                    WHERE num = :num LIMIT 1";

            $stmh = $pdo->prepare($sql);

            // 파라미터 바인딩
            $stmh->bindValue(':registration_date', $registration_date);
            $stmh->bindValue(':firstitem', $firstitem);
            $stmh->bindValue(':model_name', $model_name);
            $stmh->bindValue(':model_UA', $model_UA);
            $stmh->bindValue(':author', $author);
            $stmh->bindValue(':remark', $remark);
            $stmh->bindValue(':update_log', $update_log);
            $stmh->bindValue(':search_tag', $search_tag);
            $stmh->bindValue(':search_keyword', $search_keyword);
            $stmh->bindValue(':bar_width', $bar_width);
            $stmh->bindValue(':bar_height', $bar_height);
            $stmh->bindValue(':finishing_type', $finishing_type);
            $stmh->bindValue(':bending_components', $bending_components); // 스냅샷 JSON 저장
            $stmh->bindValue(':material_summary', $material_summary); // 자재요약 JSON 저장
            $stmh->bindValue(':num', $num, PDO::PARAM_INT);

            $stmh->execute();
            $pdo->commit();
        } catch (PDOException $Exception) {
            $pdo->rollBack();
            echo json_encode(['error' => $Exception->getMessage()]);
            exit;
        }
    }
    else if ($mode == "insert" || $mode == "copy") {
        $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . (isset($update_log) ? $update_log : '') . "&#10";
        
        // search_tag 생성
        $search_tag = $registration_date . ' ' .
                     $author . ' ' .
                     $remark . ' ' .
                     $model_name . ' ' .
                     $bar_width . ' ' .
                     $bar_height . ' ' .
                     $finishing_type . ' ' .
                     $firstitem . ' ' .
                     $model_UA . ' ' .
                     $search_keyword;
        
        try {
            $pdo->beginTransaction();
            
            $sql = "INSERT INTO {$DB}.{$tablename} (
                        registration_date, firstitem, model_name, model_UA, author, remark, update_log,
                        search_tag, search_keyword, bar_width, bar_height, finishing_type, 
                        bending_components, material_summary
                    ) VALUES (
                        :registration_date, :firstitem, :model_name, :model_UA, :author, :remark, :update_log,
                        :search_tag, :search_keyword, :bar_width, :bar_height, :finishing_type,
                        :bending_components, :material_summary
                    )";

            $stmh = $pdo->prepare($sql);

            $stmh->bindValue(':registration_date', $registration_date);
            $stmh->bindValue(':firstitem', $firstitem);
            $stmh->bindValue(':model_name', $model_name);
            $stmh->bindValue(':model_UA', $model_UA);
            $stmh->bindValue(':author', $author);
            $stmh->bindValue(':remark', $remark);
            $stmh->bindValue(':update_log', $update_log);
            $stmh->bindValue(':search_tag', $search_tag);
            $stmh->bindValue(':search_keyword', $search_keyword);
            $stmh->bindValue(':bar_width', $bar_width);
            $stmh->bindValue(':bar_height', $bar_height);
            $stmh->bindValue(':finishing_type', $finishing_type);
            $stmh->bindValue(':bending_components', $bending_components); // 스냅샷 JSON 저장
            $stmh->bindValue(':material_summary', $material_summary); // 자재요약 JSON 저장

            $stmh->execute();
            $num = $pdo->lastInsertId();
            $pdo->commit();
        } catch (PDOException $Exception) {
            $pdo->rollBack();
            echo json_encode(['error' => $Exception->getMessage()]);
            exit;
        }
    }
    // (삭제 로직은 동일)
    else if ($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 (PDOException $ex) {
            $pdo->rollBack();
            echo json_encode(['error' => $ex->getMessage()]);
            exit;
        }
    }
}

// 최종 성공 응답
$data = [
    'num' => $num,
    'mode' => $mode
];
echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
