<?php
	session_start();
	include("library/conn.php");
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php include("library/head.php");?>
        <title>System Menus | <?php echo $_SESSION['systemName']; ?></title>
    </head>
  	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<?php include("library/sidebar.php");?>

    	<main class="app-content">
    	    <div class="app-title">
				<div>
					<h4 class="text-primary"><i class="fa fa-list fa-lg"></i> SYSTEM MENUS</h4>
					<p>System Menus 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="system-menus">System Menus</a></li>
				</ul>
			</div>

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

	  		<div class="row">
        		<div class="col-md-12">
            
          			<div class="tile">
            			<div class="tile-body">
                			<div class="row">
								<div class="form-group col-md-12">
									<h5 class="pb-2"><span><i class="fa fa-list-ul"></i> System Menus List</span></h5>
									<hr />
								</div>
							</div>
							
							<div class="table-responsive" id="displaySystemMenusArea">
							    <?php
							    
							        $getSystemMenus = mysqli_query($conn, "SELECT * FROM main_menus ORDER BY mainMenuRank ASC");
                            		if ($getSystemMenus) {
                            			if (mysqli_num_rows($getSystemMenus) > 0) {
                                            ?><table class="table table-striped table-sm" id="systemMenusTable">
                            					<thead>
                            						<tr class="text-center bg-primary text-light" height="38px">
                            							<th class="align-middle text-center" width="10%">+ New Menu</th>
                            							<th class="align-middle text-center" width="20%">Menus Name</th>
                            							<th class="align-middle text-center">Sub Menus</th>
                            							<th class="align-middle text-center" width="12%">Action</th>
                            						</tr>
                            					</thead>
                                                <tbody class="menu_ranking"><?php
                            				        while ($main_rs = mysqli_fetch_array($getSystemMenus)) { ?>
                            							<tr style="cursor: all-scroll;" menu-id="<?php echo $main_rs[0] ?>">
                            							    
                            								<td class="align-middle text-center"><span id="generateNewMenu" style="cursor: pointer;"><i class="fa fa-plus fa-lg p-2 text-white bg-success rounded"></i></span></td>
                            								
                            								<td class="align-middle text-left">
                            									<i class="<?php echo $main_rs[2]; ?> fa-lg pl-2"></i> <a href="<?php echo $main_rs[3]; ?>"><b><?php echo $main_rs[1]; ?></b></a>
                            								</td>
                            								
                            								<td class="align-middle text-left">
                            								    <?php
                            										$getSubmenus = mysqli_query($conn, "SELECT * FROM sub_menus WHERE mainMenuID = '". $main_rs[0] ."' ORDER BY subMenuRank ASC");
                            										if ($getSubmenus) {
                            											$submenu_num = mysqli_num_rows($getSubmenus);
                            											if ($submenu_num > 0) {
                            											    ?><table class="table table-sm" id="systemSubmenusTable">
                                                                                <thead>
                            														<tr height="35px">
                            															<th width="10%">+Sub</th>
                            															<th>Submenu Name</th>
                            															<th width="15%" class="text-center"><center>Action</center> </th>
                            														</tr>
                            													</thead>
                            													<tbody class="submenu_ranking"><?php
                            														while ($submenu_rs = mysqli_fetch_array($getSubmenus)) {?>
                            															<tr submenu-id="<?php echo $submenu_rs[0] ?>">
                            															    <td class="align-middle text-left"><span id="generateNewSubmenu" style="cursor: pointer;"><i class="fa fa-plus fa-lg p-2 text-white bg-dark rounded"></i></span></td>
                            																<td class="align-middle text-left">
                            																	<i class="<?php echo $submenu_rs[2]; ?> fa-lg"></i> &nbsp;&nbsp;<a href="<?php echo $submenu_rs[3]; ?>"><?php echo $submenu_rs[1]; ?></a>
                            																</td>
                            																<td class="align-center">
                            																    <div class="btn-group col-md-10">
                                                                                                    <button type="button" class="btn btn-success btn-sm">Action</button>
                                                                                                    <button type="button" class="btn btn-success 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 btnUpdateSubmenu" id="<?php echo "Update". $submenu_rs[0]; ?>" data-id="<?php echo $submenu_rs[0]; ?>" data-toggle="modal" data-target="#updateSubmenuModal"><i class="fa fa-fw fa-lg fa-edit"></i> Edit</a>
                                                                                                        <a href="#" class="dropdown-item text-danger btnDeleteSubmenu" id="<?php echo "Delete". $submenu_rs[0]; ?>" data-id="<?php echo $submenu_rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                                                    </div>
                                                                                                </div>
                            																</td>
                            															</tr><?php
                            														} ?>
                            													</tbody>
                            												</table><?php
                            											} else {
                            											    ?><table class="table table-sm">
                            												    <tbody>
                                                                                    <tr>
                            													        <td class="align-middle text-left"></td>
                                                                            			<td class='align-middle' colspan='3'>
                                                                            			    <div class='row no-gutters'>
                                                                                			    <div class='col-md-4 pr-1'><input type='text' class='form-control' name='txtFirstSubmenuNamesList[]' id='txtFirstSubmenuNamesList' placeholder='Enter first submenu name' required /></div>
                                                                                			    <div class='col-md-3 pr-1'><input type='text' class='form-control' name='txtFirstSubmenuIconsList[]' id='txtFirstSubmenuIconsList' placeholder='Enter fa icon' required /></div>
                                                                                			    <div class='col-md-3 pr-1'><input type='text' class='form-control' name='txtFirstSubmenuLinksList[]' id='txtFirstSubmenuLinksList' placeholder='Enter submenu link' required /></div>
                                                                                			    <div class='col-md-2 text-right pr-1'>
                                                                            			            <span class='btn btn-primary btnSaveFirstGeneratedSubmenu'><i class='fa fa-save'></i> Save</span>
                                                                            			        </div>
                                                                                		    </div>
                                                                            		    </td>
                                                                            		</tr>
                                                                            	</tbody>
                                                                            </table><?php
                            											}
                            										} else {
                            											echo mysqli_error($conn);
                            										}
                            									?>
                            									
                            								</td>
                            						
                            								<td class="align-middle text-center">
                            								    <div class="btn-group col-md-10">
                                                                    <button type="button" class="btn btn-dark btn-sm">Action</button>
                                                                    <button type="button" class="btn btn-dark 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 btnUpdateMenu" id="<?php echo "Update". $main_rs[0]; ?>" data-id="<?php echo $main_rs[0]; ?>" data-toggle="modal" data-target="#updateMenuModal"><i class="fa fa-fw fa-lg fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteMenu" id="<?php echo "Delete". $main_rs[0]; ?>" data-id="<?php echo $main_rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                            								</td>
                            					        </tr><?php
                            				        } ?>
                            				   </tbody>
                            		        </table><?php
                            		        
                            			} else {
                            			    
                            			    ?><table class="table table-sm">
                            			        <thead>
                            						<tr class="text-center bg-primary text-light" height="38px">
                            							<th class="align-middle text-center" width="10%">+ New Menu</th>
                            							<th class="align-middle text-center" width="20%">Menus Name</th>
                            							<th class="align-middle text-center">Sub Menus</th>
                            							<th class="align-middle text-center" width="12%">Action</th>
                            						</tr>
                            					</thead>
                            				    <tbody>
                                                    <tr>
                                            			<td class='align-middle text-center'></td>
                                            			<td class='align-middle' colspan='3'>
                                            			    <div class='row no-gutters'>
                                                			    <div class='col-md-3 pr-1'><input type='text' class='form-control' name='txtFirstMenuNamesList[]' id='txtFirstMenuNamesList' placeholder='Enter first menu name' required /></div>
                                                			    <div class='col-md-4 pr-1'><input type='text' class='form-control' name='txtFirstMenuIconsList[]' id='txtFirstMenuIconsList' placeholder='Enter font awesome icon' required /></div>
                                                			    <div class='col-md-3 pr-1'><input type='text' class='form-control' name='txtFirstMenuLinksList[]' id='txtFirstMenuLinksList' placeholder='Enter menu link (URL)' required /></div>
                                            			        <div class='col-md-2 text-center pr-1'>
                                            			            <span class='btn btn-primary btnSaveFirstGeneratedMenu'><i class='fa fa-save'></i> Save Menu</span>
                                            			        </div>
                                            			    </div>
                                                        </td>
                                            		</tr>
                                            	</tbody>
                                            </table><?php
                            
                            			}
                            		} else {
                            			echo mysqli_error($conn);
                            		}
							    
							    ?>
							</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">
		    $("#systemMenusTable, #systemSubmenusTable").DataTable({"pageLength": 100, "order": [], searching: false, paging: false, info: false})
		    $(".menu_ranking").sortable({
                delay: 100, stop: function() {
                    var allMenuIds = new Array()
                    $(".menu_ranking > tr").each(function() { allMenuIds.push($(this).attr("menu-id")); })
                    changeMenuRank(allMenuIds)
                }
            })
            $(".submenu_ranking").sortable({
                delay: 100, stop: function() {
                    var allSubmenuIds = new Array()
                    $(".submenu_ranking > tr").each(function() { allSubmenuIds.push($(this).attr("submenu-id")); })
                    changeSubmenuRank(allSubmenuIds)
                }
            })
		</script>

		
  	</body>
</html>