<?php
include '../includes/auth.php';
include '../config/db.php';
$user_id = $_SESSION['user_id'];

// Get user info
$user_query = $conn->query("SELECT name, email, membership_active FROM users WHERE id=$user_id");
$user = $user_query->fetch_assoc();

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $reason = trim($_POST['reason']);
    $cancellation_type = $_POST['cancellation_type'];
    
    if (empty($reason)) {
        $error = "Please provide a reason for cancellation.";
    } else {
        $stmt = $conn->prepare("INSERT INTO cancellation_requests (user_id, reason, requested_at) VALUES (?, ?, NOW())");
        $stmt->bind_param("is", $user_id, $reason);
        if ($stmt->execute()) {
            $message = "Your membership cancellation request has been submitted successfully. Admin will review it shortly.";
        } else {
            $error = "Something went wrong. Please try again.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cancel Membership - Gau Seva Foundation</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 2rem;
        }

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

        /* Card Style */
        .cancel-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            animation: slideIn 0.5s ease-out;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Header */
        .card-header {
            background: linear-gradient(135deg, #dc3545, #c82333);
            color: white;
            padding: 2rem;
            text-align: center;
            position: relative;
        }

        .card-header i {
            font-size: 4rem;
            margin-bottom: 1rem;
            animation: shake 0.5s ease-in-out;
        }

        @keyframes shake {
            0%, 100% { transform: rotate(0deg); }
            25% { transform: rotate(10deg); }
            75% { transform: rotate(-10deg); }
        }

        .card-header h2 {
            font-size: 1.8rem;
            margin-bottom: 0.5rem;
        }

        .card-header p {
            opacity: 0.9;
            font-size: 0.9rem;
        }

        /* Body */
        .card-body {
            padding: 2rem;
        }

        /* User Info Box */
        .user-info {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            border-left: 4px solid #dc3545;
        }

        .user-info h3 {
            color: #333;
            font-size: 1.1rem;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .user-info h3 i {
            color: #dc3545;
        }

        .info-row {
            display: flex;
            justify-content: space-between;
            padding: 0.75rem 0;
            border-bottom: 1px solid #e0e0e0;
        }

        .info-row:last-child {
            border-bottom: none;
        }

        .info-label {
            font-weight: 500;
            color: #666;
        }

        .info-value {
            color: #333;
            font-weight: 500;
        }

        .badge-active {
            background: #28a745;
            color: white;
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.8rem;
        }

        .badge-inactive {
            background: #dc3545;
            color: white;
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.8rem;
        }

        /* Warning Box */
        .warning-box {
            background: #fff3cd;
            border-left: 4px solid #ffc107;
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 1.5rem;
            display: flex;
            gap: 1rem;
            align-items: flex-start;
        }

        .warning-box i {
            color: #ffc107;
            font-size: 1.5rem;
        }

        .warning-box p {
            color: #856404;
            font-size: 0.9rem;
            line-height: 1.5;
        }

        /* Form Group */
        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.75rem;
            font-weight: 500;
            color: #333;
        }

        .form-group label i {
            color: #dc3545;
            margin-right: 8px;
        }

        .form-group textarea {
            width: 100%;
            padding: 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-family: 'Poppins', sans-serif;
            font-size: 0.95rem;
            resize: vertical;
            transition: all 0.3s;
        }

        .form-group textarea:focus {
            outline: none;
            border-color: #dc3545;
            box-shadow: 0 0 0 3px rgba(220,53,69,0.1);
        }

        /* Radio Group */
        .radio-group {
            display: flex;
            gap: 1.5rem;
            margin-top: 0.5rem;
            flex-wrap: wrap;
        }

        .radio-label {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            padding: 0.5rem 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 50px;
            transition: all 0.3s;
        }

        .radio-label:hover {
            border-color: #dc3545;
            background: #fff5f5;
        }

        .radio-label input[type="radio"] {
            cursor: pointer;
        }

        /* Buttons */
        .btn-group {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
        }

        .btn {
            flex: 1;
            padding: 1rem;
            border: none;
            border-radius: 12px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            text-align: center;
            display: inline-block;
        }

        .btn-danger {
            background: linear-gradient(135deg, #dc3545, #c82333);
            color: white;
        }

        .btn-danger:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(220,53,69,0.3);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
            transform: translateY(-2px);
        }

        /* Message Alerts */
        .alert {
            padding: 1rem;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 10px;
            animation: slideIn 0.3s ease-out;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border-left: 4px solid #28a745;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }

        .alert i {
            font-size: 1.2rem;
        }

        /* Modal for Confirmation */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            justify-content: center;
            align-items: center;
            z-index: 1000;
            animation: fadeIn 0.3s;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .modal-content {
            background: white;
            border-radius: 20px;
            max-width: 400px;
            width: 90%;
            padding: 2rem;
            text-align: center;
            animation: slideIn 0.3s;
        }

        .modal-content i {
            font-size: 4rem;
            color: #dc3545;
            margin-bottom: 1rem;
        }

        .modal-content h3 {
            margin-bottom: 1rem;
            color: #333;
        }

        .modal-content p {
            color: #666;
            margin-bottom: 1.5rem;
        }

        .modal-buttons {
            display: flex;
            gap: 1rem;
        }

        .modal-buttons button {
            flex: 1;
            padding: 0.75rem;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-weight: 500;
        }

        .btn-confirm {
            background: #dc3545;
            color: white;
        }

        .btn-cancel {
            background: #6c757d;
            color: white;
        }

        /* Responsive */
        @media (max-width: 768px) {
            body {
                padding: 1rem;
            }

            .card-header h2 {
                font-size: 1.5rem;
            }

            .card-body {
                padding: 1.5rem;
            }

            .btn-group {
                flex-direction: column;
            }

            .radio-group {
                flex-direction: column;
                gap: 0.75rem;
            }
        }

        /* Loading Spinner */
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="cancel-card">
        <div class="card-header">
            <i class="fas fa-user-slash"></i>
            <h2>Cancel Membership</h2>
            <p>We're sad to see you go</p>
        </div>

        <div class="card-body">
            <!-- User Information -->
            <div class="user-info">
                <h3><i class="fas fa-user-circle"></i> Your Account Information</h3>
                <div class="info-row">
                    <span class="info-label"><i class="fas fa-user"></i> Name:</span>
                    <span class="info-value"><?= htmlspecialchars($user['name']) ?></span>
                </div>
                <div class="info-row">
                    <span class="info-label"><i class="fas fa-envelope"></i> Email:</span>
                    <span class="info-value"><?= htmlspecialchars($user['email']) ?></span>
                </div>
                <div class="info-row">
                    <span class="info-label"><i class="fas fa-id-card"></i> Membership Status:</span>
                    <span class="info-value">
                        <span class="<?= $user['membership_active'] ? 'badge-active' : 'badge-inactive' ?>">
                            <i class="fas <?= $user['membership_active'] ? 'fa-check-circle' : 'fa-times-circle' ?>"></i>
                            <?= $user['membership_active'] ? 'Active' : 'Inactive' ?>
                        </span>
                    </span>
                </div>
            </div>

            <!-- Warning Box -->
            <div class="warning-box">
                <i class="fas fa-exclamation-triangle"></i>
                <p>
                    <strong>Important Note:</strong> Cancelling your membership will:
                    <br>• Remove access to member benefits
                    <br>• Delete your member ID card
                    <br>• You can rejoin anytime by registering again
                </p>
            </div>

            <!-- Message Display -->
            <?php if($message): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <span><?= htmlspecialchars($message) ?></span>
            </div>
            <?php endif; ?>

            <?php if($error): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <span><?= htmlspecialchars($error) ?></span>
            </div>
            <?php endif; ?>

            <!-- Cancellation Form -->
            <?php if($user['membership_active'] && !$message): ?>
            <form method="POST" id="cancelForm">
                <div class="form-group">
                    <label><i class="fas fa-comment"></i> Reason for Cancellation</label>
                    <textarea 
                        name="reason" 
                        rows="5" 
                        required 
                        placeholder="Please help us improve by telling us why you're cancelling..."></textarea>
                </div>

                <div class="form-group">
                    <label><i class="fas fa-info-circle"></i> What would you like to do?</label>
                    <div class="radio-group">
                        <label class="radio-label">
                            <input type="radio" name="cancellation_type" value="immediate" checked> 
                            <i class="fas fa-bolt"></i> Cancel Immediately
                        </label>
                        <label class="radio-label">
                            <input type="radio" name="cancellation_type" value="end_of_period"> 
                            <i class="fas fa-calendar"></i> Cancel at Period End
                        </label>
                    </div>
                </div>

                <div class="btn-group">
                    <button type="button" class="btn btn-secondary" onclick="history.back()">
                        <i class="fas fa-arrow-left"></i> Go Back
                    </button>
                    <button type="button" class="btn btn-danger" onclick="showConfirmation()">
                        <i class="fas fa-trash-alt"></i> Request Cancellation
                    </button>
                </div>
            </form>
            <?php elseif(!$user['membership_active']): ?>
            <div class="alert alert-error">
                <i class="fas fa-info-circle"></i>
                <span>Your membership is already inactive. No action needed.</span>
            </div>
            <div class="btn-group">
                <a href="dashboard.php" class="btn btn-secondary">
                    <i class="fas fa-tachometer-alt"></i> Back to Dashboard
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Confirmation Modal -->
<div id="confirmModal" class="modal">
    <div class="modal-content">
        <i class="fas fa-question-circle"></i>
        <h3>Confirm Cancellation</h3>
        <p>Are you sure you want to cancel your membership? This action cannot be undone.</p>
        <div class="modal-buttons">
            <button class="btn-cancel" onclick="closeModal()">
                <i class="fas fa-times"></i> No, Keep It
            </button>
            <button class="btn-confirm" onclick="submitForm()">
                <i class="fas fa-check"></i> Yes, Cancel
            </button>
        </div>
    </div>
</div>

<script>
    function showConfirmation() {
        let reason = document.querySelector('textarea[name="reason"]').value.trim();
        if (reason === "") {
            alert("Please provide a reason for cancellation.");
            return;
        }
        document.getElementById('confirmModal').style.display = 'flex';
    }

    function closeModal() {
        document.getElementById('confirmModal').style.display = 'none';
    }

    function submitForm() {
        // Change button to loading state
        let btn = document.querySelector('.btn-danger');
        let originalText = btn.innerHTML;
        btn.innerHTML = '<span class="loading"></span> Processing...';
        btn.disabled = true;
        
        // Submit the form
        document.getElementById('cancelForm').submit();
    }

    // Close modal when clicking outside
    window.onclick = function(event) {
        let modal = document.getElementById('confirmModal');
        if (event.target == modal) {
            modal.style.display = 'none';
        }
    }
</script>
</body>
</html>