<?php
// index.php - 主页面
require_once 'BookingSystem.php';

$bookingSystem = new BookingSystem();
$availabilityMap = $bookingSystem->getNext7DaysAvailability();

// 处理表单提交
$message = '';
$messageType = ''; // success, error

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $masterQQ = trim($_POST['master_qq'] ?? '');
    $diceQQ = trim($_POST['dice_qq'] ?? '');
    $bookingDate = $_POST['booking_date'] ?? '';
    
    $result = $bookingSystem->createBooking($masterQQ, $diceQQ, $bookingDate);
    
    if ($result['success']) {
        $message = $result['message'];
        $messageType = 'success';
        // 刷新可用性数据
        $availabilityMap = $bookingSystem->getNext7DaysAvailability();
    } else {
        $message = $result['message'];
        $messageType = 'error';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>海豹骰内置迁移分离预约系统</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap">
    <style>
        :root {
            --vp-c-brand-1: #3b82f6;
            --vp-c-brand-2: #1d4ed8;
            --vp-c-bg: #ffffff;
            --vp-c-bg-soft: #f9fafb;
            --vp-c-bg-mute: #f1f5f9;
            --vp-c-text-1: #1f2937;
            --vp-c-text-2: #6b7280;
            --vp-c-text-3: #9ca3af;
            --vp-button-brand-bg: var(--vp-c-brand-1);
            --vp-button-brand-hover-bg: var(--vp-c-brand-2);
            --vp-font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: var(--vp-font-family);
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: var(--vp-c-bg);
            border-radius: 12px;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, var(--vp-c-brand-1), var(--vp-c-brand-2));
            color: white;
            padding: 2rem;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        
        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 2rem;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 2rem;
        }
        
        @media (max-width: 768px) {
            .content {
                grid-template-columns: 1fr;
            }
        }
        
        .booking-form {
            background: var(--vp-c-bg-soft);
            padding: 1.5rem;
            border-radius: 8px;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: var(--vp-c-text-1);
        }
        
        input, select {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #d1d5db;
            border-radius: 6px;
            font-size: 1rem;
            transition: all 0.2s;
        }
        
        input:focus, select:focus {
            outline: none;
            border-color: var(--vp-c-brand-1);
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
        }
        
        .btn {
            background: var(--vp-button-brand-bg);
            color: white;
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            width: 100%;
            transition: background 0.2s;
        }
        
        .btn:hover {
            background: var(--vp-button-brand-hover-bg);
        }
        
        .availability-section h2 {
            font-size: 1.5rem;
            margin-bottom: 1rem;
            color: var(--vp-c-text-1);
        }
        
        .date-slots {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
            gap: 1rem;
        }
        
        .date-slot {
            border: 1px solid #e5e7eb;
            padding: 1rem;
            border-radius: 8px;
            text-align: center;
            transition: all 0.2s;
        }
        
        .date-slot:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .date-slot.weekend {
            border-left: 4px solid #8b5cf6;
        }
        
        .date-slot.weekday {
            border-left: 4px solid #3b82f6;
        }
        
        .date-slot.full {
            opacity: 0.6;
            background-color: #fef2f2;
            border-color: #fecaca;
        }
        
        .date {
            font-weight: 600;
            font-size: 0.9rem;
        }
        
        .slots {
            font-size: 0.875rem;
            color: var(--vp-c-text-2);
            margin: 0.5rem 0;
        }
        
        .status {
            font-size: 0.75rem;
            font-weight: 600;
            padding: 0.25rem 0.5rem;
            border-radius: 9999px;
        }
        
        .status.available {
            background: #d1fae5;
            color: #065f46;
        }
        
        .status.full {
            background: #fee2e2;
            color: #991b1b;
        }
        
        .alert {
            padding: 1rem;
            border-radius: 6px;
            margin-bottom: 1rem;
        }
        
        .alert-success {
            background-color: #d1fae5;
            color: #065f46;
            border: 1px solid #a7f3d0;
        }
        
        .alert-error {
            background-color: #fee2e2;
            color: #991b1b;
            border: 1px solid #fca5a5;
        }
        
        .info-box {
            background: var(--vp-c-bg-mute);
            padding: 1rem;
            border-radius: 6px;
            margin-top: 1rem;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <h1>🧊 海豹骰内置迁移分离预约</h1>
            <p>工作日每天3个席位 • 周末每天5个席位</p>
        </header>
        
        <div class="content">
            <div class="booking-form">
                <?php if ($message): ?>
                    <div class="alert alert-<?php echo $messageType; ?>">
                        <?php echo htmlspecialchars($message); ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST" id="bookingForm">
                    <div class="form-group">
                        <label for="master_qq">🎯 骰主QQ号 *</label>
                        <input type="text" id="master_qq" name="master_qq" 
                               value="<?php echo isset($_POST['master_qq']) ? htmlspecialchars($_POST['master_qq']) : ''; ?>" 
                               placeholder="请输入您的QQ号" required pattern="\d{5,12}">
                    </div>
                    
                    <div class="form-group">
                        <label for="dice_qq">🎲 骰娘QQ号 *</label>
                        <input type="text" id="dice_qq" name="dice_qq" 
                               value="<?php echo isset($_POST['dice_qq']) ? htmlspecialchars($_POST['dice_qq']) : ''; ?>" 
                               placeholder="请输入骰娘QQ号" required pattern="\d{5,12}">
                    </div>

                    <div class="form-group">
                        <label for="booking_date">📅 预约日期 *</label>
                        <select id="booking_date" name="booking_date" required>
                            <option value="">-- 请选择预约日期 --</option>
                            <?php foreach ($availabilityMap as $date => $info): ?>
                            <option value="<?php echo $date; ?>" 
                                    <?php if (isset($_POST['booking_date']) && $_POST['booking_date']==$date) echo 'selected'; ?> 
                                    <?php if ($info['is_full']) echo 'disabled'; ?>
                                    data-available="<?php echo $info['available_slots']; ?>"
                                    data-max="<?php echo $info['max_slots']; ?>">
                                <?php echo $info['week_label']; ?> 
                                (<?php echo $info['available_slots']; ?>/<?php echo $info['max_slots']; ?>)
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <button type="submit" class="btn">提交预约</button>
                </form>
                
                <div class="info-box">
                    <strong>💡 预约说明：</strong>
                    <ul style="margin-top: 0.5rem; padding-left: 1.2rem;">
                        <li>每个QQ号在同一天只能预约一次</li>
                        <li>预约成功后不可修改，如需变更请重新预约</li>
                        <li>请确保QQ号填写正确，这将用于后续联系</li>
                    </ul>
                </div>
            </div>

            <section class="availability-section">
                <h2>近7日预约情况</h2>
                <div class="date-slots">
                    <?php foreach ($availabilityMap as $date => $info): ?>
                    <div class="date-slot <?php echo $info['is_weekend'] ? 'weekend' : 'weekday'; ?> <?php echo $info['is_full'] ? 'full' : ''; ?>">
                        <div class="date"><?php echo $info['week_label']; ?></div>
                        <div class="slots">
                            <span style="font-size: 1.5rem; font-weight: bold; color: <?php echo $info['is_full'] ? '#ef4444' : '#10b981'; ?>">
                                <?php echo $info['available_slots']; ?>
                            </span>
                            / <?php echo $info['max_slots']; ?>
                        </div>
                        <div class="status <?php echo $info['is_full'] ? 'full' : 'available'; ?>">
                            <?php echo $info['is_full'] ? '已满' : '可预约'; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </section>
        </div>
    </div>

    <script>
        document.getElementById('booking_date').addEventListener('change', function() {
            const selectedOption = this.options[this.selectedIndex];
            if (selectedOption.disabled) {
                alert('此日期已约满，请选择其他日期。');
                this.value = '';
            }
        });
        
        // 表单验证
        document.getElementById('bookingForm').addEventListener('submit', function(e) {
            const masterQQ = document.getElementById('master_qq').value;
            const diceQQ = document.getElementById('dice_qq').value;
            const dateSelect = document.getElementById('booking_date');
            
            if (!/^\d{5,12}$/.test(masterQQ)) {
                alert('请输入5-12位数字的骰主QQ号');
                e.preventDefault();
                return;
            }
            
            if (!/^\d{5,12}$/.test(diceQQ)) {
                alert('请输入5-12位数字的骰娘QQ号');
                e.preventDefault();
                return;
            }
            
            if (!dateSelect.value) {
                alert('请选择预约日期');
                e.preventDefault();
                return;
            }
        });
    </script>
</body>
</html>