<?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 = 'suppliers'") 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>Manage Suppliers | <?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">

			<?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="registerNewSupplierForm">
							<div class="row">
								<div class="col-md-12">
									<h6 class="text-primary"><i class="fa fa-plus-square"></i> Register New Supplier</h6>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Supplier Name</label>
									<input class="form-control form-control-sm" type="text" name="txtSupplierNameRegisterNewSupplier" id="txtSupplierNameRegisterNewSupplier" placeholder="Example: Hirgal Electronics" required>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Supplier Address</label>
									<textarea class="form-control form-control-sm" style="resize: none;" rows="2" name="txtSupplierAddressRegisterNewSupplier" id="txtSupplierAddressRegisterNewSupplier" placeholder="Example: Bakaaro" required></textarea>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Contact Person</label>
									<input class="form-control form-control-sm" type="text" name="txtContactPersonRegisterNewSupplier" id="txtContactPersonRegisterNewSupplier" placeholder="Example: Mohamed Hassan" required>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="col-form-label col-form-label-sm">Mobile Number</label>
									<input type="text" class="form-control form-control-sm" name="txtSupplierMobileRegisterNewSupplier" id="txtSupplierMobileRegisterNewSupplier" placeholder="Example: 617336622" maxlength="9" required>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Email Address</label>
									<input class="form-control form-control-sm" type="text" name="txtSupplierEmailRegisterNewSupplier" id="txtSupplierEmailRegisterNewSupplier" placeholder="Example: example@email.com" required>
								</div>
							</div>
							<div class="row">
								<div class="form-group col-md-12 text-right mt-3">
									<button type="submit" class="btn btn-success btn-sm" name="btnRegisterNewSupplier" id="btnRegisterNewSupplier"><i class="fa fa-fw fa-lg fa-save"></i>Register Supplier</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-bordered table-sm" id="suppliersTable">
									<thead>
										<tr class="text-center bg-primary text-white">
											<th class="align-middle">No</th>
											<th class="align-middle">Supplier Name</th>
											<th class="align-middle">Contact Person</th>
											<th class="align-middle">Mobile</th>
											<th class="align-middle">Email</th>
											<th class="align-middle">Status</th>
                                            <?php if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1') || ($role_rs[5] == '1')) {?><th class="align-middle text-center">Action</th><?php } ?>
										</tr>
									</thead>
									<tbody>
										<?php
											$count_suppliers = 1;
											$getSuppliers = mysqli_query($conn, "SELECT * FROM suppliers ORDER BY supplierName ASC") or die(mysqli_error($conn));
											while ($suppliers_rs = mysqli_fetch_array($getSuppliers)) { ?>

												<tr>
                                                    <td class="align-middle text-center"><?php echo $count_suppliers; ?></td>
                                                    <td class="align-middle text-left py-1"><img class="img-fluid rounded-circle pr-2" src="img/no-image.jpg" width="25px" height="25px"/> <?php echo $suppliers_rs[2]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $suppliers_rs[4]; ?></td>
                                                    <td class="align-middle text-center"><?php echo $suppliers_rs[5]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $suppliers_rs[6]; ?></td>
                                                    <td class="align-middle text-center"><?php if ($suppliers_rs[7] == 1) { echo "<span class='badge badge-success'><i class='fa fa-check-circle'></i> Active</span>"; } else if ($suppliers_rs[7] == 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 btn-sm" data-toggle="dropdown">Action</button>
                                                                    <button type="button" class="btn btn-primary btn-sm 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 btnUpdateSupplier" id="<?php echo "Update". $suppliers_rs[1]; ?>" data-id="<?php echo $suppliers_rs[1]; ?>" data-toggle="modal" data-target="#updateSupplierModal"><i class="fa fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteSupplier" id="<?php echo "Delete". $suppliers_rs[1]; ?>" data-id="<?php echo $suppliers_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 btn-sm" data-toggle="dropdown">Action</button>
                                                                    <button type="button" class="btn btn-primary btn-sm 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 btnUpdateSupplier" id="<?php echo "Update". $suppliers_rs[1]; ?>" data-id="<?php echo $suppliers_rs[1]; ?>" data-toggle="modal" data-target="#updateSupplierModal"><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 btn-sm" data-toggle="dropdown">Action</button>
                                                                    <button type="button" class="btn btn-primary btn-sm 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 btnDeleteSupplier" id="<?php echo "Delete". $suppliers_rs[1]; ?>" data-id="<?php echo $suppliers_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
												</tr><?php
												$count_suppliers++;
											}
										?>
									</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">
			$("#suppliersTable").DataTable({"Length": 15, "order": []})
			$("#cmbSupplierStatusUpdateSupplier").select2({ tags: false, dropdownParent: $("#updateSupplierModal") })
		</script>
	</body>
</html>