<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book an Appointment - Rosy</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
        }

        .card {
            background: white;
            border-radius: 16px;
            padding: 40px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        }

        header {
            text-align: center;
            margin-bottom: 30px;
        }

        h1 {
            color: #333;
            font-size: 1.8rem;
            margin-bottom: 8px;
        }

        .subtitle {
            color: #666;
            font-size: 1rem;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            font-weight: 500;
            color: #333;
            margin-bottom: 8px;
            font-size: 0.95rem;
        }

        label .required {
            color: #e74c3c;
        }

        input, select, textarea {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 1rem;
            font-family: inherit;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        textarea {
            resize: vertical;
            min-height: 100px;
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }

        @media (max-width: 500px) {
            .form-row {
                grid-template-columns: 1fr;
            }
        }

        .btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
        }

        .btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
        }

        /* Success/Error Messages */
        .message {
            padding: 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }

        .message.show {
            display: block;
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        /* Calendar styling */
        input[type="date"], input[type="time"] {
            cursor: pointer;
        }

        .help-text {
            font-size: 0.85rem;
            color: #888;
            margin-top: 6px;
        }

        footer {
            text-align: center;
            color: rgba(255, 255, 255, 0.8);
            padding: 20px;
            font-size: 0.9rem;
        }

        footer a {
            color: white;
        }

        /* Confirmation screen */
        .confirmation {
            display: none;
            text-align: center;
        }

        .confirmation.show {
            display: block;
        }

        .confirmation-icon {
            font-size: 4rem;
            margin-bottom: 20px;
        }

        .confirmation h2 {
            color: #333;
            margin-bottom: 15px;
        }

        .confirmation p {
            color: #666;
            margin-bottom: 10px;
        }

        .confirmation .details {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
            text-align: left;
        }

        .confirmation .details p {
            margin: 8px 0;
        }

        .confirmation .details strong {
            color: #333;
        }

        .back-btn {
            background: #6c757d;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <header>
                <h1>Book an Appointment</h1>
                <p class="subtitle">Schedule a time to meet with Rosy</p>
            </header>

            <div class="message" id="message"></div>

            <!-- Booking Form -->
            <form id="bookingForm">
                <div class="form-group">
                    <label>Your Name <span class="required">*</span></label>
                    <input type="text" id="name" name="name" required placeholder="Enter your full name">
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label>Email <span class="required">*</span></label>
                        <input type="email" id="email" name="email" required placeholder="your@email.com">
                    </div>
                    <div class="form-group">
                        <label>Phone</label>
                        <input type="tel" id="phone" name="phone" placeholder="(optional)">
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label>Preferred Date <span class="required">*</span></label>
                        <input type="date" id="date" name="date" required>
                    </div>
                    <div class="form-group">
                        <label>Preferred Time <span class="required">*</span></label>
                        <input type="time" id="time" name="time" required>
                    </div>
                </div>

                <div class="form-group">
                    <label>Reason for Visit <span class="required">*</span></label>
                    <select id="reason" name="reason" required>
                        <option value="">Select a reason...</option>
                        <option value="General Meeting">General Meeting</option>
                        <option value="Consultation">Consultation</option>
                        <option value="Catch Up">Catch Up / Social</option>
                        <option value="Help/Assistance">Help / Assistance</option>
                        <option value="Project Discussion">Project Discussion</option>
                        <option value="Other">Other</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Additional Details</label>
                    <textarea id="details" name="details" placeholder="Any special requests or additional information..."></textarea>
                    <p class="help-text">Let me know anything that would help prepare for our meeting.</p>
                </div>

                <button type="submit" class="btn" id="submitBtn">Request Appointment</button>
            </form>

            <!-- Confirmation Screen -->
            <div class="confirmation" id="confirmation">
                <div class="confirmation-icon">✓</div>
                <h2>Appointment Requested!</h2>
                <p>Thank you for booking. You'll receive a confirmation soon.</p>

                <div class="details" id="confirmDetails">
                    <!-- Filled by JavaScript -->
                </div>

                <button class="btn back-btn" onclick="resetForm()">Book Another Appointment</button>
            </div>
        </div>
    </div>

    <footer>
        <p><a href="/">← Back to Home</a></p>
    </footer>

    <script>
        // Set minimum date to today
        const today = new Date().toISOString().split('T')[0];
        document.getElementById('date').setAttribute('min', today);

        // Form submission
        document.getElementById('bookingForm').addEventListener('submit', async function(e) {
            e.preventDefault();

            const submitBtn = document.getElementById('submitBtn');
            const message = document.getElementById('message');

            submitBtn.disabled = true;
            submitBtn.textContent = 'Submitting...';
            message.classList.remove('show');

            const formData = {
                name: document.getElementById('name').value,
                email: document.getElementById('email').value,
                phone: document.getElementById('phone').value || 'Not provided',
                date: document.getElementById('date').value,
                time: document.getElementById('time').value,
                reason: document.getElementById('reason').value,
                details: document.getElementById('details').value || 'None',
                submitted_at: new Date().toISOString()
            };

            try {
                const response = await fetch('/appointments/api/book.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(formData)
                });

                const result = await response.json();

                if (result.success) {
                    // Show confirmation
                    document.getElementById('bookingForm').style.display = 'none';
                    document.getElementById('confirmation').classList.add('show');

                    // Format date nicely
                    const dateObj = new Date(formData.date + 'T' + formData.time);
                    const formattedDate = dateObj.toLocaleDateString('en-US', {
                        weekday: 'long',
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                    });
                    const formattedTime = dateObj.toLocaleTimeString('en-US', {
                        hour: 'numeric',
                        minute: '2-digit'
                    });

                    document.getElementById('confirmDetails').innerHTML = `
                        <p><strong>Name:</strong> ${formData.name}</p>
                        <p><strong>Date:</strong> ${formattedDate}</p>
                        <p><strong>Time:</strong> ${formattedTime}</p>
                        <p><strong>Reason:</strong> ${formData.reason}</p>
                    `;
                } else {
                    throw new Error(result.message || 'Failed to book appointment');
                }
            } catch (error) {
                message.textContent = 'Error: ' + error.message;
                message.className = 'message show error';
                submitBtn.disabled = false;
                submitBtn.textContent = 'Request Appointment';
            }
        });

        function resetForm() {
            document.getElementById('bookingForm').reset();
            document.getElementById('bookingForm').style.display = 'block';
            document.getElementById('confirmation').classList.remove('show');
            document.getElementById('submitBtn').disabled = false;
            document.getElementById('submitBtn').textContent = 'Request Appointment';
        }
    </script>
</body>
</html>
