<?php
ob_start();
session_start();

require_once 'core/classes.php';
require_once 'core/classes/user.php';

if(!isset($_GET["id"])) {
  header("Location: ?id=".$_SESSION['user']);
} else {
  $id = (int)$_GET["id"];
}

if(isset($_SESSION['user'])) {
$user = new User($con, $_SESSION['user']);
$loggedIn = true;
} else {
$loggedIn = false; 
}

function getMoney($money) {
		if ($money < 1000000) {
         // Anything less than a million
         return number_format($money);
        } else if ($money < 1000000000) {
         // Anything less than a billion
         return number_format($money / 1000000, 1) . 'M';
        } else {
         // At least a billion
         return number_format($money / 1000000000, 1) . 'B';
        }
}

$q = $con->prepare("SELECT * FROM catalog WHERE id = :id");
$q->bindParam(':id',$id,PDO::PARAM_INT);
$q->execute();
$item = $q->fetch();
$itemUser = new User($con, $item['creator']);
if(!$item) {
  header('location: /error?err=404');
  exit;
}

if($loggedIn == true) { 
if($_POST['submit'] ?? "No" == "Yes") {
if($user->getMoney('false') >= htmlspecialchars($item["price"])) {
$q = $con->prepare("UPDATE users SET money=money-:price WHERE id=:id");
$q->bindParam(':id',$_SESSION['user'],PDO::PARAM_INT);
$q->bindParam(':price',$item['price'],PDO::PARAM_INT);
$q->execute();
}
}
}
?>
<!DOCTYPE html>
<html data-bs-theme="dark">
<head>
<?php
$getitstarted = new PartyStarter;
$getitstarted->header();
?>
</head>
<title><?php echo htmlspecialchars($item["name"]); ?> | <?php echo $sitename; ?></title>
<body>
<main class="container">
<div class="mx-auto w-75">
<br><br>

<div class="card shadow-sm w-100">
  <h3 class="card-header text-start"> <?php echo htmlspecialchars($item["name"]); ?> <?php if($item['limited'] == 1) { echo '
		<span class="float-end badge bg-success">Limited</span>'; }; ?> </h3>
  <div class="card-body mx-md-auto mx-xl-0">
     <div class="row g-2">
    <div class="col-md-6 align-self-center d-block text-center">
    <img height=300 width=300 class="img-thumbnail border-0" src="
			<?php if (!empty($item["thumbnail"])) {echo htmlspecialchars($item["thumbnail"]);} else {echo "assets/placeholder.png";} ?>">
	</div>
    <div class="col p-4 mt-md-2 ms-md-0 ms-lg-4 mt-lg-0 border-0 rounded-2 align-self-center">
      <a href="user?id=<?php echo $item['creator']; ?>" class="text-decoration-none d-flex">
        <img class="float-start border border-1 rounded-1 me-2" width=64 height=64 alt="<?php echo htmlspecialchars($item["name"]); ?>" src="<?php if (!empty($itemUser->getAvatar())) {echo htmlspecialchars($itemUser->getAvatar()); } ?>">
        <p class="text-white align-self-center fs-5 mt-2"> <?php echo $itemUser->getUsername(); ?> </p>
      </a>
      <br>
      <p>Price: <span class="text-success">
          <span class="grufont fs-5">@ </span> <?php echo getMoney(htmlspecialchars($item["price"]))." (".htmlspecialchars($item["price"]).")"; ?> </span>
      </p>
      <p>Description: <span class="text-white"> <?php echo htmlspecialchars($item["description"]); ?> </span>
      </p>
      <p>Type: <span class="text-white"> <?php echo ucfirst(htmlspecialchars($item["type"])); ?> </span>
      </p>
      <p>Sales: <span class="text-white"> <?php echo ucfirst(htmlspecialchars($item["sales"])); ?> </span>
      </p>
      <button class="btn btn-success fs-4 w-100 <?php if($loggedIn == false) { echo "disabled"; } ?>" data-bs-toggle="modal" data-bs-target="#exampleModal">Buy with <span class="grufont fs-3">@</span>
      </button>
    </div>
  </div>
</div>
</div>

<div class="clearfix my-4"></div>

<div class="card"> 
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs nav-fill">
    <li class="nav-item"> <button class="nav-link active" id="comments-tab" data-bs-toggle="pill" data-bs-target="#comments" type="button" role="tab" aria-controls="comments" aria-selected="true">Comments</button> </li>
    <li class="nav-item"> <button class="nav-link" id="trades-tab" data-bs-toggle="pill" data-bs-target="#trades" type="button" role="tab" aria-controls="trades" aria-selected="false">Trades</button> </li>
</ul>
</div>
 <div class="card-body text-center">
  <div class="tab-content" id="v-pills-tabContent">
    <div class="tab-pane fade show active" id="comments" role="tabpanel" aria-labelledby="comments-tab" tabindex="0">soon commentz</div>
    <div class="tab-pane fade" id="trades" role="tabpanel" aria-labelledby="trades-tab" tabindex="0">idk if we adding trades..</div>
  </div>
 </div>
</div>
</div>




</div>
<?php if($loggedIn == true) { 
if($user->getMoney('false') >= htmlspecialchars($item["price"])) {
echo '
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Confirmation</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        Are you sure you want to buy <b>'.htmlspecialchars($item["name"]).'</b> for <b><span class="grufont fs-5">@ </span>'.getMoney(htmlspecialchars($item["price"])).' ('.htmlspecialchars($item["price"]).')</b>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">No</button>
        <form action="" method="post"> <input type="submit" name="submit" class="btn btn-success" value="Yes"> </form>
      </div>
    </div>
  </div>
</div>
'; 
} else {
echo '
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Confirmation</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        You cannot afford <b>'.htmlspecialchars($item["name"]).'</b>, you need <b><span class="grufont fs-5">@ </span>'.getMoney(htmlspecialchars($item["price"] - $user->getMoney('false'))).' ('.htmlspecialchars($item["price"] - $user->getMoney('false')).')</b> more.
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">No</button>
        <button type="button" class="btn btn-success disabled">Yes</button>
      </div>
    </div>
  </div>
</div>
'; 
}
}
?>


</main>
</body>
</html>
