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

$user = check_user();
$user_id = intval($user['id']);

// 创建通知设置表（如果不存在）
$create_table_sql = "CREATE TABLE IF NOT EXISTS `user_notification_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `email_notifications` tinyint(1) DEFAULT 1,
  `sms_notifications` tinyint(1) DEFAULT 0,
  `push_notifications` tinyint(1) DEFAULT 1,
  `expire_alert` tinyint(1) DEFAULT 1,
  `renewal_reminder` tinyint(1) DEFAULT 1,
  `traffic_alert` tinyint(1) DEFAULT 1,
  `maintenance_notice` tinyint(1) DEFAULT 1,
  `promotional` tinyint(1) DEFAULT 0,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$DB->query($create_table_sql);

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email_notifications = isset($_POST['email_notifications']) ? 1 : 0;
    $sms_notifications = isset($_POST['sms_notifications']) ? 1 : 0;
    $push_notifications = isset($_POST['push_notifications']) ? 1 : 0;
    $expire_alert = isset($_POST['expire_alert']) ? 1 : 0;
    $renewal_reminder = isset($_POST['renewal_reminder']) ? 1 : 0;
    $traffic_alert = isset($_POST['traffic_alert']) ? 1 : 0;
    $maintenance_notice = isset($_POST['maintenance_notice']) ? 1 : 0;
    $promotional = isset($_POST['promotional']) ? 1 : 0;
    
    // 检查是否已有设置记录
    $existing = $DB->get_row("SELECT id FROM user_notification_settings WHERE user_id = $user_id");
    
    if ($existing) {
        // 更新现有记录
        $sql = "UPDATE user_notification_settings SET 
            email_notifications = $email_notifications,
            sms_notifications = $sms_notifications,
            push_notifications = $push_notifications,
            expire_alert = $expire_alert,
            renewal_reminder = $renewal_reminder,
            traffic_alert = $traffic_alert,
            maintenance_notice = $maintenance_notice,
            promotional = $promotional
        WHERE user_id = $user_id";
    } else {
        // 插入新记录
        $sql = "INSERT INTO user_notification_settings 
            (user_id, email_notifications, sms_notifications, push_notifications, 
            expire_alert, renewal_reminder, traffic_alert, maintenance_notice, promotional)
        VALUES 
            ($user_id, $email_notifications, $sms_notifications, $push_notifications,
            $expire_alert, $renewal_reminder, $traffic_alert, $maintenance_notice, $promotional)";
    }
    
    $DB->query($sql);
    
    $success_message = '通知设置已保存';
}

// 获取当前通知设置
$result = $DB->get_row("SELECT * FROM user_notification_settings WHERE user_id = $user_id");

// 默认设置
$settings = array(
    'email_notifications' => 1,
    'sms_notifications' => 0,
    'push_notifications' => 1,
    'expire_alert' => 1,
    'renewal_reminder' => 1,
    'traffic_alert' => 1,
    'maintenance_notice' => 1,
    'promotional' => 0
);

// 如果有设置记录，覆盖默认值
if ($result) {
    $settings = array(
        'email_notifications' => isset($result['email_notifications']) ? $result['email_notifications'] : 1,
        'sms_notifications' => isset($result['sms_notifications']) ? $result['sms_notifications'] : 0,
        'push_notifications' => isset($result['push_notifications']) ? $result['push_notifications'] : 1,
        'expire_alert' => isset($result['expire_alert']) ? $result['expire_alert'] : 1,
        'renewal_reminder' => isset($result['renewal_reminder']) ? $result['renewal_reminder'] : 1,
        'traffic_alert' => isset($result['traffic_alert']) ? $result['traffic_alert'] : 1,
        'maintenance_notice' => isset($result['maintenance_notice']) ? $result['maintenance_notice'] : 1,
        'promotional' => isset($result['promotional']) ? $result['promotional'] : 0
    );
}
?>

<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>
    /* 确保整个页面所有元素始终可见 */
    html, body, div, span, button, input, label, form, h2, h3, p, i, .switch, .slider {
        opacity: 1 !important;
        visibility: visible !important;
    }

    .settings-container {
        padding: 24px;
        max-width: 700px;
        margin: 0 auto;
    }
    .panel-header {
        margin-bottom: 24px;
        text-align: center;
    }
    .panel-header h2 {
        color: #188038;
        font-size: 20px;
        font-weight: 600;
        margin: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
    }
    .panel-header p {
        color: #5f6368;
        font-size: 13px;
        margin: 8px 0 0 0;
    }
    .settings-card {
        background: #fff;
        border-radius: 8px;
        padding: 24px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        border: 1px solid #e8eaed;
        margin-bottom: 20px;
    }
    .settings-card-title {
        font-size: 16px;
        font-weight: 600;
        color: #202124;
        margin-bottom: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .settings-card-title i {
        color: #188038;
    }
    .settings-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px 0;
        border-bottom: 1px solid #e8eaed;
    }
    .settings-item:last-child {
        border-bottom: none;
    }
    .settings-item-content {
        flex: 1;
    }
    .settings-item-title {
        font-size: 14px;
        font-weight: 500;
        color: #202124;
        margin-bottom: 4px;
    }
    .settings-item-desc {
        font-size: 12px;
        color: #5f6368;
    }
    .switch {
        position: relative;
        display: inline-block;
        width: 44px;
        height: 24px;
    }
    .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: 24px;
    }
    .slider:before {
        position: absolute;
        content: "";
        height: 18px;
        width: 18px;
        left: 3px;
        bottom: 3px;
        background-color: white;
        transition: .4s;
        border-radius: 50%;
    }
    input:checked + .slider {
        background-color: #188038;
    }
    input:checked + .slider:before {
        transform: translateX(20px);
    }
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        border-radius: 6px;
        font-size: 14px;
        font-weight: 500;
        padding: 10px 24px;
        cursor: pointer;
        transition: background 0.2s;
        border: none;
        text-decoration: none;
    }
    .btn-primary {
        background: #188038;
        color: #fff;
    }
    .btn-primary:hover {
        background: #137333;
    }
    .alert {
        padding: 12px 16px;
        border-radius: 6px;
        margin-bottom: 20px;
    }
    .alert-success {
        background: #e8f5e9;
        color: #2e7d32;
        border: 1px solid #c8e6c9;
    }
    .info-box {
        background: #e3f2fd;
        border-radius: 8px;
        padding: 16px;
        margin-bottom: 20px;
        border: 1px solid #bbdefb;
    }
    .info-box p {
        margin: 0;
        font-size: 13px;
        color: #1565c0;
    }
    @media (max-width: 768px) {
        .settings-container {
            padding: 16px;
        }
        .settings-card {
            padding: 16px;
        }
        .settings-item {
            flex-direction: column;
            align-items: flex-start;
            gap: 12px;
        }
    }
</style>

<div class="settings-container">
    <div class="panel-header">
        <h2><i class="fas fa-bell"></i> <?php echo L('title', 'notification_settings'); ?></h2>
        <p><?php echo L('my_notifications', 'notifications'); ?></p>
    </div>
    
    <?php if (isset($success_message)): ?>
    <div class="alert alert-success">
        <i class="fas fa-check-circle"></i> <?php echo htmlspecialchars($success_message); ?>
    </div>
    <?php endif; ?>
    
    <div class="info-box">
        <p><i class="fas fa-info-circle"></i> <?php echo L('notification_settings', 'menu'); ?></p>
    </div>
    
    <form method="post" action="notification-settings.php">
        <div class="settings-card">
            <div class="settings-card-title">
                <i class="fas fa-envelope"></i> <?php echo L('email_notification', 'notification_settings'); ?>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title"><?php echo L('email_notification', 'notification_settings'); ?></div>
                    <div class="settings-item-desc">通过邮件接收重要通知</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="email_notifications" <?php echo $settings['email_notifications'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">短信通知</div>
                    <div class="settings-item-desc">通过短信接收重要提醒</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="sms_notifications" <?php echo $settings['sms_notifications'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">站内推送</div>
                    <div class="settings-item-desc">在网站内接收实时通知</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="push_notifications" <?php echo $settings['push_notifications'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
        </div>
        
        <div class="settings-card">
            <div class="settings-card-title">
                <i class="fas fa-bell"></i> 通知类型
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">到期提醒</div>
                    <div class="settings-item-desc">服务到期前的提醒通知</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="expire_alert" <?php echo $settings['expire_alert'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">续费提醒</div>
                    <div class="settings-item-desc">续费优惠活动通知</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="renewal_reminder" <?php echo $settings['renewal_reminder'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">流量告警</div>
                    <div class="settings-item-desc">流量使用达到阈值时的提醒</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="traffic_alert" <?php echo $settings['traffic_alert'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">维护公告</div>
                    <div class="settings-item-desc">系统维护和升级通知</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="maintenance_notice" <?php echo $settings['maintenance_notice'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
            
            <div class="settings-item">
                <div class="settings-item-content">
                    <div class="settings-item-title">推广优惠</div>
                    <div class="settings-item-desc">最新优惠活动和促销信息</div>
                </div>
                <label class="switch">
                    <input type="checkbox" name="promotional" <?php echo $settings['promotional'] ? 'checked' : ''; ?>>
                    <span class="slider"></span>
                </label>
            </div>
        </div>
        
        <div style="text-align: center;">
            <button type="submit" class="btn btn-primary">
                <i class="fas fa-save"></i> 保存设置
            </button>
        </div>
    </form>
</div>
