<?php
	session_start();
	include("library/conn.php");
	
	$role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'account_transactions'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Account Transactions | <?php echo $_SESSION['systemName']; ?></title>
		<?php include("library/head.php");?>
	</head>
	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
		<?php include("library/sidebar.php");?>
  
		<main class="app-content">
			<div class="app-title">
				<div>
					<h4 class="text-primary"><i class="fa fa-credit-card-alt fa-lg"></i> ACCOUNT TRANSACTIONS</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="account-transactions">Account Transaction</a></li>
				</ul>
			</div>

			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>

			<div class="row">

				<div class="col-md-3">
					<div class="tile">
						<form action="#" method="POST" name="registerAccountTransactionForm" id="registerAccountTransactionForm">
							<div class="row">
								<div class="form-group col-md-12">
									<h5 class="text-primary"><i class="fa fa-plus-square"></i> Register New Transaction</h5>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Account Name</label>
									<select class="form-control" style="width: 100%;" name="cmbAccountNameNewAccountTransaction" id="cmbAccountNameNewAccountTransaction" required>

									</select>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Transaction Type</label>
									<select class="form-control" style="width: 100%;" name="cmbTransactionTypeNewAccountTransaction" id="cmbTransactionTypeNewAccountTransaction" required>
										<option value="">Select Type</option>
										<option value="1">Deposit</option>
										<option value="2">Withdrawal</option>
									</select>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Amount</label>
									<input type="text" class="form-control" name="txtAmountNewAccountTransaction" id="txtAmountNewAccountTransaction" placeholder="Example: 130" min="0" required>
								</div>
								
								<div class="form-group col-sm-12">
									<label class="control-label">Amount in Words</label>
									<input type="text" class="form-control" name="txtAmountInWordsNewAccountTransaction" id="txtAmountInWordsNewAccountTransaction" style="font-weight: bold;" required readonly>
								</div>
								<div class="form-group col-sm-12">
									<label class="control-label">Description</label>
									<textarea class="form-control" name="txtTransactionDescriptionNewAccountTransaction" id="txtTransactionDescriptionNewAccountTransaction" placeholder="Enter transaction remarks..." required></textarea>
								</div>
								<div class="form-group col-md-7">
									<label class="control-label">Transaction Date</label>
									<input class="form-control" type="date" name="txtRegisterDateNewAccountTransaction" id="txtRegisterDateNewAccountTransaction" max="<?php echo date("Y-m-d"); ?>" value="<?php echo date("Y-m-d"); ?>" required>
								</div>
								<div class="form-group col-md-5 pl-0">
								    <label class="control-label">Time</label>
								    <input class="form-control" type="time" name="txtRegisterTimeNewAccountTransaction" id="txtRegisterTimeNewAccountTransaction" value="<?php echo date("H:i"); ?>" required>
									<input class="form-control" type="hidden" name="txtRegisteredByNewAccountTransaction" id="txtRegisteredByNewAccountTransaction" value="<?php echo $_SESSION['userIdd']; ?>" required>
								</div>
								<div class="form-group col-md-12 text-right pt-2">
									<button type="submit" class="btn btn-success" name="btnRegisterAccountTransaction" id="btnRegisterAccountTransaction"><i class="fa fa-fw fa-lg fa-save"></i>Register Transaction</button>
								</div>
							</div>
						</form>
					</div>
				</div>

				<div class="col-md-9">
					<div class="tile">
						<div class="tile-body">
							<div class="table-responsive">
							    <table class="table table-striped table-bordered table-hover table-sm" height="35px" id="accountTranbsactionsTable">
									<thead>
										<tr class="text-center bg-primary text-light">
											<th class="align-middle">Date</th>
											<th class="align-middle">Description</th>
											<th class="align-middle">Account</th>
											<th class="align-middle">Type</th>
											<th class="align-middle">Amount</th>
                                            <th class="align-middle">Action</th>
										</tr>
									</thead>
									<tbody>
										<?php
											$getAccountTransactions = mysqli_query($conn, "SELECT account_transactions.transactionID, account_transactions.registerDate, account_transactions.transactionDescription, payment_gateways.gatewayType, payment_gateways.gatewayLogo, payment_gateways.gatewayName, account_numbers.accountNoName, account_numbers.accountNumber, account_transactions.withdrawalAmount, account_transactions.depositedAmount
											FROM account_transactions
											JOIN account_numbers ON account_transactions.accountNoID = account_numbers.accountNoID
											JOIN payment_gateways ON account_numbers.gatewayID = payment_gateways.gatewayID
											WHERE account_transactions.transactionDescription != 'Openning Balance' AND account_transactions.referenceID = '0' AND account_transactions.transactionStatus = '1' ORDER BY account_transactions.rowNo DESC") or die(mysqli_error($conn));
											$x = 1;
											while ($transaction_rs = mysqli_fetch_array($getAccountTransactions)) {
												?>
													<tr>
													    <td class="align-middle text-center"><?php echo substr($transaction_rs[1], 0, 16); ?></td>
													    <td class="align-middle text-left"><?php echo $transaction_rs[2]; ?></td>
														<td class="py-1 align-middle text-left">
														    <?php 
														        if ($transaction_rs[3] === "1") {
														            $compain_details = $transaction_rs[5]. " (". $transaction_rs[7]. ") ";
														        } else if ($transaction_rs[3] === "2") {
														            $compain_details = $transaction_rs[5]. " ". $transaction_rs[6]. " (". $transaction_rs[7]. ") ";
														        } else if ($transaction_rs[3] === "3") {
														            $compain_details = $transaction_rs[6]. " (". $transaction_rs[7]. ") ";
														        }
														    ?>
														    <img class="img-fluid img-thumbnail rounded" src="<?php echo $transaction_rs[4]; ?>" width="25px" height="25px"/> &nbsp; <?php echo $compain_details; ?>
														</td>
														<td class="align-middle text-center"><?php if ($transaction_rs[8] > 0) { echo "Withdraw"; } else { echo "Deposit"; } ?></td>
														<td class="align-middle text-center font-weight-bold"><?php if ($transaction_rs[8] > 0) { echo "<span class='text-danger'>$ ". number_format($transaction_rs[8], 2). "</span>"; } else { echo "<span class='text-success'>$ ". number_format($transaction_rs[9], 2). "</span>"; } ?></td>
														<td class="align-middle text-center"><span class="btn btn-outline-danger btnDeleteAccountTransaction" id="<?php echo "Delete". $transaction_rs[0]; ?>" data-id="<?php echo $transaction_rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</span></td>
													</tr>
												<?php
												$x++;
											}
										?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</main>
  
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>  
		<?php include("library/scripts.php");?>
		<script type="text/javascript">$('#accountTranbsactionsTable').DataTable({"pageLength": 100, "order": []});</script>
		<script type="text/javascript"> $("#cmbAccountNameNewAccountTransaction, #cmbTransactionTypeNewAccountTransaction").select2({ tags: false });</script>
	</body>
</html>