<?php
require_once('../database/header.php');

// Generate a CSRF token if it doesn't exist already
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

// Get the plans from the database
$query = "SELECT * FROM plans";
$stmt = $conn->prepare($query);
$stmt->execute();
$plans = $stmt->fetchAll();

// Get the current date
$current_date = new DateTime();
?>
<style>




    .card {
        border: none;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }

    .card-header {
        background-color: #4e73df;
        color: white;
        border-radius: 10px 10px 0 0;
    }

    .card-header h6 {
        margin: 0;
        font-size: 1.5rem;
    }

    .card-body {
        padding: 20px;
    }

    /* Pricing Card Styles */
    .pricing {
        background-color: white;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
        overflow: hidden;
        transition: transform 0.3s ease;
    }

    .pricing:hover {
        transform: translateY(-5px);
    }

    .pricing-highlight {
        border: 2px solid #4e73df;
    }

    .pricing-title {
        background-color: #4e73df;
        color: white;
        padding: 15px;
        text-align: center;
        font-size: 1.25rem;
        font-weight: bold;
    }

    .pricing-padding {
        padding: 20px;
    }

    .pricing-price {
        text-align: center;
        margin-bottom: 20px;
    }

    .pricing-price div:first-child {
        font-size: 2rem;
        font-weight: bold;
        color: #4e73df;
    }

    .pricing-price div:last-child {
        font-size: 1rem;
        color: #6c757d;
    }

    .pricing-details {
        margin-bottom: 20px;
    }

    .pricing-item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
    }

    .pricing-item-icon {
        color: #4e73df;
        margin-right: 10px;
    }

    .pricing-item-label {
        color: #6c757d;
    }

    .pricing-cta {
        text-align: center;
        padding: 20px;
        background-color: #f8f9fa;
    }

    .btn-primary {
        background-color: #4e73df;
        border: none;
        padding: 10px 20px;
        font-size: 1rem;
        border-radius: 5px;
        transition: background-color 0.3s ease;
    }

    .btn-primary:hover {
        background-color: #375abb;
    }
</style>

<div class="main-panel"> 
<div class="content-wrapper"><br>


 
 
    
<?php
// session_start();

if (isset($_SESSION['error_message'])) {
 echo '<div class="alert alert-info alert-has-icon alert-position">
<div class="alert-body">
<div class="alert-title">' . $_SESSION['error_message'] . '</div>
</div>
</div>';   
    
    //echo '<div class="error-message">' . $_SESSION['error_message'] . '</div>';
    unset($_SESSION['error_message']); // Clear the error message after displaying it
}
?>



<div class="row">							
											
<?php foreach ($plans as $plan): ?>


<div class="col-12 col-md-4 col-lg-4">
<div class="pricing <?php echo ($plan['plan_name'] == 'BUSINESS') ? 'pricing-highlight' : ''; ?>">
<div class="pricing-title">
<?php echo $plan['plan_name']; ?>
</div>
<div class="pricing-padding">
<div class="pricing-price">
<div>Rs. <?php echo $plan['plan_amount']; ?></div>
<?php
$p_type = $plan['plan_type'];
if($p_type == 'Month') $p_type_cn = '月';
else if($p_type == 'Year') $p_type_cn = '年';
else $p_type_cn = $p_type;
?>
<div>1 <?php echo $p_type_cn; ?></div>
</div>
<div class="pricing-details">
<?php
// Loop through the features (comma-separated) for this plan
$features = explode(',', $plan['features']);
foreach ($features as $feature) {
echo '<div class="pricing-item">
<div class="pricing-item-icon"><i class="fas fa-check"></i></div>
<div class="pricing-item-label"> ✅ ' . trim($feature) . '</div>
</div>';
}
?>

</div>
</div>

<div class="pricing-cta">
<!-- Subscription Form -->

<?php
// Create a new instance of DateTime for each plan to avoid carrying over changes
$subscription_date = new DateTime();

if ($plan['plan_type'] == 'Month') {
// Add one month to the current date
$subscription_date->modify('+1 month');
} elseif ($plan['plan_type'] == 'Year') {
// Add one year to the current date
// Ensure we handle the end-of-month case properly (e.g., February 29th)
$current_day = $subscription_date->format('d');
$subscription_date->modify('+1 year');

// If the resulting date is invalid (like Feb 29 in a non-leap year), adjust to the last valid day
if ($current_day != $subscription_date->format('d')) {
$subscription_date->modify('last day of February ' . $subscription_date->format('Y'));
}
}

// Reset the time to 00:00:00 after modification to avoid unwanted time part
$subscription_date->setTime(0, 0, 0);

// Format subscription date (without time part)
$subscription_date = $subscription_date->format('Y-m-d');


// Combine the data into a single string
$data = implode(',', [
$userdata['username'],
$userdata['id'],
$plan['plan_amount'],
$plan['plan_name'],
$plan['txn_limit'],
$subscription_date
]);

// Base64 encode the combined data (to obscure it, similar to encoding it in 64-bit format)
$encoded_data = base64_encode($data);


?>

<form method="POST" action="function/get-subscription.php">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="encrypted_data" value="<?php echo $encoded_data; ?>"> 
<button type="submit" class="btn btn-primary">立即订阅 <i class="fas fa-arrow-right"></i></button>
</form>
</div>
</div>

</div>
<?php endforeach; ?>
</div>
</div>

<?php
require_once('../database/footer.php');
?>
