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

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

$admin = is_admin_login();

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $expire_pause_days = intval($_POST['expire_pause_days'] ?? 0);
    $pause_delete_days = intval($_POST['pause_delete_days'] ?? 7);
    $expire_check_time = $_POST['expire_check_time'] ?? '01:00';
    $delete_notify_days = intval($_POST['delete_notify_days'] ?? 3);
    $auto_delete_enabled = intval($_POST['auto_delete_enabled'] ?? 1);
    
    // 验证时间格式
    if (!preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/', $expire_check_time)) {
        $expire_check_time = '01:00';
    }
    
    // 更新设置
    $DB->query("UPDATE `expire_settings` SET `setting_value` = '{$expire_pause_days}' WHERE `setting_key` = 'expire_pause_days'");
    $DB->query("UPDATE `expire_settings` SET `setting_value` = '{$pause_delete_days}' WHERE `setting_key` = 'pause_delete_days'");
    $DB->query("UPDATE `expire_settings` SET `setting_value` = '{$expire_check_time}' WHERE `setting_key` = 'expire_check_time'");
    $DB->query("UPDATE `expire_settings` SET `setting_value` = '{$delete_notify_days}' WHERE `setting_key` = 'delete_notify_days'");
    $DB->query("UPDATE `expire_settings` SET `setting_value` = '{$auto_delete_enabled}' WHERE `setting_key` = 'auto_delete_enabled'");
    
    $success_msg = '设置已保存';
}

// 获取当前设置
$settings = array();
$result = $DB->query("SELECT * FROM `expire_settings`");
while ($row = $DB->fetch($result)) {
    $settings[$row['setting_key']] = $row;
}

$expire_pause_days = intval($settings['expire_pause_days']['setting_value'] ?? 0);
$pause_delete_days = intval($settings['pause_delete_days']['setting_value'] ?? 7);
$expire_check_time = $settings['expire_check_time']['setting_value'] ?? '01:00';
$delete_notify_days = intval($settings['delete_notify_days']['setting_value'] ?? 3);
$auto_delete_enabled = intval($settings['auto_delete_enabled']['setting_value'] ?? 1);
?>
<!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>
        .settings-card {
            background: #fff;
            border-radius: 8px;
            padding: 24px;
            margin-bottom: 20px;
            box-shadow: 0 1px 2px rgba(60,64,67,0.1);
        }
        .settings-card h3 {
            color: #202124;
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 1px solid #e8eaed;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            color: #202124;
            font-size: 14px;
            font-weight: 500;
            margin-bottom: 8px;
        }
        .form-group .description {
            color: #5f6368;
            font-size: 12px;
            margin-top: 4px;
        }
        .form-control {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #dadce0;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.2s;
        }
        .form-control:focus {
            outline: none;
            border-color: #1a73e8;
        }
        .form-control.small {
            width: 120px;
            display: inline-block;
        }
        .switch {
            position: relative;
            display: inline-block;
            width: 50px;
            height: 26px;
        }
        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 26px;
        }
        .slider:before {
            position: absolute;
            content: "";
            height: 18px;
            width: 18px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }
        input:checked + .slider {
            background-color: #1a73e8;
        }
        input:checked + .slider:before {
            transform: translateX(24px);
        }
        .btn-save {
            background: #1a73e8;
            color: #fff;
            border: none;
            border-radius: 4px;
            padding: 10px 24px;
            font-size: 14px;
            cursor: pointer;
            transition: background 0.2s;
        }
        .btn-save:hover {
            background: #1557b0;
        }
        .alert-success {
            background: #e6f4ea;
            color: #137333;
            border: 1px solid #ceead6;
            border-radius: 4px;
            padding: 12px 16px;
            margin-bottom: 20px;
        }
        .timeline {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin-top: 20px;
        }
        .timeline h4 {
            color: #202124;
            font-size: 14px;
            margin-bottom: 16px;
        }
        .timeline-item {
            display: flex;
            align-items: flex-start;
            margin-bottom: 16px;
            padding-left: 24px;
            position: relative;
        }
        .timeline-item:before {
            content: '';
            position: absolute;
            left: 6px;
            top: 6px;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: #1a73e8;
        }
        .timeline-item:not(:last-child):after {
            content: '';
            position: absolute;
            left: 10px;
            top: 20px;
            width: 2px;
            height: calc(100% + 6px);
            background: #dadce0;
        }
        .timeline-content {
            flex: 1;
        }
        .timeline-title {
            color: #202124;
            font-size: 14px;
            font-weight: 500;
        }
        .timeline-desc {
            color: #5f6368;
            font-size: 12px;
            margin-top: 2px;
        }
    </style>
</head>
<body>
    <div class="content-area">
        <div class="alert">
            <i class="fas fa-cog"></i> 
            <strong>定时任务设置</strong> | 配置到期主机的自动暂停和删除策略
        </div>

        <?php if (isset($success_msg)): ?>
        <div class="alert-success">
            <i class="fas fa-check-circle"></i> <?php echo $success_msg; ?>
        </div>
        <?php endif; ?>

        <form method="post">
            <div class="row" style="margin: 0 -10px;">
                <div class="col-md-8" style="padding: 0 10px;">
                    <div class="settings-card">
                        <h3><i class="fas fa-stop-circle"></i> 到期暂停设置</h3>
                        
                        <div class="form-group">
                            <label>到期后暂停天数</label>
                            <input type="number" name="expire_pause_days" class="form-control small" value="<?php echo $expire_pause_days; ?>" min="0" max="30">
                            <span style="margin-left: 8px; color: #5f6368;">天</span>
                            <div class="description">主机到期后多少天自动暂停（0表示到期当天立即暂停）</div>
                        </div>

                        <div class="form-group">
                            <label>到期检查时间</label>
                            <input type="time" name="expire_check_time" class="form-control small" value="<?php echo $expire_check_time; ?>">
                            <div class="description">每天执行到期检查的时间（建议设置为凌晨低峰期）</div>
                        </div>
                    </div>

                    <div class="settings-card">
                        <h3><i class="fas fa-trash-alt"></i> 自动删除设置</h3>
                        
                        <div class="form-group">
                            <label>启用自动删除</label>
                            <br>
                            <label class="switch">
                                <input type="checkbox" name="auto_delete_enabled" value="1" <?php echo $auto_delete_enabled ? 'checked' : ''; ?>>
                                <span class="slider"></span>
                            </label>
                            <div class="description">开启后，系统会自动删除已暂停超过指定天数的主机</div>
                        </div>

                        <div class="form-group">
                            <label>暂停后删除天数</label>
                            <input type="number" name="pause_delete_days" class="form-control small" value="<?php echo $pause_delete_days; ?>" min="1" max="90">
                            <span style="margin-left: 8px; color: #5f6368;">天</span>
                            <div class="description">主机暂停后多少天自动删除（包含网站、数据库、FTP）</div>
                        </div>

                        <div class="form-group">
                            <label>删除前通知天数</label>
                            <input type="number" name="delete_notify_days" class="form-control small" value="<?php echo $delete_notify_days; ?>" min="0" max="30">
                            <span style="margin-left: 8px; color: #5f6368;">天</span>
                            <div class="description">删除前多少天发送通知提醒用户（0表示不发送通知）</div>
                        </div>
                    </div>

                    <button type="submit" class="btn-save">
                        <i class="fas fa-save"></i> 保存设置
                    </button>
                    <a href="expire_manage.php" class="btn" style="margin-left: 10px; background: #f1f3f4; color: #202124; padding: 10px 20px; border-radius: 4px; text-decoration: none;">
                        <i class="fas fa-arrow-left"></i> 返回到期管理
                    </a>
                </div>

                <div class="col-md-4" style="padding: 0 10px;">
                    <div class="settings-card">
                        <h3><i class="fas fa-info-circle"></i> 执行流程说明</h3>
                        
                        <div class="timeline">
                            <h4>主机生命周期</h4>
                            
                            <div class="timeline-item">
                                <div class="timeline-content">
                                    <div class="timeline-title">1. 主机到期</div>
                                    <div class="timeline-desc">到达到期时间</div>
                                </div>
                            </div>
                            
                            <div class="timeline-item">
                                <div class="timeline-content">
                                    <div class="timeline-title">2. 宽限期</div>
                                    <div class="timeline-desc">等待 <?php echo $expire_pause_days; ?> 天后暂停</div>
                                </div>
                            </div>
                            
                            <div class="timeline-item">
                                <div class="timeline-content">
                                    <div class="timeline-title">3. 自动暂停</div>
                                    <div class="timeline-desc">停止网站访问</div>
                                </div>
                            </div>
                            
                            <div class="timeline-item">
                                <div class="timeline-content">
                                    <div class="timeline-title">4. 删除通知</div>
                                    <div class="timeline-desc">删除前 <?php echo $delete_notify_days; ?> 天发送通知</div>
                                </div>
                            </div>
                            
                            <div class="timeline-item">
                                <div class="timeline-content">
                                    <div class="timeline-title">5. 自动删除</div>
                                    <div class="timeline-desc">暂停 <?php echo $pause_delete_days; ?> 天后删除所有数据</div>
                                </div>
                            </div>
                        </div>

                        <div style="margin-top: 20px; padding: 16px; background: #fef7e0; border-radius: 8px; border-left: 4px solid #f9ab00;">
                            <h4 style="color: #b06000; font-size: 13px; margin-bottom: 8px;"><i class="fas fa-exclamation-triangle"></i> 注意事项</h4>
                            <ul style="color: #5f6368; font-size: 12px; line-height: 1.8; margin: 0; padding-left: 16px;">
                                <li>删除操作不可恢复，请谨慎设置</li>
                                <li>建议开启删除前通知功能</li>
                                <li>暂停后用户数据仍保留</li>
                                <li>删除后会清理网站、数据库、FTP</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</body>
</html>
