<?php
include('includes/header.php');
include('includes/navbar.php');
?>

<div class="container-fluid">

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary" style="margin-bottom:5px">Upgrade Plans</h6>
            <a href="edit_plan.php" class="btn btn-primary"  style="margin-bottom:10px"> <i class="fa-solid fa-plus"></i> Add New Plan</a>
        </div>
        <?php
        // DELETE USER DATA
        if(isset($_POST['delete_uid'])){
            $package_id = $_POST['package_id'];

            $sqldep = "SELECT * FROM packages WHERE package_id = '$package_id'";
            $dep_result = $connection->query($sqldep);
            while ($row = $dep_result->fetch_assoc()) {
              $db_uname = $row['package_name'];
          }

            $query_delete = "DELETE FROM packages WHERE package_id = '$package_id'";
            if ($connection->query($query_delete)===TRUE) {
                $error = "delete_success";
            }else {
                $error = "error";
              }
        }
         ?>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th> S/N </th>
                            <th> Plan Name </th>
                            <th> Min Amount </th>
                            <th> Max Amount </th>
                            <th> Return </th>
                            <th> Referral </th>
                            <th> Gift Bonus </th>
                            <th> Duration</th>
                            <th> Insurance</th>
                            <th> Recommended</th>
                            <th> Extra Features</th>
                            <th> Status</th>
                            <th>ACTION</th>
                        </tr>
                    </thead>
                    <tbody>
                      <?php
                          $sqldep = "SELECT * FROM packages";
                          $dep_result = mysqli_query($connection, $sqldep);
                          $i = 1;
                          if(mysqli_num_rows($dep_result) > 0){
                            while ($row = mysqli_fetch_assoc($dep_result)){
                              $package_id = $row['package_id'];
                              $package_name = $row['package_name'];
                              $package_min = $row['package_min'];
                              $package_max = $row['package_max'];
                              $return = $row['return_Percent'];
                              $referral = $row['referral'];
                              $gift_bonus = $row['gift_bonus'];
                              $duration = $row['duration'];
                              $insurance = $row['insurance'];
                              $recommend = $row['recommend'];
                              $other_features = $row['other_features'];
                              $status = $row['status'];

                              $d_min = number_format("$package_min",2);
                              $d_max = number_format("$package_max",2);
                              $d_gift_bonus = number_format("$gift_bonus",2);

                      ?>
                      <tr>
                        <td><?php echo $i++ ?></td>
                        <td><?php echo $package_name ?></td>
                        <td><?php echo $default_currency ?><?php echo $d_min ?></td>
                        <td><?php echo $default_currency ?><?php echo $d_max ?></td>
                        <td><?php echo $return ?>%</td>
                        <td><?php echo $referral ?>%</td>
                        <td><?php echo $default_currency ?><?php echo $d_gift_bonus ?></td>
                        <td><?php echo $duration * 24?>Hours</td>
                        <td><?php echo $insurance ?>%</td>
                        <td>
                          <?php if ($recommend == "yes"): ?>
                            <span class="text-success" style="text-transform:uppercase"> <b><?php echo $recommend ?></b> </span>
                          <?php elseif ($recommend == "no"): ?>
                              <span class="text-danger" style="text-transform:uppercase"><b><?php echo $recommend ?></b></span>
                          <?php endif; ?>
                        </td>
                        <td>
                          <?php if ($other_features == "yes"): ?>
                            <span class="text-success" style="text-transform:uppercase"> <b><?php echo $other_features ?></b> </span>
                          <?php elseif ($other_features == "no"): ?>
                              <span class="text-danger" style="text-transform:uppercase"><b><?php echo $other_features ?></b></span>
                          <?php endif; ?>
                        </td>
                        <td>
                          <?php if ($status == "active"): ?>
                            <span class="text-success" style="text-transform:uppercase"> <b><?php echo $status ?></b> </span>
                          <?php elseif ($status == "inactive"): ?>
                              <span class="text-danger" style="text-transform:uppercase"><b><?php echo $status ?></b></span>
                          <?php endif; ?>
                        </td>
                        <td>
                            <form action="" method="post" onsubmit="return(confirm('Delete <?php echo $package_name ?> Upgrade Plan, Click to proceed'))">
                                <input type="hidden" name="package_id" value="<?php echo $package_id; ?>">
                                <button type="submit" name="delete_uid" class="btn btn-danger"> <i class="fa-solid fa-trash-can"></i></button>
                                <button type="button" onclick="window.location.href='edit_plan.php?edit_pid=<?php echo $package_id; ?>'" class="btn btn-success"> <i class="fa-solid fa-user-pen"></i></button>
                            </form>
                        </td>
                      </tr>
                      <?php
                          }
                      }
                      else {

                      ?>
                      <center><h3 class="badge badge-info">No Deposit Method Added Yet</h3></center>
                    <?php } ?>

                    </tbody>
                </table>

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



</div>
<!-- /.container-fluid -->
<p id="error" style='display: none '><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == "delete_success") {
      swal("Successful!", "<?php echo $db_uname ?> upgrade plan deleted successfully", "success");
        setTimeout(() => {
            window.location.href = ''
        }, 2000);
    }else if (error.textContent == "error") {
      swal("ERROR!", "Sorry!!!, Something went wrong, try later or contact support", "error");
      setTimeout(() => {
          window.location.href = ''
      }, 3000);
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
