<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
// kr/skin/gallery/basic/view.skin.php
// [역할] 갤러리 상세 보기
?>
<div class="content-body">
    <div class="board-view-wrap layout_default" style="border-top:1px solid #000; padding:20px; background:#fff;">
        <div style="padding:0px 0px ; color:#666;  margin-bottom:20px;">
            <h2 style="font-size:22px; font-weight:bold; padding:0px 0px"><?= htmlspecialchars($view['title']) ?></h2>
        </div>
    </div>   

    <div style="border-bottom:1px solid #ccc;border-top:1px solid #ccc; padding:13px 20px ; color:#666;  margin-bottom:20px;">
            <span style="margin-right:15px;">작성자: <strong><?= htmlspecialchars($view['writer']) ?></strong></span>
            <span style="margin-right:15px;">날짜: <?= $view['created_at'] ?></span>
            <span>조회: <?= $view['hit'] ?></span>
    </div>

    <div class="view-content" style="min-height:200px; line-height:1.8; ">
        
        <?php if (isset($files) && count($files) > 0): ?>
            <div class="gallery-images" style="text-align:center; margin-bottom:30px;">
                <?php foreach($files as $file): ?>
                    <?php 
                        $ext = strtolower(pathinfo($file['file_name_org'], PATHINFO_EXTENSION));
                        if(in_array($ext, ['jpg','jpeg','png','gif','webp'])): 
                    ?>
                        <img src="<?= $file['file_path'] ?>" style="max-width:100%; margin-bottom:15px;">
                    <?php endif; ?>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        
        <div style="padding:0 10px; margin-bottom:30px;">
            <?= nl2br($view['content']) ?>
        </div>

        <?php if ($board_type === 'news' && !empty($view['link'])): ?>
        <div style="margin-top:20px; padding:15px; border-top:1px solid #eee; border-bottom:1px solid #eee; background:#f9f9f9;">
            <h4 style="margin-bottom:10px; font-size:16px; color:#333;">📰 뉴스 원문보기</h4>
            <p style="margin:0; font-size:14px;">
                <strong>뉴스 링크 :</strong> <a href="<?= $view['link'] ?>" target="_blank" style="color:#007bff; text-decoration:underline;"><?= $view['link'] ?></a>
            </p>
        </div>
        <?php endif; ?>
    </div>

    <div style="margin-top:30px; text-align:right; border-top:1px solid #eee; padding-top:20px;">
        <a href="?menu_id=<?= $target_id ?>&type=<?= $board_type ?>" class="btn-board" style="background:#666; color:#fff; padding:8px 20px; text-decoration:none; border-radius:4px; ">목록으로</a>
        
        <?php if (isset($_SESSION['user_id'])): ?>
            <a href="?menu_id=<?= $target_id ?>&type=<?= $board_type ?>&mode=edit&uid=<?= $uid ?>" class="btn-board" style="background:#007bff; color:#fff; padding:8px 20px; text-decoration:none; border-radius:4px;  margin-left:5px;">수정</a>
            <button onclick="deleteGallery()" class="btn-board" style="background:#dc3545; color:#fff; padding:8px 20px; border:none; border-radius:4px;  margin-left:5px; cursor:pointer;">삭제</button>
        <?php endif; ?>
    </div>
</div>

<script>
function deleteGallery() {
    if(confirm('정말 삭제하시겠습니까?')) {
        location.href = '../gallery/process.php?act=delete&uid=<?= $uid ?>&menu_id=<?= $target_id ?>&type=<?= $board_type ?>';
    }
}
</script>