<?php
session_start();
require("../../mainconfig.php");
$msg_type = "nothing";

if (isset($_SESSION['user'])) {
	$sess_username = $_SESSION['user']['username'];
	$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$sess_username'");
	$data_user = mysqli_fetch_assoc($check_user);
	if (mysqli_num_rows($check_user) == 0) {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['status'] == "Suspended") {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['level'] == "Member") {
		header("Location: ".$cfg_baseurl);
	} else {
		if (isset($_POST['add'])) {
			$post_username = $_POST['username'];
			$post_balance = $_POST['balance'];

			$checkdb_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$post_username'");
			$datadb_user = mysqli_fetch_assoc($checkdb_user);
			if (empty($post_username) || empty($post_balance)) {
				$msg_type = "error";
				$msg_content = '<b>Gagal:</b> Mohon mengisi semua input.<script>swal("Error!", "Mohon mengisi semua input.", "error");</script>';
			} else if (mysqli_num_rows($checkdb_user) == 0) {
				$msg_type = "error";
				$msg_content = '<b>Gagal:</b> User '.$post_username.' tidak ditemukan.<script>swal("Error!", "User tidak ditemukan.", "error");</script>';
			} else if ($post_balance < $cfg_min_transfer) {
				$msg_type = "error";
				$msg_content = '<b>Gagal:</b> Minimal transfer adalah Rp '.$cfg_min_transfer.'<script>swal("Error!", "Transfer tidak sesuai.", "error");</script>';
			} else if ($data_user['balance'] < $post_balance) {
				$msg_type = "error";
				$msg_content = '<b>Gagal:</b> Saldo Anda tidak mencukupi untuk melakukan transfer dengan jumlah tersebut.<script>swal("Error!", "Saldo tidak mencukupi.", "error");</script>';
			} else {
				$update_user = mysqli_query($db, "UPDATE users SET balance = balance-$post_balance WHERE username = '$sess_username'"); // cut sender
				$update_user = mysqli_query($db, "UPDATE users SET balance = balance+$post_balance WHERE username = '$post_username'"); // send receiver
				$insert_tf = mysqli_query($db, "INSERT INTO catatan (username, note, waktu) VALUES ('$sess_username', 'Kamu telah melakukan aktifitas Transfer Saldo sebesar $post_balance', '$date $time')");
				$insert_tf = mysqli_query($db, "INSERT INTO transfer_balance (sender, receiver, quantity, date) VALUES ('$sess_username', '$post_username', '$post_balance', '$date')");
				if ($insert_tf == TRUE) {
					$msg_type = "success";
					$msg_content = "<b>Berhasil:</b> Transfer saldo berhasil.<br /><b>Pengirim:</b> $sess_username<br /><b>Penerima:</b> $post_username<br /><b>Jumlah Transfer:</b> Rp ".number_format($post_balance,0,',','.')." Saldo<br /><b>Tanggal:</b> $date";
				} else {
					$msg_type = "error";
					$msg_content = '<b>Gagal:</b> Error system.<script>swal("Error!", "Error system.", "error");</script>';
				}
			}
		}

	include("../../lib/header.php");
?>
<div class="page-body"> 
<?php include('../../lib/partial/breadcrumb.php') ?>
   <!-- Container-fluid starts-->
   <div class="container-fluid">
            <div class="row justify-content-center">
              <div class="col-xl-12">
                  <?php 
											if ($msg_type == "success") {
											?>
											<div class="alert alert-success dark" role="alert">
                      <p><?php echo $msg_content; ?></p>
                    </div>
											<?php
											} else if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php
											}
											?>
                <div class="card height-equal">
                  <div class="card-body custom-input">
                    <form class="row g-3" method='POST'>
                        <div class="alert alert-info">
												- Saldo Anda akan dipotong sesuai jumlah transfer saldo.<br />
												- Minimal transfer Rp <?php echo number_format($cfg_min_transfer,0,',','.'); ?>.
											</div>
                      <div class="col-12"> 
                        <label class="form-label">Username</label>
                        <input class="form-control" type="text" placeholder="username" name='username'>
                      </div>
                      <div class="col-12"> 
                        <label class="form-label">Balance</label>
                        <input class="form-control" type="text" placeholder="balance" name='balance'>
                      </div>
                      <div class="col-12">
                        <button class="btn btn-primary" type="submit" name='add'>Submit</button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>
              </div>
   </div>
   <!-- Container-fluid Ends-->
</div>
<?php
	include("../../lib/footer.php");
	}
} else {
	header("Location: ".$cfg_baseurl);
}
?>