<?php
/**
 * Cancel Subscription
 * Cancel an active subscription
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';

requireAdmin();

$pdo = getDB();
$subscriptionId = (int)($_GET['id'] ?? 0);
$error = '';
$success = '';

// Get subscription details
$stmt = $pdo->prepare("SELECT s.*, u.full_name, u.email 
                      FROM subscriptions s
                      JOIN users u ON s.user_id = u.id
                      WHERE s.id = ?");
$stmt->execute([$subscriptionId]);
$subscription = $stmt->fetch();

if (!$subscription) {
    header('Location: index.php?error=Subscription not found');
    exit;
}

// Handle cancellation
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $confirm = $_POST['confirm'] ?? '';
    
    if ($confirm == 'CANCEL') {
        $stmt = $pdo->prepare("UPDATE subscriptions SET is_active = 0 WHERE id = ?");
        if ($stmt->execute([$subscriptionId])) {
            $success = "Subscription cancelled successfully!";
            header("refresh:2;url=index.php");
        } else {
            $error = "Failed to cancel subscription";
        }
    } else {
        $error = "Please type CANCEL to confirm";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cancel Subscription - Admin Panel</title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-6 mx-auto">
                <div class="card">
                    <div class="card-header bg-warning">
                        <h4 class="mb-0"><i class="fas fa-ban"></i> Cancel Subscription</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?php echo $error; ?></div>
                        <?php endif; ?>
                        
                        <?php if ($success): ?>
                            <div class="alert alert-success"><?php echo $success; ?></div>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                <strong>⚠️ Warning!</strong><br>
                                You are about to cancel the following subscription:
                            </div>
                            
                            <table class="table table-bordered">
                                <tr><th>Doctor:</th><td><?php echo escape($subscription['full_name']); ?></td></tr>
                                <tr><th>Plan:</th><td><?php echo ucfirst($subscription['plan_type']); ?></td></tr>
                                <tr><th>Amount:</th><td><?php echo formatCurrency($subscription['amount']); ?></td></tr>
                                <tr><th>End Date:</th><td><?php echo $subscription['end_date'] ? date(DISPLAY_DATE_FORMAT, strtotime($subscription['end_date'])) : 'Lifetime'; ?></td></tr>
                            </table>
                            
                            <div class="alert alert-danger">
                                <strong>This action cannot be undone!</strong><br>
                                The doctor will lose access to the system immediately.
                            </div>
                            
                            <form method="POST">
                                <div class="mb-3">
                                    <label>Type <strong>CANCEL</strong> to confirm:</label>
                                    <input type="text" name="confirm" class="form-control" placeholder="CANCEL" required>
                                </div>
                                <button type="submit" class="btn btn-warning">
                                    <i class="fas fa-ban"></i> Cancel Subscription
                                </button>
                                <a href="index.php" class="btn btn-secondary">Go Back</a>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>