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

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

$admin = is_admin_login();
$today = date('Y-m-d');

// 手动执行自动停止
if (isset($_POST['action']) && $_POST['action'] == 'run_stop') {
    $output = [];
    $return_var = 0;
    exec('php ' . __DIR__ . '/../cron/auto_stop_expire.php 2>&1', $output, $return_var);
    $result = implode("\n", $output);
    $exec_result = '<div class="alert" style="background: #e6f4ea; color: #137333; border-color: #ceead6;"><i class="fas fa-check-circle"></i> 执行完成：<pre style="margin: 10px 0 0 0; background: #f5f5f5; padding: 10px; border-radius: 4px; color: #333;">' . htmlspecialchars($result) . '</pre></div>';
    // 执行完成后刷新页面以获取最新统计数据
    header('Location: /admin/expire_manage.php?exec=success');
    exit;
}

// 显示执行结果消息
if (isset($_GET['exec']) && $_GET['exec'] == 'success') {
    $exec_result = '<div class="alert" style="background: #e6f4ea; color: #137333; border-color: #ceead6;"><i class="fas fa-check-circle"></i> 执行完成：到期用户网站已成功停止</div>';
}

// 获取统计数据
$expired_count = $DB->count("SELECT COUNT(*) FROM `zj` WHERE `dqsj` != '' AND `dqsj` < '{$today}' AND `zt`='正常'");
$upcoming_count = $DB->count("SELECT COUNT(*) FROM `zj` WHERE `dqsj` != '' AND `dqsj` >= '{$today}' AND `dqsj` <= DATE_ADD('{$today}', INTERVAL 7 DAY) AND `zt`='正常'");
$stopped_count = $DB->count("SELECT COUNT(*) FROM `zj` WHERE `zt`='已过期'");
$deleted_count = $DB->count("SELECT COUNT(*) FROM `log` WHERE `lx`='到期自动删除'");

$expired_users = [];
$expired_result = $DB->query("SELECT * FROM `zj` WHERE `dqsj` != '' AND `dqsj` < '{$today}' AND `zt`='正常' ORDER BY `dqsj` ASC");
while ($row = $DB->fetch($expired_result)) {
    $expired_users[] = $row;
}

$upcoming_users = [];
$upcoming_result = $DB->query("SELECT * FROM `zj` WHERE `dqsj` != '' AND `dqsj` >= '{$today}' AND `dqsj` <= DATE_ADD('{$today}', INTERVAL 7 DAY) AND `zt`='正常' ORDER BY `dqsj` ASC");
while ($row = $DB->fetch($upcoming_result)) {
    $row['days_left'] = ceil((strtotime($row['dqsj']) - strtotime($today)) / (60 * 60 * 24));
    $upcoming_users[] = $row;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>到期管理 - 宝塔主机管理系统</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?v=2026040501">
    <style>
        .tab-nav { display: flex; border-bottom: 1px solid #e8eaed; margin-bottom: 18px; }
        .tab-item { padding: 10px 16px; cursor: pointer; border-bottom: 2px solid transparent; transition: all 0.2s; color: #5f6368; font-size: 13px; }
        .tab-item:hover { color: #1a73e8; }
        .tab-item.active { color: #1a73e8; border-bottom-color: #1a73e8; font-weight: 500; }
        .tab-content { display: none; }
        .tab-content.active { display: block; }
        .action-bar { margin-bottom: 18px; }
        .empty-state { text-align: center; padding: 40px; color: #5f6368; }
        .empty-state i { font-size: 48px; color: #34a853; margin-bottom: 15px; }

    </style>
</head>
<body>
    <div class="content-area">
        <div class="alert">
            <i class="fas fa-info-circle"></i> 
            <strong>到期自动停止管理</strong> | 系统每天凌晨1点自动检查并停止到期用户网站
        </div>

        <?php if (isset($exec_result)) echo $exec_result; ?>

        <div class="stats-grid">
            <div class="stat-card">
                <div class="icon" style="background: #fce8e6; color: #c5221f;">
                    <i class="fas fa-exclamation-circle"></i>
                </div>
                <div class="stat-info">
                    <h4>已到期未停止</h4>
                    <div class="number"><?php echo $expired_count; ?></div>
                </div>
                <div class="progress">
                    <div class="progress-bar" style="width: 100%; background: #c5221f;"></div>
                </div>
            </div>
            <div class="stat-card">
                <div class="icon" style="background: #fef7e0; color: #b06000;">
                    <i class="fas fa-clock"></i>
                </div>
                <div class="stat-info">
                    <h4>7天内到期</h4>
                    <div class="number"><?php echo $upcoming_count; ?></div>
                </div>
                <div class="progress">
                    <div class="progress-bar" style="width: 100%; background: #b06000;"></div>
                </div>
            </div>
            <div class="stat-card">
                <div class="icon" style="background: #e6f4ea; color: #137333;">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div class="stat-info">
                    <h4>已过期停止</h4>
                    <div class="number"><?php echo $stopped_count; ?></div>
                </div>
                <div class="progress">
                    <div class="progress-bar" style="width: 100%; background: #137333;"></div>
                </div>
            </div>
            <div class="stat-card">
                <div class="icon" style="background: #e8f0fe; color: #1a73e8;">
                    <i class="fas fa-trash-alt"></i>
                </div>
                <div class="stat-info">
                    <h4>已删除用户</h4>
                    <div class="number"><?php echo $deleted_count; ?></div>
                </div>
                <div class="progress">
                    <div class="progress-bar" style="width: 100%;"></div>
                </div>
            </div>
        </div>

        <div class="action-bar">
            <form method="post" style="display: inline;">
                <input type="hidden" name="action" value="run_stop">
                <button type="submit" class="btn btn-danger" onclick="return confirm('确定要立即执行自动停止吗？这将停止所有已到期的用户网站。');">
                    <i class="fas fa-stop-circle"></i> 立即执行自动停止
                </button>
            </form>
            <a href="expire_settings.php" class="btn btn-primary" style="margin-left: 10px;">
                <i class="fas fa-cog"></i> 定时任务设置
            </a>
        </div>

        <div class="tab-nav">
            <div class="tab-item active" onclick="switchTab('expired')">
                <i class="fas fa-exclamation-triangle"></i> 已到期未停止 (<?php echo $expired_count; ?>)
            </div>
            <div class="tab-item" onclick="switchTab('upcoming')">
                <i class="fas fa-bell"></i> 即将到期 (<?php echo $upcoming_count; ?>)
            </div>
        </div>

        <div id="tab-expired" class="tab-content active">
            <div class="card">
                <h3><i class="fas fa-list"></i> 已到期未停止用户列表</h3>
                <?php if (count($expired_users) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>到期时间</th>
                            <th>已过期天数</th>
                            <th>网站ID</th>
                            <th>状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($expired_users as $user): 
                            $expired_days = floor((strtotime($today) - strtotime($user['dqsj'])) / (60 * 60 * 24));
                        ?>
                        <tr>
                            <td><?php echo htmlspecialchars($user['user']); ?></td>
                            <td><?php echo $user['dqsj']; ?></td>
                            <td><span class="badge badge-danger"><?php echo $expired_days; ?> 天</span></td>
                            <td><?php echo $user['webid']; ?></td>
                            <td><span class="badge badge-warning"><?php echo $user['zt']; ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-check-circle"></i>
                    <p>没有已到期未停止的用户</p>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div id="tab-upcoming" class="tab-content">
            <div class="card">
                <h3><i class="fas fa-list"></i> 7天内即将到期用户列表</h3>
                <?php if (count($upcoming_users) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>到期时间</th>
                            <th>剩余天数</th>
                            <th>网站ID</th>
                            <th>状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($upcoming_users as $user): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($user['user']); ?></td>
                            <td><?php echo $user['dqsj']; ?></td>
                            <td>
                                <?php if ($user['days_left'] <= 3): ?>
                                <span class="badge badge-danger"><?php echo $user['days_left']; ?> 天</span>
                                <?php else: ?>
                                <span class="badge badge-warning"><?php echo $user['days_left']; ?> 天</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo $user['webid']; ?></td>
                            <td><span class="badge badge-success"><?php echo $user['zt']; ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-check-circle"></i>
                    <p>7天内没有即将到期的用户</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script>
    function switchTab(tab) {
        $('.tab-item').removeClass('active');
        $('.tab-content').removeClass('active');
        
        if (tab === 'expired') {
            $('.tab-item:first').addClass('active');
            $('#tab-expired').addClass('active');
        } else {
            $('.tab-item:last').addClass('active');
            $('#tab-upcoming').addClass('active');
        }
    }
    </script>
</body>
</html>
