<?php
/**
 * dashboard.php -- Dashboard
 */

include '../inc/config.inc.php'; // Configuration

ob_start();
session_start();

//date_default_timezone_set('Africa/Lagos');

error_reporting(E_ALL);
ini_set('display_errors', 1);

if ( !isset($_SESSION['user']) || $_SESSION["loggedin"] !== true || trim($_SESSION['user']) == '' ){
header('Location: ' . $BASE_URL . 'signin');
exit;
}

// User Details
$stmt = $db_con->prepare("SELECT * FROM users WHERE id=:user_id");
$stmt->execute(array(":user_id"=>$_SESSION['user']));
$userRow = $stmt->fetch(PDO::FETCH_ASSOC);



$taxCodeInput = $_POST["taxCodeInput"];

$stmt = $db_con->prepare("SELECT * FROM code ORDER BY id DESC LIMIT 1");
$stmt->execute();
$allcoupon = $stmt->fetch(PDO::FETCH_ASSOC);
$amount = $_POST['amount'];

// var_dump($allcoupon["code"] == $taxCodeInput);

if ($taxCodeInput == FALSE) {
echo "<div class='alert alert-danger' style='text-align: center; color: red; background:none;'>tax code is empty</div>";
} else {
if ($allcoupon["code"] == $taxCodeInput) {
  $updateTB = $db_con->query("UPDATE user_total_balance SET amount=amount-'$amount', current_balance=current_balance-'$amount' WHERE user_id='".$userRow['id']."'");
  $updatePD = $db_con->query("UPDATE user_pending_withdraw SET amount='$amount', current_balance=current_balance+$amount WHERE user_id='".$userRow['id']."'");
  
  ?>
  
 <script>
  setTimeout(function(){
noticePop('Withdrawal Request Has Been Sent. Await Confirmation.', 'success');
}, 7000);
  </script>
<?php

} else {
echo "<div class='alert alert-danger' style='text-align: center; color: red; background:none;'>Your tax code is wrong try again</div>";
} 
}  