<?php
$csrfField = \Helper::csrfField();
?>
<div class="page-header">
    <h1 class="page-title">本地文件浏览</h1>
    <div class="page-actions">
        <a href="<?php echo \Helper::url('admin/storage/files'); ?>" class="btn btn-sm btn-default">
            <i class="fa fa-list"></i> 返回文件列表
        </a>
    </div>
</div>

<div class="browse-container">
    <!-- 路径导航 -->
    <div class="path-bar">
        <i class="fa fa-folder-open"></i>
        <span class="path-label">当前位置：</span>
        <a href="<?php echo \Helper::url('admin/storage/browseLocal'); ?>" class="path-item">uploads</a>
        <?php if ($current_path): ?>
        <?php 
        $pathParts = explode('/', trim($current_path, '/'));
        $currentPathBuild = '';
        foreach ($pathParts as $part): 
            $currentPathBuild .= '/' . $part;
        ?>
        <span class="path-sep">/</span>
        <a href="<?php echo \Helper::url('admin/storage/browseLocal?path=' . ltrim($currentPathBuild, '/')); ?>" class="path-item"><?php echo $part; ?></a>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- 文件列表 -->
    <div class="file-grid">
        <?php if ($parentPath !== ''): ?>
        <div class="file-item folder-item" onclick="location.href='<?php echo \Helper::url('admin/storage/browseLocal?path=' . $parentPath); ?>'">
            <div class="file-preview">
                <i class="fa fa-arrow-up"></i>
            </div>
            <div class="file-details">
                <div class="file-name">上级目录</div>
                <div class="file-meta"></div>
            </div>
        </div>
        <?php endif; ?>
        
        <?php if (empty($items)): ?>
        <div class="empty-state">
            <i class="fa fa-folder-open"></i>
            <p>此目录为空</p>
        </div>
        <?php else: ?>
        <?php foreach ($items as $item): ?>
        <?php if ($item['is_dir']): ?>
        <div class="file-item folder-item" onclick="location.href='<?php echo \Helper::url('admin/storage/browseLocal?path=' . ($current_path ? $current_path . '/' : '') . $item['name']); ?>'">
            <div class="file-preview">
                <i class="fa fa-folder"></i>
            </div>
            <div class="file-details">
                <div class="file-name"><?php echo \Helper::escape($item['name']); ?></div>
                <div class="file-meta">文件夹</div>
            </div>
        </div>
        <?php else: ?>
        <?php
            $ext = strtolower(pathinfo($item['name'], PATHINFO_EXTENSION));
            $isImage = in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg']);
            $fileUrl = defined('SITE_URL') ? rtrim(SITE_URL, '/') . '/uploads/' . ($current_path ? $current_path . '/' : '') . $item['name'] : '/uploads/' . ($current_path ? $current_path . '/' : '') . $item['name'];
        ?>
        <div class="file-item file-item <?php echo $isImage ? 'image-item' : ''; ?>">
            <div class="file-preview">
                <?php if ($isImage): ?>
                <img src="<?php echo $fileUrl; ?>" alt="<?php echo \Helper::escape($item['name']); ?>">
                <?php else: ?>
                <?php
                    // 简单的文件图标逻辑
                    $icon = 'text-o';
                    if (in_array($ext, ['zip', 'rar', '7z', 'tar', 'gz'])) $icon = 'archive-o';
                    elseif (in_array($ext, ['pdf'])) $icon = 'pdf-o';
                    elseif (in_array($ext, ['doc', 'docx'])) $icon = 'word-o';
                    elseif (in_array($ext, ['xls', 'xlsx', 'csv'])) $icon = 'excel-o';
                    elseif (in_array($ext, ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv'])) $icon = 'video-o';
                    elseif (in_array($ext, ['mp3', 'wav', 'flac', 'aac', 'ogg'])) $icon = 'audio-o';
                    elseif (in_array($ext, ['html', 'css', 'js', 'json', 'xml', 'php', 'py', 'java'])) $icon = 'code-o';
                ?>
                <i class="fa fa-file-<?php echo $icon; ?>"></i>
                <?php endif; ?>
            </div>
            <div class="file-details">
                <div class="file-name" title="<?php echo \Helper::escape($item['name']); ?>"><?php echo \Helper::escape($item['name']); ?></div>
                <div class="file-meta">
                    <span><?php echo $item['size_formatted']; ?></span>
                    <span><?php echo $item['modified']; ?></span>
                </div>
            </div>
            <div class="file-actions">
                <a href="<?php echo $fileUrl; ?>" target="_blank" class="btn-icon" title="查看">
                    <i class="fa fa-eye"></i>
                </a>
                <a href="<?php echo $fileUrl; ?>" download="<?php echo \Helper::escape($item['name']); ?>" class="btn-icon" title="下载">
                    <i class="fa fa-download"></i>
                </a>
                <button onclick="event.stopPropagation(); confirmDelete('<?php echo \Helper::escape(addslashes($item['name'])); ?>', '<?php echo \Helper::escape(($current_path ? $current_path . '/' : '') . $item['name']); ?>')" class="btn-icon text-danger" title="删除">
                    <i class="fa fa-trash"></i>
                </button>
            </div>
        </div>
        <?php endif; ?>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<style>
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.page-title { margin: 0; font-size: 20px; font-weight: 500; }
.page-actions { display: flex; gap: 10px; }
.browse-container { background: #fff; border-radius: 8px; padding: 20px; }
.path-bar { display: flex; align-items: center; padding: 12px 15px; background: #f8f9fa; border-radius: 6px; margin-bottom: 20px; font-size: 14px; }
.path-bar i { margin-right: 10px; color: #1890ff; }
.path-label { color: #666; }
.path-sep { margin: 0 5px; color: #ccc; }
.path-item { color: #1890ff; text-decoration: none; }
.path-item:hover { text-decoration: underline; }
.file-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 15px; }
.file-item { background: #fff; border: 1px solid #eee; border-radius: 8px; padding: 15px; cursor: pointer; transition: all 0.3s; position: relative; }
.file-item:hover { border-color: #1890ff; box-shadow: 0 2px 8px rgba(24, 144, 255, 0.15); }
.file-item.folder-item:hover { background: #f0f7ff; }
.file-preview { width: 100%; height: 80px; display: flex; align-items: center; justify-content: center; margin-bottom: 10px; }
.file-preview img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.file-preview i { font-size: 48px; color: #1890ff; }
.file-preview i.fa-file-archive-o { color: #fa8c16; }
.file-preview i.fa-file-pdf-o { color: #ff4d4f; }
.file-preview i.fa-file-word-o { color: #1890ff; }
.file-preview i.fa-file-excel-o { color: #52c41a; }
.file-preview i.fa-file-video-o { color: #722ed1; }
.file-preview i.fa-file-audio-o { color: #eb2f96; }
.file-preview i.fa-file-code-o { color: #13c2c2; }
.file-preview i.fa-file-text-o { color: #666; }
.file-details { text-align: center; }
.file-name { font-size: 13px; font-weight: 500; color: #333; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 120px; margin: 0 auto; }
.file-meta { font-size: 11px; color: #999; margin-top: 5px; }
.file-meta span { margin: 0 3px; }
.file-actions { position: absolute; top: 8px; right: 8px; display: none; gap: 5px; }
.file-item:hover .file-actions { display: flex; }
.btn-icon { width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; background: #fff; border: 1px solid #ddd; border-radius: 4px; color: #666; cursor: pointer; transition: all 0.3s; }
.btn-icon:hover { background: #1890ff; border-color: #1890ff; color: #fff; }
.btn-icon.text-danger:hover { background: #ff4d4f; border-color: #ff4d4f; color: #fff; }
.empty-state { grid-column: span 4; text-align: center; padding: 50px; color: #999; }
.empty-state i { font-size: 48px; display: block; margin-bottom: 15px; }
@media (max-width: 768px) {
    .file-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
}
</style>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
function confirmDelete(name, path) {
    if (!confirm('确定要删除文件 "' + name + '" 吗？此操作不可恢复。')) {
        return;
    }
    
    $.post('<?php echo \Helper::url("admin/storage/deleteLocalFile"); ?>', {
        path: path,
        csrf_token: $('input[name="csrf_token"]').val()
    }, function(res) {
        if (res.code === 0) {
            alert('删除成功');
            location.reload();
        } else {
            alert(res.message || '删除失败');
        }
    }, 'json').fail(function() {
        alert('请求失败，请重试');
    });
}
</script>
