<?php
// session_start();
require("../includes/db_connect.php");
// require_once("../includes/admin_session_token.php");
require_once("includes/admin_session_token.php");

$accountId = $_SESSION['adm_accountId'];


$rowperpage = 50;
$pageno = 1;

// $accountId = $_SESSION['usr_accountId'];

$sql = "SELECT accountId, date FROM users ORDER BY date ASC LIMIT 1";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute([':accountId' => $accountId]);
$users = $sth->fetchAll();

foreach ($users as $key => $value) {
  $date = $value['date'];
  $dt = new DateTime($date);
  $dateFrom = $dt->format('d/m/Y');
}

$dateTo =  date("d/m/Y");


$rowperpage = 50;

//Calculate total pages
$allcount_query = "SELECT * FROM kyc_documents WHERE full_legal_name <> 'default' ORDER BY date DESC";
$allcount_fetch = $conn->prepare($allcount_query, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$allcount_fetch->execute([]);
$allcount = $allcount_fetch->rowCount();

$all_history = $allcount_fetch->fetchAll();

$totalPages = ceil($allcount / $rowperpage);

//Fetch history
$sql = "SELECT kyc_documents.*, users.accountId, users.email FROM kyc_documents INNER JOIN users ON kyc_documents.accountId = users.accountId WHERE kyc_documents.full_legal_name <> 'default' ORDER BY kyc_documents.date DESC LIMIT 0, $rowperpage";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute([]);
$history = $sth->fetchAll();

$users_history = "";

if (count($history) > 0) {
  foreach ($history as $key => $value) {
    $accountId = $value['accountId'];
    $email = $value['email'];
    $ssn = $value['ssn'];
    $full_legal_name = $value['full_legal_name'];
    $nationality = $value['nationality'];
    $date_of_birth = $value['date_of_birth'];
    $address = $value['address'];
    $mobile_number = $value['mobile_number'];
    $email_address = $value['email_address'];
    $status  = $value['status'];
    $date = $value['date'];

    $dtf = new DateTime($date);
    $dis_date = $dtf->format('d/m/Y');

    if ($status == 1) {
      $action = "<span class='badge badge-pill badge-soft-success primary-color'>Approved</span>";
    }else {
      $action = "<a class='btn btn-xs btn-primary flex-fill' href='review-identity?accountId=$accountId'><b>Approve</b></a> <a class='btn btn-xs btn-danger flex-fill' href='?action=decline&accountId=$accountId'><b>Decline</b></a>";
    }

    $users_history.="
    <tr>
    <td>$accountId</td>
    <td>$email</td>
    <td>$ssn</td>
    <td>$full_legal_name</td>
    <td>$nationality</td>
    <td>$date_of_birth</td>
    <td>$address</td>
    <td>$mobile_number</td>
    <td>$email_address</td>
    <td>$action</td>
    <td>$dis_date</td>
    </tr>
    ";
  }

}else {
  $users_history = "<tr><td colspan='11' class='tx-color-03 tx-normal text-center'>No data available</td></tr>";
}


if (isset($_GET['action']) && isset($_GET['accountId'])) {

  $action = $_GET['action'];
  $accountId = $_GET['accountId'];

  if ($action == "decline") {

    $identity_sql = "UPDATE kyc_documents SET full_legal_name=?, ssn=?, nationality=?, date_of_birth=?, address=?, mobile_number=?, email_address=?, front_document=?, back_document=?, selfie_document=? WHERE accountId=?";

    $update_identity = $conn->prepare($identity_sql)->execute(["default", "", "default", "default", "default", "default", "default", "default", "default", "default", $accountId]);

    if ($update_identity) {
      header('Location: identity-verification');
      exit();
    }else {
      die("An unexpected error occurred. Please try again.");
    }
  }

}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Identity Verification</title>

    <?php require("includes/header.php"); ?>

    <div class="content ht-100v pd-0">
        <div class="content-header">
        </div><!-- content-header -->

        <div class="content-body">
            <div class="container pd-x-0">
                <div class="d-sm-flex align-items-center justify-content-between mg-b-20 mg-lg-b-25 mg-xl-b-30">
                    <div>
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb breadcrumb-style1 mg-b-10">
                                <li class="breadcrumb-item"><a href="#">Identity Verification</a></li>
                            </ol>
                        </nav>
                        <!-- <h4 class="mg-b-0 tx-spacing--1">Total Users (<?php echo ($allcount); ?>)</h4> -->
                    </div>
                </div>

                <div class="row row-xs">
                    <div class="col-sm-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="row col-xl-12">
                                    <div class="col-md-6 row">
                                        <div class="col-md-6">
                                            <label>From</label>

                                            <input type="text" id="dateFrom" class="form-control"
                                                placeholder="DD/MM/YYYY" value="<?php echo ($dateFrom) ?>">
                                        </div><!-- col -->
                                        <div class="col-md-6">
                                            <label>To</label>

                                            <input type="text" id="dateTo" class="form-control" placeholder="DD/MM/YYYY"
                                                value="<?php echo ($dateTo) ?>">
                                        </div><!-- col -->
                                    </div>
                                    <div class="col-md-4 search-field" style="padding-top: 28px; margin-bottom: 10px">

                                        <input type="text" id="search_field" class="form-control"
                                            placeholder="Search account ID, email..." name="search">
                                        <button id="search_button" class="btn" type="button">
                                            <i class="search-icon" data-feather="search"></i></i>
                                        </button>
                                    </div>
                                </div>

                                <div class="table-responsive" style="margin-top: 30px">
                                    <table class="table table-centered table-nowrap mb-0"
                                        style="min-width: max-content;">
                                        <thead class="thead-light">
                                            <tr>
                                                <th>Account ID</th>
                                                <th>User Email</th>
                                                <th>SSN</th>
                                                <th>Full Legal Name</th>
                                                <th>Nationality</th>
                                                <th>Date of Birth</th>
                                                <th>Address</th>
                                                <th>Mobile Number</th>
                                                <th>Email</th>
                                                <th>Action</th>
                                                <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody id="users_history">
                                            <?php echo ($users_history); ?>
                                        </tbody>
                                    </table>
                                    <input type="hidden" id="history_page" value="1">
                                    <input type="hidden" id="total_pages" value="<?php echo ($totalPages); ?>">
                                    <input type="hidden" id="all_history" value="<?php echo ($allcount); ?>">
                                    <input type="hidden" id="loading_status" value="loaded">

                                    <input type="hidden" id="delete_account_id" value="loaded">
                                </div>
                                <div class="col-md-10" style="margin-left: auto; margin-right: auto; margin-top: 20px">
                                    <div class="" style="float: right;">
                                        <nav aria-label="Page navigation example">
                                            <ul id="pagination" class="pagination pagination-circle mg-b-0" <?php if ($totalPages < 2) {
                                                                                                                echo "style='display: none'";
                                                                                                            } ?>>
                                                <li id="prev_history" class="page-item <?php if ($pageno <= 1) {
                                                                                            echo 'disabled';
                                                                                        } ?>">
                                                    <a class="page-link page-link-icon"
                                                        href="javascript:function previous() { return false; }"><i
                                                            data-feather="chevron-left"></i></a>
                                                </li>
                                                <li id="next_history" class="page-item <?php if ($pageno >= $totalPages) {
                                                                                            echo 'disabled';
                                                                                        } ?>">
                                                    <a class="page-link page-link-icon"
                                                        href="javascript:function next() { return false; }"><i
                                                            data-feather="chevron-right"></i></a>
                                                </li>
                                            </ul>
                                        </nav>
                                    </div><!-- df-example -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div><!-- row -->
            </div><!-- container -->
        </div>
    </div>
    </div>
    </div>

    <?php require("includes/footer.php"); ?>
    <script src="../js/admin/identity-verification.js"></script>

    </body>

</html>