<?php
session_start();

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


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

  if((!empty($_POST['accountId'])) ) {

    if ($conn) {
      try {

        $accountId = $_POST['accountId'];

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

        $history = $sth->fetchAll();


        if (count($history) > 0) {

          foreach ($history as $key => $value) {
            // $firstName = $value['firstName'];
            // $lastName = $value['lastName'];
            // $user_name = $firstName. " ".$lastName;
            $user_name = $value['fullname'];

            $response = array(
              'user_name' => $user_name,
              'status' => "success",
            );

            echo (json_encode($response));

          }

        }else {
          die("User with account ID ".$accountId." not found.");
        }

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


function convertDate($date) {
 $dt = str_replace('/', '-', $date);

 return date('Y-m-d', strtotime($dt));
}

?>