<?php
require_once('../database/header.php');
if ($userdata['status'] == 'paywait') {
echo '<script>window.location = "paywait.php"</script>';
exit; // Ensure to exit after redirection
}
?>

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


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

<!-- 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
if (isset($_POST['add_bank']) &&
!empty($_POST['brand_name']) &&
!empty($_POST['promotional_text']) &&
!empty($_POST['image'])) {

// Handle checkboxes in one line
$share_qr = isset($_POST['share_qr']) ? 'on' : 'off';
$paytm_button = isset($_POST['paytm_button']) ? 'on' : 'off';
$gpay_button = isset($_POST['gpay_button']) ? 'on' : 'off';
$upi_id = isset($_POST['upi_id']) ? 'on' : 'off';

// Check if the record already exists
$check_sql = "SELECT COUNT(*) FROM `checkout_page` WHERE `user` = :user AND `brand_name` = :brand_name";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bindParam(":user", $userdata['username']);
$check_stmt->bindParam(":brand_name", filter_var($_POST['brand_name'], FILTER_SANITIZE_STRING));
$check_stmt->execute();
$record_exists = $check_stmt->fetchColumn();

// SQL for Insert or Update
if ($record_exists > 0) {
// If record exists, update it
$sql = "UPDATE `checkout_page` SET `promotional_text` = :promotional_text, `image` = :image, `save_qr` = :save_qr, `share_qr` = :share_qr, `paytm_button` = :paytm_button, `gpay_button` = :gpay_button, `upi_id` = :upi_id WHERE `user` = :user AND `brand_name` = :brand_name";

} else {
// If no record exists, insert new record
$sql = "INSERT INTO `checkout_page` (`user`, `brand_name`, `promotional_text`, `image`, `save_qr`, `share_qr`, `paytm_button`, `gpay_button`, `upi_id`) VALUES (:user, :brand_name, :promotional_text, :image, :save_qr, :share_qr, :paytm_button, :gpay_button, :upi_id)";

}

// Prepare and execute the query
$stmt = $conn->prepare($sql);
$stmt->bindParam(":user", $userdata['username']);
$stmt->bindParam(":brand_name", filter_var($_POST['brand_name'], FILTER_SANITIZE_STRING));
$stmt->bindParam(":promotional_text", filter_var($_POST['promotional_text'], FILTER_SANITIZE_STRING));
$stmt->bindParam(":image", filter_var($_POST['image'], FILTER_SANITIZE_STRING));
$stmt->bindParam(":save_qr", filter_var($_POST['save_qr'], FILTER_SANITIZE_STRING));
$stmt->bindParam(":share_qr", $share_qr);
$stmt->bindParam(":paytm_button", $paytm_button);
$stmt->bindParam(":gpay_button", $gpay_button);
$stmt->bindParam(":upi_id", $upi_id);

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 = "";
}, 2000); // Delay redirect by 2 seconds
</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 = "";
}, 2000); // Delay redirect by 2 seconds
</script>'; 
}
}
?>




<?php
$user = $userdata['username'];

// Query to fetch data from the database
$sql = "SELECT * FROM `checkout_page` WHERE `user` = :user";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":user", $user);
$stmt->execute();
$checkout = $stmt->fetch(PDO::FETCH_ASSOC);

?>

<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="brand_name" value="<?php echo isset($checkout['brand_name']) ? $checkout['brand_name'] : '';?>" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
<div class="col-sm-6 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-dark">促销图片 URL</label>
<input  type="url" class="form-control" placeholder="输入促销图片 URL" name="image" value="<?php echo isset($checkout['image']) ? $checkout['image'] : '';?>">
</div>
</div>

 <div class="form-group">
<label class="m-0 font-weight-bold text-dark">促销文本</label>
<textarea class="form-control" placeholder="输入促销文本" name="promotional_text" rows="4"><?php echo isset($checkout['promotional_text']) ? htmlspecialchars($checkout['promotional_text']) : ''; ?></textarea>
</div>





<div class="form-group" style="display: flex; flex-wrap: wrap;">
<div class="pretty p-switch" style="margin-right: 10px;">
<input type="checkbox" name="save_qr" <?php echo (isset($checkout['save_qr']) && $checkout['save_qr'] == 'on' ? 'checked="checked"' : ''); ?> />
<div class="state p-primary">
<label>保存二维码</label>
</div>
</div>
<div class="pretty p-switch" style="margin-right: 10px;">
<input type="checkbox" name="share_qr" <?php echo (isset($checkout['share_qr']) && $checkout['share_qr'] == 'on' ? 'checked="checked"' : ''); ?> />
<div class="state p-primary">
<label>分享二维码</label>
</div>
</div>
<div class="pretty p-switch" style="margin-right: 10px;">
<input type="checkbox" name="upi_id" <?php echo (isset($checkout['upi_id']) && $checkout['upi_id'] == 'on' ? 'checked="checked"' : ''); ?>/>
<div class="state p-primary">
<label>显示 UPI ID</label>
</div>
</div>
<div class="pretty p-switch" style="margin-right: 10px;">
<input type="checkbox" name="paytm_button" <?php echo (isset($checkout['paytm_button']) && $checkout['paytm_button'] == 'on' ? 'checked="checked"' : ''); ?>/>
<div class="state p-primary">
<label>Paytm 按钮</label>
</div>
</div>
<div class="pretty p-switch" style="margin-right: 10px;">
<input type="checkbox" name="gpay_button" <?php echo (isset($checkout['gpay_button']) && $checkout['gpay_button'] == 'on' ? 'checked="checked"' : ''); ?>/>
<div class="state p-primary">
<label>Google Pay 按钮</label>
</div>
</div>
</div>

<input required="required" type="submit" name="add_bank" class="btn btn-primary btn-block" value="提交">
</form>
</div>
</div>
<?php } else { ?>
<img class="img-fluid" src="../bootstrap/img/cloud.png">
<?php } ?>
</div>
<!-- /.container-fluid -->
<!-- End of Main Content -->

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