{include file="app/manage/view/index/emlog_nav"}

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-puzzle-piece"></i>
    插件市场
</h1>

<?php if (!empty($needUpgrade)) { ?>
<!-- 升级提示 -->
<div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
    <i class="fas fa-exclamation-triangle mr-2"></i>
    <strong>插件表结构需要升级</strong> — 检测到旧的插件注册表结构，标准化插件系统需要新的字段。点击下方按钮一键重建（原有数据将被清空）。
    <a href="index.php?r=manage/plugin/upgradeTable" class="btn btn-danger btn-sm ml-3"
       onclick="return confirm('确定升级插件表结构吗？原有插件数据将清空。');">
        <i class="fas fa-sync-alt"></i> 一键升级插件表结构
    </a>
</div>
<?php } ?>

<!-- 工具栏：上传安装 + 统计 -->
<div class="filter-bar mb-4">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center">
        <div class="filter-title mb-3 mb-md-0">
            <i class="fas fa-plug"></i>
            <span>已安装插件 <span class="small font-weight-light">(<span id="count_active"><?php echo count((array)$list); ?></span>)</span></span>
        </div>
        <div class="d-flex flex-wrap" style="gap: 10px;">
            <button class="btn btn-success btn-sm shadow-sm" data-toggle="modal" data-target="#addModal">
                <i class="fas fa-plus"></i> 上传安装插件
            </button>
        </div>
    </div>
</div>

<!-- 已安装插件（表格列表，参考 emlog 风格，非卡片式） -->
<div class="card shadow mb-4">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-striped table-hover plugin-table mb-0">
                <thead>
                    <tr>
                        <th style="width:34%;">插件</th>
                        <th style="width:10%;">状态</th>
                        <th style="width:12%;">类型</th>
                        <th style="width:12%;">作者</th>
                        <th style="width:10%;">版本</th>
                        <th style="width:22%;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($list)) { ?>
                        <?php foreach ($list as $row) {
                            $valid = !empty($row['_valid']);
                            $type = $row['_type'] ?? 'native';
                            $initial = mb_substr($row['name'] ?? $row['alias'], 0, 1);
                            $colors = ['native' => '#4e73df', 'zblog' => '#E6A23C', 'emlog' => '#67C23A', 'wordpress' => '#0073aa'];
                            $color = $colors[$type] ?? '#4e73df';
                            $typeLabels = ['zblog' => 'Z-Blog', 'emlog' => 'Emlog', 'wordpress' => 'WP'];
                            if (!$valid) $color = '#e74a3b';
                            $typeBadge = isset($typeLabels[$type]) ? $typeLabels[$type] : '原生';
                            $typeBadgeClass = $type === 'zblog' ? 'badge-warning' : ($type === 'emlog' ? 'badge-success' : ($type === 'wordpress' ? 'badge-info' : 'badge-secondary'));
                        ?>
                        <tr>
                            <td class="plugin-cell">
                                <div class="d-flex align-items-center">
                                    <div class="flex-shrink-0 mr-3">
                                        <div class="rounded-lg d-flex align-items-center justify-content-center text-white font-weight-bold" style="width: 42px; height: 42px; background: <?php echo $color; ?>; font-size: 16px;">
                                            <?php echo htmlspecialchars($initial); ?>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 min-width-0">
                                        <div class="font-weight-bold" style="font-size: 15px; color: var(--zc-text);">
                                            <?php echo htmlspecialchars($row['name'] ?? $row['alias']); ?>
                                        </div>
                                        <div class="text-muted small" style="white-space: normal; line-height: 1.5;">
                                            <?php echo htmlspecialchars($row['description'] ?? ''); ?>
                                            <?php if (!empty($row['url'])): ?>
                                                <a href="<?php echo htmlspecialchars($row['url']); ?>" target="_blank" class="text-muted ml-1" title="插件主页"><i class="fas fa-external-link-alt"></i></a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <?php if (!$valid): ?>
                                    <span class="badge badge-danger"><i class="fas fa-exclamation-circle mr-1"></i>文件缺失</span>
                                <?php elseif ($row['status'] == 1): ?>
                                    <span class="badge badge-success"><i class="fas fa-check mr-1"></i>启用</span>
                                <?php else: ?>
                                    <span class="badge badge-secondary"><i class="fas fa-pause mr-1"></i>停用</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="badge <?php echo $typeBadgeClass; ?>"><?php echo $typeBadge; ?></span>
                            </td>
                            <td class="text-muted small">
                                <?php echo htmlspecialchars($row['author'] ?? ''); ?>
                            </td>
                            <td class="text-muted small">
                                v<?php echo htmlspecialchars($row['version'] ?? '1.0.0'); ?>
                            </td>
                            <td>
                                <div class="d-flex flex-wrap" style="gap: 6px;">
                                    <?php if ($valid && !empty($row['hasSetting'])): ?>
                                        <a href="index.php?r=manage/plugin/setting&alias=<?php echo $row['alias']; ?>" class="btn btn-primary btn-sm">
                                            <i class="fas fa-cog"></i> 设置
                                        </a>
                                    <?php endif; ?>
                                    <?php if ($valid && !empty($row['hasShow'])): ?>
                                        <a href="index.php?r=plug/plugin/show&alias=<?php echo $row['alias']; ?>" target="_blank" class="btn btn-outline-info btn-sm">
                                            <i class="fas fa-eye"></i> 前台
                                        </a>
                                    <?php endif; ?>
                                    <?php if ($valid): ?>
                                        <?php if ($row['status'] == 1): ?>
                                            <a href="index.php?r=manage/plugin/disable&alias=<?php echo $row['alias']; ?>" class="btn btn-outline-warning btn-sm">
                                                <i class="fas fa-power-off"></i> 停用
                                            </a>
                                        <?php else: ?>
                                            <a href="index.php?r=manage/plugin/enable&alias=<?php echo $row['alias']; ?>" class="btn btn-outline-success btn-sm">
                                                <i class="fas fa-play"></i> 启用
                                            </a>
                                        <?php endif; ?>
                                        <a href="index.php?r=manage/plugin/uninstall&alias=<?php echo $row['alias']; ?>" class="btn btn-outline-danger btn-sm" onclick="return confirm('确定卸载该插件吗？');">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                        <a href="index.php?r=manage/plugin/uninstall&alias=<?php echo $row['alias']; ?>&del=1" class="btn btn-outline-secondary btn-sm" onclick="return confirm('确定卸载并删除插件文件吗？此操作不可恢复！');" title="卸载并删除文件">
                                            <i class="fas fa-eraser"></i>
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                        <?php } ?>
                    <?php } else { ?>
                        <tr>
                            <td colspan="6">
                                <div class="empty-state">
                                    <i class="fas fa-plug fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">暂无已安装插件</h5>
                                    <p class="text-muted">上传或安装一个插件吧</p>
                                </div>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 待安装插件（已上传未安装） -->
<?php if (!empty($available)) { ?>
<div class="filter-bar mb-4">
    <div class="filter-title">
        <i class="fas fa-box-open"></i>
        <span>待安装插件（已上传未安装） <span class="small font-weight-light">(<?php echo count($available); ?>)</span></span>
    </div>
</div>
<div class="card shadow mb-4">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-striped table-hover plugin-table mb-0">
                <thead>
                    <tr>
                        <th style="width:46%;">插件</th>
                        <th style="width:14%;">类型</th>
                        <th style="width:14%;">作者</th>
                        <th style="width:10%;">版本</th>
                        <th style="width:16%;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($available as $meta) {
                        $at = $meta['type'] ?? '';
                        $aColor = '#36b9cc';
                        if ($at === 'zblog') $aColor = '#E6A23C';
                        elseif ($at === 'emlog') $aColor = '#67C23A';
                        elseif ($at === 'wordpress') $aColor = '#0073aa';
                        $alt = ['zblog' => 'Z-Blog', 'emlog' => 'Emlog', 'wordpress' => 'WP'];
                        $aBadge = isset($alt[$at]) ? $alt[$at] : '原生';
                        $aBadgeClass = $at === 'zblog' ? 'badge-warning' : ($at === 'emlog' ? 'badge-success' : ($at === 'wordpress' ? 'badge-info' : 'badge-secondary'));
                        $aInitial = mb_substr($meta['name'] ?? $meta['alias'], 0, 1);
                    ?>
                    <tr>
                        <td class="plugin-cell">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0 mr-3">
                                    <div class="rounded-lg d-flex align-items-center justify-content-center text-white font-weight-bold" style="width: 42px; height: 42px; background: <?php echo $aColor; ?>; font-size: 16px;">
                                        <?php echo htmlspecialchars($aInitial); ?>
                                    </div>
                                </div>
                                <div class="flex-grow-1 min-width-0">
                                    <div class="font-weight-bold" style="font-size: 15px; color: var(--zc-text);">
                                        <?php echo htmlspecialchars($meta['name'] ?? $meta['alias']); ?>
                                    </div>
                                    <div class="text-muted small" style="white-space: normal; line-height: 1.5;">
                                        <?php echo htmlspecialchars($meta['description'] ?? ''); ?>
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td><span class="badge badge-light">未安装</span> <span class="badge <?php echo $aBadgeClass; ?> ml-1" style="font-size:10px;"><?php echo $aBadge; ?></span></td>
                        <td class="text-muted small"><?php echo htmlspecialchars($meta['author'] ?? ''); ?></td>
                        <td class="text-muted small">v<?php echo htmlspecialchars($meta['version'] ?? '1.0.0'); ?></td>
                        <td>
                            <a href="index.php?r=manage/plugin/install&alias=<?php echo $meta['alias']; ?>" class="btn btn-success btn-sm">
                                <i class="fas fa-plus-circle"></i> 安装并启用
                            </a>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php } ?>

<!-- 上传安装插件模态框（参考 emlog addModal） -->
<div class="modal fade" id="addModal">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <div class="modal-header border-0">
                <h5 class="modal-title"><i class="fas fa-upload mr-2"></i>上传安装插件</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="index.php?r=manage/plugin/upload" method="post" enctype="multipart/form-data">
                <div class="modal-body px-4">
                    <div class="custom-file mb-2">
                        <input type="file" class="custom-file-input" name="file" id="pluginFile" accept=".zip,.zba">
                        <label class="custom-file-label" for="pluginFile">选择插件文件 (.zip / .zba)</label>
                    </div>
                    <small class="form-text text-muted mt-2">
                        支持 .zip（原生/Z-Blog/WordPress/Emlog）和 .zba（Z-Blog App 包）
                    </small>
                </div>
                <div class="modal-footer border-0">
                    <button type="button" class="btn btn-sm btn-light" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-sm btn-success"><i class="fas fa-cloud-upload-alt"></i> 上传并解压</button>
                </div>
            </form>
        </div>
    </div>
</div>

{include file="app/manage/view/index/emlog_footer"}

<style>
/* 插件表格：第一列允许换行（覆盖全局 nowrap !important，避免描述被截断） */
table.plugin-table td.plugin-cell {
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: clip !important;
}
table.plugin-table td .min-width-0 {
    min-width: 0;
}
</style>

<script>
// 上传文件选择后更新 label
document.getElementById('pluginFile').addEventListener('change', function() {
    var fileName = this.files[0] ? this.files[0].name : '选择插件文件 (.zip / .zba)';
    this.nextElementSibling.textContent = fileName;
});
</script>
