<?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">User KYC</h6>
        </div>
        <?php
        // DELETE USER KYC
        if(isset($_POST['delete_kyc_btn']))
        {
            $kyc_userid = $_POST['delete_kyc'];
            $query_delete = "DELETE FROM kyc WHERE kyc_user_id = '$kyc_userid'";
            $user_update = "UPDATE users SET kyc_status = '0' WHERE user_id = '$kyc_userid'";
            if ($connection->query($query_delete)===TRUE && $connection->query($user_update)===TRUE) {
                $error = "delete_success";
            }else {
                $error = "error";
              }
        }


          // APPROVE USER KYC
        if(isset($_POST['approve_kyc_btn']))
        {
            $kyc_userid = $_POST['approve_kyc'];

            $kyc_approve = "UPDATE kyc SET status = 'approve' WHERE kyc_user_id = '$kyc_userid'";

            if ($connection->query($kyc_approve)===TRUE) {

              $user_update = "UPDATE users SET kyc_status = '2' WHERE user_id = '$kyc_userid'";
              if ($connection->query($user_update)===TRUE) {
                  $error = "approve_success";
              }else {
                  $error = "error";
                }
            }else {
                $error = "error";
              }
        }
         ?>
        <div class="card-body">
            <div class="table-responsive">
            <?php
                $i=1;
                $query = "SELECT * FROM kyc ORDER BY kyc_id DESC";
                $query_run = mysqli_query($connection, $query);
            ?>
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th> S/N </th>
                            <th> User ID </th>
                            <th> Fullname </th>
                            <th> Email </th>
                            <th> DOB </th>
                            <th> Address </th>
                            <th> Phone </th>
                            <th> ID Type </th>
                            <th> ID Upload </th>
                            <th> Proof of Address </th>
                            <th>Status</th>
                            <th>Date</th>
                            <th>ACTION</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if(mysqli_num_rows($query_run) > 0)
                        {
                            while($row = mysqli_fetch_assoc($query_run))
                            {
                        ?>
                            <tr>
                                <td><?php  echo $i++; ?></td>
                                <td>
                                  <?php
                                    $kyc_userid = $row['kyc_user_id'];
                                    echo $kyc_userid;
                                  ?></td>
                                <?php
                                $sql2 = "SELECT * FROM users WHERE user_id = '$kyc_userid'";
                                $result = $connection->query($sql2);
                                while ($row2 = $result->fetch_assoc()) {
                                    $uname = $row2['full_name'];
                                    $user_email = $row2['email'];
                                }
                                 ?>
                               <td><?php  echo $uname; ?></td>
                                <td><?php  echo $user_email ?></td>
                                <td><?php  echo $row['dob']; ?></td>
                                <td><?php  echo $row['address']; ?></td>
                                <td><?php  echo $row['phone']; ?></td>
                                <td style="text-transform:capitalize"><?php  echo $row['id_type']; ?></td>
                                <td><a href="../images/kyc/<?php echo $row['gov_id_file'] ?>">
                                  <img src="../images/kyc/<?php echo $row['gov_id_file'] ?>" alt="" style="max-width:40%">
                                </a></td>
                                <td>
                                  <a href="../images/kyc/<?php echo $row['proof_address_file'] ?>">
                                    <img src="../images/kyc/<?php echo $row['proof_address_file'] ?>" alt="" style="max-width:40%">
                                  </a>
                                </td>
                                <td style="text-transform:capitalize">
                                  <?php
                                    $kyc_status = $row['status'];
                                    if ($kyc_status == "pending"):
                                   ?>
                                   <span class="btn btn-warning"><?php echo $kyc_status ?></span>
                                   <?php
                                   elseif ($kyc_status == "approve"):
                                  ?>
                                  <a href="kyc.php" class="btn btn-success"><?php echo $kyc_status ?></a>
                               <?php endif; ?>
                                </td>
                                <td style="text-transform:capitalize"><?php  echo $row['date_created']; ?></td>
                                <?php
                                  if ($kyc_status == "approve"):
                                ?>
                                  <td>No Action</td>
                                  <?php else: ?>
                                  <td>
                                    <form action="" method="post" onsubmit="return(confirm('KYC approve confirmation, Click to proceed'))">
                                        <input type="hidden" name="approve_kyc" value="<?php echo $row['kyc_user_id']; ?>">
                                        <button type="submit" name="approve_kyc_btn" class="btn btn-success"> <i class="fa-solid fa-square-check"></i></button>
                                    </form>

                                      <form action="" method="post" onsubmit="return(confirm('KYC declined and delete confirmation, Click to proceed'))">
                                          <input type="hidden" name="delete_kyc" value="<?php echo $row['kyc_user_id']; ?>">
                                          <button type="submit" name="delete_kyc_btn" class="btn btn-danger"> <i class="fa-solid fa-trash-can"></i></button>
                                      </form>
                                  </td>
                                <?php endif; ?>
                            </tr>
                        <?php
                            }
                        }
                        else {
                            echo "No Record Found";
                        }
                        ?>
                    </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!", "User KYC was declined and deleted successfully", "success");
        setTimeout(() => {
            window.location.href = ''
        }, 2000);
    }if (error.textContent == "approve_success") {
      swal("Successful!", "User KYC approved successfully", "success");
        setTimeout(() => {
            window.location.href = 'kyc.php'
        }, 2000);
    }else if (error.textContent == "error") {
      swal("ERROR!", "Sorry!!!, Something went wrong, try later or contact support", "error");
    }

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