<?php
require_once __DIR__ . '/../MPHX/common.php';

$user = check_user();

// 清空部署记录
if (isset($_POST['clear_logs']) && $_POST['clear_logs'] == '1') {
    $DB->query("DELETE FROM `bsjl` WHERE `uid`='{$user['id']}'");
    $success = '部署记录已清空';
}

// 获取部署记录
$logs = $DB->query("SELECT b.*, bs.name as program_name, bs.type as program_type 
    FROM `bsjl` b 
    LEFT JOIN `bs` bs ON b.bs_id = bs.id 
    WHERE b.uid='{$user['id']}' 
    ORDER BY b.time DESC");
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo L('deploy_history', 'menu'); ?> - <?php echo $conf['name']; ?></title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
    <style>
        .card-header-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        .card-header-actions h3 {
            margin: 0;
        }
        .btn-clear {
            background: #dc3545;
            color: #fff;
            border: none;
            padding: 6px 16px;
            border-radius: 4px;
            font-size: 13px;
            cursor: pointer;
            transition: background 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        .btn-clear:hover {
            background: #c82333;
        }
        .btn-clear:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }
        .alert {
            padding: 12px 15px;
            border-radius: 4px;
            margin-bottom: 15px;
            font-size: 14px;
        }
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        /* 确认对话框 */
        .modal-confirm {
            display: none;
            position: fixed;
            z-index: 9999;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-confirm-content {
            background-color: #fff;
            margin: 15% auto;
            padding: 0;
            border-radius: 8px;
            width: 90%;
            max-width: 400px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
        }
        .modal-confirm-header {
            padding: 16px 20px;
            border-bottom: 1px solid #e8eaed;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .modal-confirm-header i {
            color: #dc3545;
            font-size: 20px;
        }
        .modal-confirm-header h4 {
            margin: 0;
            font-size: 16px;
            color: #202124;
        }
        .modal-confirm-body {
            padding: 20px;
            color: #5f6368;
            font-size: 14px;
        }
        .modal-confirm-footer {
            padding: 15px 20px;
            border-top: 1px solid #e8eaed;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        .modal-confirm-footer .btn {
            padding: 8px 20px;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            border: none;
        }
        .modal-confirm-footer .btn-cancel {
            background: #f1f3f4;
            color: #5f6368;
        }
        .modal-confirm-footer .btn-cancel:hover {
            background: #e8eaed;
        }
        .modal-confirm-footer .btn-confirm {
            background: #dc3545;
            color: #fff;
        }
        .modal-confirm-footer .btn-confirm:hover {
            background: #c82333;
        }
    </style>
</head>
<body>
    <div class="content-area" style="margin-left: 0; padding: 20px;">
        <div class="card">
            <div class="card-header-actions">
                <h3><i class="fas fa-history"></i> <?php echo L('deploy_history', 'menu'); ?></h3>
                <button type="button" class="btn-clear" onclick="showConfirmModal()" <?php echo empty($logs) ? 'disabled' : ''; ?>>
                    <i class="fas fa-trash-alt"></i> 清空记录
                </button>
            </div>
            
            <?php if (isset($success)): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i> <?php echo $success; ?>
            </div>
            <?php endif; ?>
            
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th style="width: 80px; text-align: center;">ID</th>
                            <th><?php echo L('program_name', 'menu'); ?></th>
                            <th style="width: 120px; text-align: center;"><?php echo L('program_type', 'menu'); ?></th>
                            <th style="width: 150px; text-align: center;"><?php echo L('deploy_time', 'menu'); ?></th>
                            <th style="width: 100px; text-align: center;"><?php echo L('status', 'menu'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($logs as $log): ?>
                        <tr>
                            <td style="text-align: center;"><?php echo $log['id']; ?></td>
                            <td><?php echo htmlspecialchars($log['program_name'] ?: L('unknown_program', 'menu')); ?></td>
                            <td style="text-align: center;">
                                <span class="badge badge-info"><?php echo htmlspecialchars($log['program_type'] ?: '-'); ?></span>
                            </td>
                            <td style="text-align: center;"><?php echo $log['time']; ?></td>
                            <td style="text-align: center;">
                                <span class="badge badge-success"><?php echo htmlspecialchars($log['zt']); ?></span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <?php if (empty($logs)): ?>
            <div class="text-center py-5">
                <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                <p class="text-muted">暂无部署记录</p>
            </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- 确认对话框 -->
    <div id="confirmModal" class="modal-confirm">
        <div class="modal-confirm-content">
            <div class="modal-confirm-header">
                <i class="fas fa-exclamation-triangle"></i>
                <h4>确认清空</h4>
            </div>
            <div class="modal-confirm-body">
                确定要清空所有部署记录吗？此操作不可恢复。
            </div>
            <div class="modal-confirm-footer">
                <button type="button" class="btn btn-cancel" onclick="hideConfirmModal()">取消</button>
                <button type="button" class="btn btn-confirm" onclick="confirmClear()">确定清空</button>
            </div>
        </div>
    </div>
    
    <!-- 隐藏的表单 -->
    <form id="clearForm" method="post" style="display: none;">
        <input type="hidden" name="clear_logs" value="1">
    </form>
    
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        function showConfirmModal() {
            document.getElementById('confirmModal').style.display = 'block';
        }
        
        function hideConfirmModal() {
            document.getElementById('confirmModal').style.display = 'none';
        }
        
        function confirmClear() {
            document.getElementById('clearForm').submit();
        }
        
        // 点击模态框外部关闭
        window.onclick = function(event) {
            var modal = document.getElementById('confirmModal');
            if (event.target == modal) {
                hideConfirmModal();
            }
        }
    </script>
</body>
</html>