<?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 = 'expenses'") 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>
        <?php include("library/head.php");?>
        <title>Expenses | <?php echo $_SESSION['systemName']; ?></title>
	</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-list-ul fa-lg"></i> EXPENSES</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="expenses">Expenses</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" id="registerNewExpenseForm">
							<div class="row">
								<div class="form-group col-md-12">
									<h5 class="text-primary"><i class="fa fa-plus-square"></i> Register New Expense</h5>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Expense Type</label>
									<select class="form-control" style="width: 100%;" name="cmbExpenseTypeRegisterNewExpense" id="cmbExpenseTypeRegisterNewExpense" required>

									</select>
								</div>
								<div class="form-group col-sm-12">
									<label class="control-label">Expense Amount ($)</label>
									<input type="number" class="form-control" name="txtExpenseAmountRegisterNewExpense" id="txtExpenseAmountRegisterNewExpense" placeholder="Example: 130" step="0.01" 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="txtExpenseAmountWordsRegisterNewExpense" id="txtExpenseAmountWordsRegisterNewExpense" style="font-weight: bold;" required readonly>
								</div>
		
								<div class="form-group col-md-12">
									<label class="control-label">Withdrawal Account</label>
									<select class="form-control" style="width: 100%;" name="cmbExpensesWithdrawalAccountRegisterNewExpense" id="cmbExpensesWithdrawalAccountRegisterNewExpense" required>
		
									</select>
								</div>
								<div class="form-group col-sm-12">
									<label class="control-label">Description</label>
									<textarea class="form-control" name="txtExpensePaymentRemarksRegisterNewExpense" id="txtExpensePaymentRemarksRegisterNewExpense" placeholder="Enter Expense Remarks..." required></textarea>
								</div>
								<div class="form-group col-md-7">
									<label class="control-label">Payment Date</label>
									<input class="form-control" type="date" name="txtExpenseDateRegisterNewExpense" id="txtExpenseDateRegisterNewExpense" max="<?php echo date("Y-m-d"); ?>" value="<?php echo date("Y-m-d"); ?>" required>
									<input class="form-control" type="hidden" name="txtRegisteredByRegisterNewExpense" id="txtRegisteredByRegisterNewExpense" value="<?php echo $_SESSION['userIdd']; ?>" required>
								</div>
								<div class="form-group col-md-5">
									<label class="control-label">Payment Time</label>
								    <input class="form-control" type="time" name="txtRegisterTimeNewExpense" id="txtRegisterTimeNewExpense" value="<?php echo date("H:i"); ?>" required>
									<input class="form-control" type="hidden" name="txtRegisteredByRegisterNewExpense" id="txtRegisteredByRegisterNewExpense" 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="btnRegisterNewExpense" id="btnRegisterNewExpense"><i class="fa fa-fw fa-lg fa-save"></i>Register Expense</button>
								</div>
							</div>
						</form>
					</div>
				</div>


				<div class="col-md-9">
					<div class="tile">
						<div class="tile-body">
							<div class="row">
								<div class="form-group col-md-12">
									<h5><span><i class="fa fa-list-ul"></i> Expenses List</span></h5>
									<hr />
								</div>
							</div>
							<div class="table-responsive">
                                <table class="table table-bordered table-sm" height="35px" id="expensesTable">
									<thead>
										<tr class="text-center bg-primary text-light">
											<th class="align-middle">Date</th>
											<th class="align-middle">Type</th>
											<th class="align-middle">Description</th>
											<th class="align-middle">Amount</th>
											<th class="align-middle">Process</th>
											<th class="align-middle">Approve</th>
                                            <?php if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1') || ($role_rs[5] == '1')) {?><th class="align-middle"><center>Action</center></th><?php } ?>

										</tr>
									</thead>
									<tbody>
										
										<?php
											$expensesListEx = mysqli_query($conn, "SELECT expenses.expenseID, expenses.paymentDate, expense_types.expenseTypeName, expenses.expenseRemarks, payment_gateways.gatewayType, payment_gateways.gatewayLogo, payment_gateways.gatewayName, account_numbers.accountNoName, account_numbers.accountNumber, expenses.expenseAmount, processors.Username, approver.Username, expenses.approvedDate
											FROM expenses 
											JOIN expense_types ON expenses.expenseTypeID = expense_types.expenseTypeID
											JOIN account_numbers ON expenses.accountNoID = account_numbers.accountNoID
											JOIN payment_gateways ON account_numbers.gatewayID = payment_gateways.gatewayID
											LEFT JOIN system_users AS processors ON expenses.registeredBy = processors.userID 
											LEFT JOIN system_users AS approver ON expenses.approvedBy = approver.userID
											WHERE expenses.expenseStatus != '0' 
											ORDER BY expenses.registerDate DESC") or die(mysqli_error($conn));
											while ($rs = mysqli_fetch_array($expensesListEx)) { ?>
                                                <tr>
                                                    <td class="align-middle text-center" width="12%"><?php echo substr($rs[1], 0, 16); ?></td>
                                                    <td class="align-middle text-left" width="20%"><?php echo $rs[2]; ?></td>
                                                    <td class="align-middle text-left" width="32%"><?php echo $rs[3]; ?></td>
                                                    <td class="align-middle text-center" width="10%"> <?php echo "$ ". number_format($rs[9], 2); ?></td>
													<td class="align-middle text-center" width="13%"> 
													    <?php 
													        if ($rs[10] != NULL) {
													            ?><span class="btn btn-secondary btn-sm text-light" data-toggle="tooltip" data-html="true" title="<?php echo "Processed By: <b>".$rs[10]."</b><br>Date: <b>". $rs[1]. "</b>"; ?>"><i class="fa fa-check"></i> Processed</span><?php 
													        } 
													    ?>
												    </td>
													<?php
														if ($rs[9] <= 300) {
															if ($_SESSION['userTypeID'] == 'PS0001' || $_SESSION['userTypeID'] == 'PS0002' || $_SESSION['userTypeID'] == 'PS0003' || $_SESSION['userTypeID'] == 'PS0004') {
																?><td class="align-middle text-center" width="13%">
																	<?php
																		if ($rs[11] === NULL) {
																			?><span class="btn btn-warning btn-sm text-light btnApproveExpense" data-id="<?php echo $rs[0]; ?>">Approve <i class="fa fa-question-circle"></i></span><?php
																		} else {
																			?><span class="btn btn-primary btn-sm text-light" data-toggle="tooltip" data-html="true" title="<?php echo "Approved By: <b>".$rs[11]."</b><br>Date: <b>". $rs[12]. "</b>"; ?>"><i class="fa fa-check"></i> Approved</span><?php
																		}
																	?>
																</td><?php
															} else {
																?><td class="align-middle text-center" width="13%">
																	<?php
																		if ($rs[11] === NULL) {
																			?><span class="btn btn-dark btn-sm text-light">Waiting <i class="fa fa-question-circle"></i></span><?php
																		} else {
																			?><span class="btn btn-primary btn-sm text-light" data-toggle="tooltip" data-html="true" title="<?php echo "Approved By: <b>".$rs[11]."</b><br>Date: <b>". $rs[12]. "</b>"; ?>"><i class="fa fa-check"></i> Approved</span><?php
																		}
																	?>
																</td><?php
															}
														} else if ($rs[9] > 300) {
															if ($_SESSION['userTypeID'] == 'PS0001' || $_SESSION['userTypeID'] == 'PS0002' || $_SESSION['userTypeID'] == 'PS0003') {
																?><td class="align-middle text-center" width="13%">
																	<?php
																		if ($rs[11] === NULL) {
																			?><span class="btn btn-warning btn-sm text-light btnApproveExpense" data-id="<?php echo $rs[0]; ?>">Approve <i class="fa fa-question-circle"></i></span><?php
																		} else {
																			?><span class="btn btn-primary btn-sm text-light" data-toggle="tooltip" data-html="true" title="<?php echo "Approved By: <b>".$rs[11]."</b><br>Date: <b>". $rs[12]. "</b>"; ?>"><i class="fa fa-check"></i> Approved</span><?php
																		}
																	?>
																</td><?php
															} else {
																?><td class="align-middle text-center" width="13%">
																	<?php
																		if ($rs[11] === NULL) {
																			?><span class="btn btn-dark btn-sm text-light">Waiting <i class="fa fa-question-circle"></i></span><?php
																		} else {
																			?><span class="btn btn-primary btn-sm text-light" data-toggle="tooltip" data-html="true" title="<?php echo "Approved By: <b>".$rs[11]."</b><br>Date: <b>". $rs[12]. "</b>"; ?>"><i class="fa fa-check"></i> Approved</span><?php
																		}
																	?>
																</td><?php
															}
														}											
													?>
													
													<?php
                                                        if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[5] == '1')) {
                                                            ?><td class="align-middle text-center" width="10%">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary">Action</button>
                                                                    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                        <span class="caret"></span>
                                                                    </button>
                                                                    <div class="dropdown-menu" style="min-width: 8rem;">
                                                                        <a href="#" class="dropdown-item text-danger btnCancelExpense" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]. "###0". "###". $_SESSION['userIdd']. "###". date("Y-m-d H:s:i"); ?>"><i class="fa fa-fw fa-lg fa-times-rectangle"></i> Cancel</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
                                                </tr><?php
											}
										?>
									</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">
			$('#expensesTable').DataTable({"pageLength": 100, "order": []})
			$("#cmbExpenseTypeRegisterNewExpense, #cmbExpensesWithdrawalAccountRegisterNewExpense").select2({ tags: false })
		</script>

	</body>
</html>