<?php

// error_reporting(E_ALL);
// ini_set('display_errors', 1);
require_once('../database/header.php');

if ($userdata['status'] == 'paywait') {
echo '<script>
window.location = "paywait.php"
</script>';
}

?>

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

 <?php
if($userdata['usertype']=="mainadmin"){
?>

<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3 bg-primary">
<h6 class="m-0 font-weight-bold text-white">添加订阅</h6>
</div>
<div class="card-body">

<?php

// Default values for fields
$plan_name = $plan_type = $plan_amount = $txn_limit = $expired_date = "";
$features = [];

// Handle form submission
if (isset($_POST['add_merchant']) && 
!empty($_POST['plan_name']) && 
!empty($_POST['plan_type']) && 
!empty($_POST['plan_amount']) && 
!empty($_POST['txn_limit'])) {

// Sanitize and validate the inputs
$plan_name = filter_var($_POST['plan_name'], FILTER_SANITIZE_STRING);
$plan_type = filter_var($_POST['plan_type'], FILTER_SANITIZE_STRING);
$plan_amount = filter_var($_POST['plan_amount'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$txn_limit = filter_var($_POST['txn_limit'], FILTER_SANITIZE_NUMBER_INT);

// Get the selected features
if (isset($_POST['features'])) {
$features = $_POST['features']; // This is an array of selected features
}

// Calculate the expiration date based on the plan type (Month/Year)
if ($plan_type == 'Month') {
$expired_date = date('Y-m-d', strtotime("+1 month"));
} elseif ($plan_type == 'Year') {
$expired_date = date('Y-m-d', strtotime("+1 year"));
}

$status = "Active"; // Static status
// Proceed with insertion into the database
$sql = "INSERT INTO `plans`(`user`, `status`, `plan_name`, `plan_type`, `plan_amount`, `txn_limit`, `expired_date`, `features`) 
VALUES (:user, :status, :plan_name, :plan_type, :plan_amount, :txn_limit, :expired_date, :features)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":user", $userdata['username']); // Assuming you have a $userdata['username']
$stmt->bindParam(":status", $status);
$stmt->bindParam(":plan_name", $plan_name);
$stmt->bindParam(":plan_type", $plan_type);
$stmt->bindParam(":plan_amount", $plan_amount);
$stmt->bindParam(":txn_limit", $txn_limit);
$stmt->bindParam(":expired_date", $expired_date);
$stmt->bindParam(":features", implode(',', $features)); // Store features as a comma-separated string

if ($stmt->execute()) {
echo '<div class="alert alert-success alert-has-icon alert-position">
<div class="alert-icon"><i class="far fa-lightbulb"></i></div>
<div class="alert-body">
<div class="alert-title">计划添加成功！！</div>
</div>
</div>

<script>
setTimeout(function() {
window.location = "subscriptions.php";
}, 2000); 
</script>';
} else {
echo '<div class="alert alert-danger alert-has-icon alert-position">
<div class="alert-icon"><i class="far fa-lightbulb"></i></div>
<div class="alert-body">
<div class="alert-title">错误！数据未插入！</div>
</div>
</div>

<script>
setTimeout(function() {
window.location = "subscriptions.php";
}, 2000); 
</script>';
}
}
?>

<form class="user" action="" method="POST">
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">计划名称</label>
<input required="required" type="text" class="form-control" placeholder="输入计划名称" name="plan_name" value="<?php echo isset($_POST['plan_name']) ? $_POST['plan_name'] : ''; ?>">
</div>
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">计划类型（月/年）</label>
<select required="required" class="form-control" name="plan_type" id="plan_type">
<option value="">选择计划类型</option>
<option value="Month" <?php echo (isset($_POST['plan_type']) && $_POST['plan_type'] == 'Month') ? 'selected' : ''; ?>>月</option>
<option value="Year" <?php echo (isset($_POST['plan_type']) && $_POST['plan_type'] == 'Year') ? 'selected' : ''; ?>>年</option>
</select>
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">计划金额</label>
<input required="required" type="number" step="0.01" class="form-control" placeholder="输入计划金额" name="plan_amount" value="<?php echo isset($_POST['plan_amount']) ? $_POST['plan_amount'] : ''; ?>">
</div>
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">交易限制</label>
<input required="required" type="number" class="form-control" placeholder="输入交易限制" name="txn_limit" value="<?php echo isset($_POST['txn_limit']) ? $_POST['txn_limit'] : ''; ?>">
</div>
</div>

<!-- Feature Selection -->
<div class="form-group">
<label class="m-0 font-weight-bold text-dark">选择功能</label><br>
<div class="features-container">
<label><input type="checkbox" name="features[]" value="Real-time Transactions" id="feature1" <?php echo (in_array('Real-time Transactions', $features)) ? 'checked' : ''; ?>> 实时交易</label>
<label><input type="checkbox" name="features[]" value="Multipal Merchant Add" id="feature2" <?php echo (in_array('Multipal Merchant Add', $features)) ? 'checked' : ''; ?>> 添加多个商户</label>
<label><input type="checkbox" name="features[]" value="Dynamic QR Code" id="feature3" <?php echo (in_array('Dynamic QR Code', $features)) ? 'checked' : ''; ?>> 动态二维码</label>
<label><input type="checkbox" name="features[]" value="Accept All UPI Apps" id="feature4" <?php echo (in_array('Accept All UPI Apps', $features)) ? 'checked' : ''; ?>> 接受所有 UPI 应用</label>
<label><input type="checkbox" name="features[]" value="Live Support" id="feature5" <?php echo (in_array('Live Support', $features)) ? 'checked' : ''; ?>> 实时支持</label>
<label><input type="checkbox" name="features[]" value="Direct UPI Intent" id="feature6" <?php echo (in_array('Direct UPI Intent', $features)) ? 'checked' : ''; ?>> 直接 UPI Intent</label>
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">立即提交</label><br>
<input required="required" type="submit" name="add_merchant" class="btn btn-info btn-block" value="提交">
</div>
</div>
</form>

<script>
// JavaScript to update the Expired Date when Plan Type changes
document.getElementById('plan_type').addEventListener('change', function() {
var planType = this.value;
var expiredDateField = document.getElementById('expired_date');

var currentDate = new Date();
if (planType === 'Month') {
currentDate.setMonth(currentDate.getMonth() + 1);
} else if (planType === 'Year') {
currentDate.setFullYear(currentDate.getFullYear() + 1);
}

// Format the expired date as YYYY-MM-DD
var expiredDate = currentDate.toISOString().split('T')[0];
expiredDateField.value = expiredDate;
});
</script>

<style>
.features-container {
display: flex;
flex-wrap: wrap;
}

.features-container label {
margin-right: 20px;
margin-bottom: 10px;
}
</style>
<?php

}else{
?>
<div class="text-center">
<img class="img-fluid" width="500" height="700" src="https://stickers.bobblekeyboard.net/preview/b1cbcce4-1fd4-46be-b3eb-d6e05d4853c9.png">
</div>
<?php
}
?>

</div>
</div>
</div>

<?php
// Assuming you have a footer file
require_once('../database/footer.php');
?>
