<?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 = 'item_units'") 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>Item Units | <?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-th-large"><i class="fa fa-filter fa-lg"></i> UNITS OF MEASURE</h4>
					<p>Units of Measure 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="units-of-measure">Units of Measure</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="registerNewUnitForm">
							<div class="row">
								<div class="col-md-12">
									<h6 class="text-primary"><i class="fa fa-plus-square"></i> Register New Unit</h6>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Unit Name</label>
									<input class="form-control form-control-sm" type="text" name="txtUnitNameRegisterNewUnit" id="txtUnitNameRegisterNewUnit" placeholder="Example: Kilogram" required>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Unit Remarks</label>
									<textarea class="form-control form-control-sm" style="resize: none;" rows="4" name="txtUnitRemarksRegisterNewUnit" id="txtUnitRemarksRegisterNewUnit" placeholder="Example: Unit of weight" required></textarea>
								</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="btnRegisterNewUnit" id="btnRegisterNewUnit"><i class="fa fa-fw fa-lg fa-save"></i>Register Unit</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="item_unitsTable">
									<thead>
										<tr class="text-center bg-primary text-white">
											<th class="align-middle">No</th>
											<th class="align-middle">Unit ID</th>
											<th class="align-middle">Unit Name</th>
											<th class="align-middle" width="35%">Remarks</th>
											<th class="align-middle">Register Date</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_item_units = 1;
											$getitem_units = mysqli_query($conn, "SELECT * FROM item_units ORDER BY UnitName ASC") or die(mysqli_error($conn));
											while ($item_units_rs = mysqli_fetch_array($getitem_units)) { ?>

												<tr>
                                                    <td class="align-middle text-center"><?php echo $count_item_units; ?></td>
                                                    <td class="align-middle text-center"><?php echo $item_units_rs[1]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $item_units_rs[2]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $item_units_rs[3]; ?></td>
                                                    <td class="align-middle text-center"><?php echo substr($item_units_rs[5], 0, 10); ?></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 btnUpdateUnit" id="<?php echo "Update". $item_units_rs[1]; ?>" data-id="<?php echo $item_units_rs[1]; ?>" data-toggle="modal" data-target="#updateUnitModal"><i class="fa fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteUnit" id="<?php echo "Delete". $item_units_rs[1]; ?>" data-id="<?php echo $item_units_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 btnUpdateUnit" id="<?php echo "Update". $item_units_rs[1]; ?>" data-id="<?php echo $item_units_rs[1]; ?>" data-toggle="modal" data-target="#updateUnitModal"><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 btnDeleteUnit" id="<?php echo "Delete". $item_units_rs[1]; ?>" data-id="<?php echo $item_units_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
												</tr><?php
												$count_item_units++;
											}
										?>
									</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">
			$("#item_unitsTable").DataTable({"Length": 15, "order": []})
			$("#cmbitem_unitstatusUpdateUnit").select2({ tags: false, dropdownParent: $("#updateUnitModal") })
		</script>
	</body>
</html>