<?php
session_start();

require("../includes/db_connect.php");


if($_SERVER['REQUEST_METHOD']==='POST') {

  if((!empty($_SESSION['approve_accountId'])) ) {

    if ($conn) {
      try {

        $approve_accountId = $_SESSION['approve_accountId'];

        $sql = "SELECT * FROM kyc_documents WHERE accountId=? LIMIT 1";
        $sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
        $sth->execute([$approve_accountId]);
        $kyc_documents = $sth->fetchAll();

        if (count($kyc_documents) > 0) {

          foreach ($kyc_documents as $key => $value) {

            $status  = $value['status'];

            if ($status == 0) {

              $identity_sql = "UPDATE kyc_documents SET status=? WHERE accountId=?";

              $update_identity = $conn->prepare($identity_sql)->execute([1, $approve_accountId]);

              if ($update_identity) {
                die("success");
              }else {
                die("An unexpected error occurred. Please try again.");
              }

            }else {
              die("Identity has already been approved.");
            }

          }

        }else {
          die("Identity not found.");
        }

      } catch(PDOException $e) {
        die('An unexpected error occurred. Connection failed');
      }
    }else {
      die('An unexpected error occurred. Connection failed.');
    }
  } else {
    die('Invalid request.');
  }
}else {
  die('Invalid request.');
}



?>