<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
// kr/skin/main/basic/view.skin.php

if (!isset($view_data)) exit("잘못된 접근입니다.");

// 변수 설정
$target_id          = $view_data['target_id'];
$page_main_title    = $view_data['page_main_title'];
$display_title      = $view_data['display_title'];
$display_subtitle   = $view_data['display_subtitle'];
$content_body       = $view_data['content_body'];
$path               = $view_data['path'];
$sub_list_2depth    = $view_data['sub_list_2depth'];
$sub_list_3depth    = $view_data['sub_list_3depth'];
$current_menu_type  = $view_data['current_menu_type'];
$has_content        = $view_data['has_content'];
$header_image       = $view_data['header_image'];

// [수정] 실제 콘텐츠 PK는 이제 'id' 컬럼입니다.
$real_content_id = $view_data['content_data']['id'] ?? 0;

// 스타일 제어
$style_2depth = empty($sub_list_2depth) ? 'style="display:none;"' : '';
$style_3depth = empty($sub_list_3depth) ? 'style="display:none;"' : '';

// 링크 헬퍼
function makeSkinLink($id, $params) {
    $url = "?menu_id=" . $id;
    if (!empty($params)) $url .= '&' . ltrim($params, '?&');
    return $url;
}

$res_path = "../../../resources";
$kr_path = "../../../";
$skip_sub_visual = true;

include dirname(__DIR__, 4) . '/kr/common/header.php';
?>
<style>
/* [추가] 서브 비주얼 타이틀 애니메이션 스타일 */
.visual-wrapper {
    overflow: hidden;       
    height: 400px;
    margin-bottom: 80px;
    position: relative;
    background-color: #f1f1f1;
}
.sub-visual {
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}
.visual-img {
    transform: scale(1.2);
    transition: transform 3s ease-out;
}
.visual-img.active {
    transform: scale(1.0);
}
.tit-sub-visu {
    position: absolute;
    top: 180px;
    left: 50%;
    transform: translate(-50%, 50px);
    opacity: 0;
    color: #fff;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    font-size: 40px;
    font-weight: 700;
    z-index: 20;
    transition: opacity 1.5s ease-out, transform 1.5s ease-out;
}
.tit-sub-visu.active {
    opacity: 1;
    transform: translate(-50%, 0);
}
.layout_default {width:1400px !important; margin:0 auto}
</style>

<?php
    $bg_style = "";
    $visual_class = "sub-visual visual-img"; 
    if (!empty($header_image)) {
        $bg_style = "style='background-image: url(\"{$header_image}\");'";
    } else {
        $visual_class .= " visual-intro1"; 
    }
?>

<div class="visual-wrapper">
    <section class="<?= $visual_class ?>" <?= $bg_style ?>></section>
</div>

<h1 class="tit-sub-visu"><?= htmlspecialchars($page_main_title) ?></h1>

<nav class="nav-sub-visu" <?= $style_2depth ?>>
    <div class="frame-1600">
        <ul class="subnavi3">
        <?php foreach($sub_list_2depth as $menu): 
            $isActive = false;
            if ($path) { foreach($path as $p) { if($p['id'] == $menu['id']) $isActive = true; } }
            $cls = $isActive ? 'on' : '';
            $link = makeSkinLink($menu['id'], $menu['extra_params'] ?? '');
        ?>
            <li class="<?= $cls ?>"><a href="<?= $link ?>" class="raleway"><?= $menu['title'] ?></a></li>
        <?php endforeach; ?>
        </ul>
    </div>
</nav>

<div class="smallTab centerScrollTab" <?= $style_3depth ?>>
    <div>
        <ul>
        <?php foreach($sub_list_3depth as $menu): 
            $cls = ($menu['id'] == $target_id) ? 'on' : '';
            $link = makeSkinLink($menu['id'], $menu['extra_params'] ?? '');
        ?>
            <li class="<?= $cls ?>"><a href="<?= $link ?>" class="raleway"><?= $menu['title'] ?></a></li>
        <?php endforeach; ?>
        </ul>
    </div>
</div>
<main class="contents">

    <div class="layout_default row">
        <div class="col-6">
            <ul class="col-12 subtitle">
                <li style="font-size: 2.5em; font-weight: bold;"><?= htmlspecialchars($display_title) ?></li>
                <li><?= htmlspecialchars($display_subtitle) ?></li>
            </ul>
        </div>
        <div class="col-6">
            <div class="location-path" style="text-align: right; margin-bottom: 20px; font-size: 13px; color: #666;">
                <a href="/" style="color: #666; text-decoration: none;">HOME</a>
                <?php 
                if ($path) {
                    foreach ($path as $node) {
                        $link = makeSkinLink($node['id'], $node['extra_params'] ?? '');
                        echo ' &gt; <a href="' . $link . '" style="color: #666; text-decoration: none;">' . $node['title'] . '</a>';
                    }
                }
                ?>
            </div>
        </div>
    </div>

<!--<div class="content-body">-->
    <?php if ($current_menu_type === 'board'): ?>
        <?php 
            $board_core_path = __DIR__ . '/../../../board/board.php';
            if (file_exists($board_core_path)) include $board_core_path;
        ?>

    <?php elseif ($current_menu_type === 'gallery'): ?>
        <?php 
            $gallery_core_path = __DIR__ . '/../../../gallery/board.php';
            if (file_exists($gallery_core_path)) include $gallery_core_path;
        ?>
    
    <?php elseif ($current_menu_type === 'product'): ?>
        <?php 
            // [수정] 보내주신 이미지(image_691394.png) 확인 결과
            // 경로: kr/skin/main/basic (현재) -> kr/product/product.php (타겟)
            $product_core_path = __DIR__ . '/../../../product/product.php';

            if (file_exists($product_core_path)) {
                include $product_core_path;
            } else {
                echo "<div style='padding:50px; text-align:center; color:red;'>⚠️ Product 모듈(product.php)을 찾을 수 없습니다.<br>경로: {$product_core_path}</div>";
            }
        ?>

    <?php elseif ($current_menu_type === 'product2'): ?>
        <?php 
            // [수정] 보내주신 이미지(image_691394.png) 확인 결과
            // 경로: kr/skin/main/basic (현재) -> kr/product/product.php (타겟)
            $product_core_path = __DIR__ . '/../../../product/product.php';

            if (file_exists($product_core_path)) {
                include $product_core_path;
            } else {
                echo "<div style='padding:50px; text-align:center; color:red;'>⚠️ Product 모듈(product.php)을 찾을 수 없습니다.<br>경로: {$product_core_path}</div>";
            }
        ?>

            <div id="view-area">
                <div style="text-align:right; margin-bottom:10px; border-bottom:1px solid #eee; padding-bottom:10px;">
                    <button type="button" onclick="toggleEditMode(true)" style="background:#333; color:#fff; border:none; padding:5px 15px; border-radius:4px; font-size:13px; cursor:pointer;">
                        ✎ 본문 수정하기
                    </button>
                </div>

                <?php if ($has_content): ?>
                    <div style="margin-top:20px; line-height:1.6;" class="editor-output">
                        <?= $content_body ?>
                    </div>
                <?php else: ?>
                    <div style="text-align: center; color: #999; width:100%; height:300px; padding-top:50px;">
                        <p>등록된 콘텐츠가 없습니다. '수정하기'를 눌러 내용을 입력하세요.</p>
                    </div>
                <?php endif; ?>
            </div>

            <?php 
                if (file_exists(__DIR__ . '/view_edit.skin.php')) {
                    include __DIR__ . '/view_edit.skin.php'; 
                } else {
                    echo "";
                }
            ?>


    <?php else: ?>

            <div id="view-area">
            <?php if (isset($_SESSION['member_role']) && in_array($_SESSION['member_role'], ['ADMIN', 'REPORT', 'MANAGER'])): ?>
                <div class="content-body ">
                <div style="text-align:right; margin-bottom:10px; border-bottom:1px solid #eee; padding-bottom:10px;">
                    <button type="button" onclick="toggleEditMode(true)" style="background:#333; color:#fff; border:none; padding:5px 15px; border-radius:4px; font-size:13px; cursor:pointer;">
                        ✎ 본문 수정하기
                    </button>
                </div>
                </div>
            <?php endif; ?>
    
                <?php if ($has_content): ?>
                    <div style="margin-top:20px; line-height:1.6;" class="editor-output">
                        <?= $content_body ?>
                    </div>
                <?php else: ?>
                    <div style="text-align: center; color: #999; width:100%; height:300px; padding-top:50px;">
                        <p>등록된 콘텐츠가 없습니다. '수정하기'를 눌러 내용을 입력하세요.</p>
                    </div>
                <?php endif; ?>
            </div>

            <?php 
                if (file_exists(__DIR__ . '/view_edit.skin.php')) {
                    include __DIR__ . '/view_edit.skin.php'; 
                } else {
                    echo "";
                }
            ?>
        <?php endif; ?>
    <!--</div>-->
</main>
<div class="footer_slogan">
    <div class="content-body ">
        <h2>
        "WITH FAITH IN TOMORROW'S HOPE,</br>
         JUNGWON ENSYS BUILDS A BRIGHTER FUTURE THROUGH NEW CHALLENGES."
        </h2>
    </div>
</div>

<?php include dirname(__DIR__, 4) . '/kr/common/footer.php'; ?>

