<?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 = 'customers'") 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>Customers | <?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-users fa-lg"></i> CUSTOMERS</h4>
					<p>Customers List Page</p>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="customers">Customers</a></li>
				</ul>
			</div>

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

			<div class="row">
				<div class="col-md-4">
					<div class="tile">
						<form action="#" method="POST" id="registerNewCustomerPageForm">
							<div class="row">
								<div class="form-group col-md-12">
									<h5 class="text-primary"><i class="fa fa-user-plus"></i> Register New Customer</h5>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Customer Name</label>
									<input class="form-control" type="text" name="txtCustomerNameRegisterNewCustomerPage" id="txtCustomerNameRegisterNewCustomerPage" placeholder="Example: Mohamed Ali Jamac" required>
								</div>
								<div class="form-group col-sm-12">
									<label class="control-label">Mobile</label>
									<input type="text" class="form-control" name="txtCustomerMobileRegisterNewCustomerPage" id="txtCustomerMobileRegisterNewCustomerPage" placeholder="Example: 617336622" maxlength="9" required>
								</div>
								<div class="form-group col-md-12">
									<label class="control-label">Address</label>
									<input type="text" class="form-control" name="txtCustomerAddressRegisterNewCustomerPage" id="txtCustomerAddressRegisterNewCustomerPage" placeholder="Example: Waaberi" required>
									<input type="hidden" name="txtRegisteredByRegisterNewCustomerPage" id="txtRegisteredByRegisterNewCustomerPage" value="<?php echo $_SESSION['userIdd']; ?>" required>
									<input type="hidden" name="txtRegisterDateRegisterNewCustomerPage" id="txtRegisterDateRegisterNewCustomerPage" value="<?php echo date("Y-m-d H:i"); ?>" required>
								</div>
								<div class="form-group col-md-12 text-right mt-3">
									<button type="submit" class="btn btn-primary" name="btnRegisterNewCustomerPage" id="btnRegisterNewCustomerPage"><i class="fa fa-fw fa-lg fa-save"></i>Register Customer</button>
								</div>
							</div>
						</form>
					</div>
				</div>

				<div class="col-md-8">
					<div class="tile">
						<div class="tile-body">
							<div class="table-responsive">
								<table class="table table-bordered table-sm" id="customersTable">
									<thead>
										<tr class="text-center bg-primary text-white" height="35px">
											<th class="align-middle">No</th>
											<th class="align-middle">Photo</th>
											<th class="align-middle">Customer Name</th>
											<th class="align-middle">Mobile</th>
											<th class="align-middle">Address</th>
											<th class="align-middle">Status</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
											$count_customers = 1;
											$getCustomers = mysqli_query($conn, "SELECT * FROM customers ORDER BY rowNo ASC") or die(mysqli_error($conn));
											while ($customers_rs = mysqli_fetch_array($getCustomers)) { ?>

												<tr>
                                                    <td class="text-center"><?php echo $count_customers; ?></td>
                                                    <td class="align-middle text-center py-1"><img class="img-fluid rounded-circle" src="img/no-image.jpg" width="35px" height="35px"/></td>
                                                    <td class="align-middle text-left"><?php echo $customers_rs[2]; ?></td>
                                                    <td class="align-middle text-center"><?php echo $customers_rs[3]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $customers_rs[4]; ?></td>
                                                    <td class="align-middle text-center"><?php if ($customers_rs[5] == 1) { echo "<span class='badge badge-success'><i class='fa fa-check-circle'></i> Active</span>"; } else if ($customers_rs[5] == 0) { echo "<span class='badge badge-danger'><i class='fa fa-times-circle'></i> InAcrive</span>"; } ?></td>
                                                    <?php
                                                        if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary" data-toggle="dropdown">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-primary btnUpdateCustomer" id="<?php echo "Update". $customers_rs[1]; ?>" data-id="<?php echo $customers_rs[1]; ?>" data-toggle="modal" data-target="#updateCustomerModal"><i class="fa fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteCustomer" id="<?php echo "Delete". $customers_rs[1]; ?>" data-id="<?php echo $customers_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                                                           </td><?php
                                                        } else if ($role_rs[4] == '1') {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary" data-toggle="dropdown">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-primary btnUpdateCustomer" id="<?php echo "Update". $customers_rs[1]; ?>" data-id="<?php echo $customers_rs[1]; ?>" data-toggle="modal" data-target="#updateCustomerModal"><i class="fa fa-edit"></i> Edit</a>
                                                                    </div>
                                                                </div>                                                            
                                                            </td><?php
                                                        } else if ($role_rs[5] == '1') {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary" data-toggle="dropdown">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 btnDeleteCustomer" id="<?php echo "Delete". $customers_rs[1]; ?>" data-id="<?php echo $customers_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
												</tr><?php
												$count_customers++;
											}
										?>
									</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">
			$("#customersTable").DataTable({"pageLength": 15, "order": []})
			$("#cmbCustomerGenderRegisterNewCustomer, #cmbCustomerStatusRegisterNewCustomer").select2({ tags: false })
			$("#cmbCustomersCustomerGenderUp, #cmbCustomersCustomerStatusUp").select2({ tags: false, dropdownParent: $("#updateCustomerModal") })
		</script>
	</body>
</html>