<?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>';
}

?>
    <!-- Bootstrap CSS -->
    <!--<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">-->

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

    <!-- Bootstrap JS -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
    
<div class="main-panel">
<div class="content-wrapper">
    
    
    <!-- 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
// Fetch plans from the database
$sql = "SELECT * FROM plans ORDER BY expired_date DESC";  // Adjust the ordering as needed
$stmt = $conn->prepare($sql);
$stmt->execute();
$plans = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Handling the deletion of a plan
if (isset($_POST['delete_id'])) {
    $delete_id = $_POST['delete_id'];

    // Delete plan from the database
    $deleteSql = "DELETE FROM plans WHERE id = ?";
    $deleteStmt = $conn->prepare($deleteSql);
    $deleteStmt->execute([$delete_id]);

    echo "<script>alert('计划删除成功'); window.location.href = ''; </script>";
}

// Handling the update of a plan
if (isset($_POST['update_plan'])) {
    $id = $_POST['plan_id'];
    $name = $_POST['plan_name'];
    $type = $_POST['plan_type'];
    $amount = $_POST['plan_amount'];
    $limit = $_POST['txn_limit'];
    $features = $_POST['features'];
    $status = $_POST['status'];

    // Update the plan in the database
    $updateSql = "UPDATE plans SET plan_name = ?, plan_type = ?, plan_amount = ?, txn_limit = ?, features = ?, status = ? WHERE id = ?";
    $updateStmt = $conn->prepare($updateSql);
    $updateStmt->execute([$name, $type, $amount, $limit, $features, $status, $id]);

    echo "<script>alert('计划更新成功'); window.location.href = ''; </script>";
}
?>




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

            <?php if (!empty($plans)): ?>
                      <table class="table table-striped table-hover" id="order-listing" style="width:100%;">
                    <thead>
                        <tr>
                            <th> 名称</th>
                            <th> Type</th>
                            <th> 金额</th>
                            <th> 限制</th>
                            <th> 日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($plans as $plan): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($plan['plan_name']); ?></td>
                                <td><?php echo htmlspecialchars($plan['plan_type']); ?></td>
                                <td><?php echo htmlspecialchars($plan['plan_amount']); ?></td>
                                <td><?php echo htmlspecialchars($plan['txn_limit']); ?></td>
                                <td><?php echo htmlspecialchars($plan['expired_date']); ?></td>
     
                                <td><?php echo htmlspecialchars($plan['status']); ?></td>
                                <td>
                                    <!-- Edit button -->
                                  <br>  <button class="btn btn-warning btn-sm edit-btn" data-id="<?php echo $plan['id']; ?>" data-name="<?php echo htmlspecialchars($plan['plan_name']); ?>" data-type="<?php echo htmlspecialchars($plan['plan_type']); ?>" data-amount="<?php echo htmlspecialchars($plan['plan_amount']); ?>" data-limit="<?php echo htmlspecialchars($plan['txn_limit']); ?>" data-features="<?php echo htmlspecialchars($plan['features']); ?>" data-status="<?php echo htmlspecialchars($plan['status']); ?>">编辑</button><br><br>
                                    <!-- Delete button -->
                                    <form method="POST" action="" style="display:inline;">
                                        <input type="hidden" name="delete_id" value="<?php echo $plan['id']; ?>">
                                        <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('您确定要删除此计划吗？')">删除</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p>未找到计划。</p>
            <?php endif; ?>
        </div>
    </div>

</div>

<!-- Edit Plan Modal -->
<div class="modal fade" id="editPlanModal" tabindex="-1" role="dialog" aria-labelledby="editPlanModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editPlanModalLabel">编辑计划</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="POST" action="">
                    <input type="hidden" id="planId" name="plan_id">
                    <div class="form-group">
                        <label for="planName">计划名称</label>
                        <input type="text" class="form-control" id="planName" name="plan_name" required>
                    </div>
                    <div class="form-group">
                        <label for="planType">计划类型</label>
                        <input type="text" class="form-control" id="planType" name="plan_type" required>
                    </div>
                    <div class="form-group">
                        <label for="planAmount">计划金额</label>
                        <input type="number" class="form-control" id="planAmount" name="plan_amount" required>
                    </div>
                    <div class="form-group">
                        <label for="txnLimit">交易限制</label>
                        <input type="number" class="form-control" id="txnLimit" name="txn_limit" required>
                    </div>
                    <div class="form-group">
                        <label for="features">功能</label>
                        <textarea class="form-control" id="features" name="features" rows="3" required></textarea>
                    </div>
                    <div class="form-group">
                        <label for="status">状态</label>
                        <select class="form-control" id="status" name="status" required>
                            <option value="active">Active</option>
                            <option value="inactive">Inactive</option>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary" name="update_plan">保存更改</button>
                </form>
            </div>
        </div>
    </div>

<script>
$(document).ready(function() {
    // Open the modal and populate fields with data
    $('.edit-btn').on('click', function() {
        var planData = $(this).data();
        
        // Fill the modal with the data from the selected plan
        $('#planId').val(planData.id);
        $('#planName').val(planData.name);
        $('#planType').val(planData.type);
        $('#planAmount').val(planData.amount);
        $('#txnLimit').val(planData.limit);
        $('#features').val(planData.features);
        $('#status').val(planData.status);
        
        // Show the modal
        $('#editPlanModal').modal('show');
    });
});
</script>

<?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>
<?php
// Assuming you have a footer file
require_once('../database/footer.php');
?>
