<?php
$where 					= " WHERE 1=1 AND parent_id=0 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "module";
$paging_table_name	= $table_name;
$paging_query		= $where;
$page_header = "Modules";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name	$where order by ord LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

#################################  FOR CHANGING THE ORDER OF THE category ########################################
if(isset($_GET['dir']) && $_GET['dir'] =='up'){  
	$order_of_array = array();
	$id		= $_REQUEST['id'];
	$sql = "SELECT id,ord FROM module ORDER BY ord";
	$rs = mysqli_query($conn,$sql);
	while($data=mysqli_fetch_array($rs)){
		array_push($order_of_array,$data['ord']);
	}
	$arry_key = array_search($_GET['order'],$order_of_array);
	$up_key_value = $order_of_array[$arry_key-1];
	
	if($up_key_value > 0){
		mysqli_query($conn,"UPDATE module SET ord='".$_GET['order']."' WHERE ord='".$up_key_value."' ");
		mysqli_query($conn,"UPDATE module SET ord='".$up_key_value."' WHERE id='$id' ");
	}

	header("Location: index?page=view");
}
// when user clicks on down arrow key this line of code will execute and it will change the order
if(isset($_GET['dir'])  && $_GET['dir']=='down'){
	$order_of_array = array();
	$id		= $_REQUEST['id'];
	$sql = "SELECT id,ord FROM module ORDER BY ord";
	$rs = mysqli_query($conn,$sql);
	while($data=mysqli_fetch_array($rs)){
		array_push($order_of_array,$data['ord']);
	}
	$arry_key = array_search($_GET['order'],$order_of_array);
	$down_key_value = $order_of_array[$arry_key+1];

	if(isset($down_key_value) && $down_key_value !=""){
		mysqli_query($conn,"UPDATE module SET ord='".$_GET['order']."' WHERE ord='".$down_key_value."' ");
		mysqli_query($conn,"UPDATE module SET ord='".$down_key_value."' WHERE id='$id' ");
	}

	header("Location: index?page=view");
}
#################################  END  #######################################################
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	$sql=mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	header("location: $file_name?page=view&message=Module deleted sucessfully".$next_page_condition.$paging);
}
#################################  END DELETE    #########################################
?>
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-12">
				<div class="card">
				<!----------------------- Message ------------------------------->
				<? if(isset($_SESSION['info_message'])){ ?>
				  <div class="alert alert-success alert-dismissable">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
				  </div>
				  <? } ?>
				<!----------------------- End Message ------------------------------->
					<div class="card-body">
					<!-- search form -->
					<?php //include_once('category_search_options.inc.php');?>
					<!-- /.search form -->
						<form action="index?page=add" method="post" name="form1" id="form1">
						<div class="card-footer">
						   <button type="submit" name="add" class="btn btn-primary">Add Module</button>
						</div>
						<? if($result>0){?>
						 <table id="example1" class="table table-bordered table-striped">
							<thead>
							  <tr>
								<th>Module Name</th>
								<th>Display Name</th>
								<th>Show</th>
								<th style="text-align:center;">Order</th>
								<th>Edit</th>
								<th>Delete</th>
							  </tr>
							</thead>
							<tbody>
								<?
									while($row =mysqli_fetch_array($sql)){ 
										$active_status_val = $row['display'];
										$active_status_img = ($row['display'] == 1 ) ? "yes.png" : "no.png";
								?>
							  <tr>
								<td><b><?php echo $row['module']; ?></b></td>
								<td><?php echo ucfirst($row['display_name']); ?></td>
								<td id="<?=$row['id'];?>"><img style='cursor:pointer;width:16px;' class='active_status' alt='<?=$active_status_val?>' src="../images/<?=$active_status_img?>"></td>
								<td align="center">
									<a href="index?page=view&dir=up&order=<?=$row['ord']?>&id=<?=$row['id'];?>"><img src="../images/up_arrow.jpg" alt='Decrease Order' width="16" height="13" border="0"/></a>
									<a href="index?page=view&dir=down&order=<?=$row['ord']?>&id=<?=$row['id'];?>"><img src="../images/down_arrow.jpg" alt='Increase Order' width="16" height="13" border="0"/></a>
								</td>
								<td><a href="index?page=add&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit"></i> </a></td>
								<td><a href="index?page=view&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>" onclick=" javascript:return confirm('Are you sure! You want to delete');"><i class="fa fa-fw fa-trash"></i> </a></td>
							  </tr>
								<? 
									$parent_id = $row['id'];
									$indentation = "";
									utility::getSubModuleList($parent_id,$indentation);
									} 
								?>
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=view";
									include("paging/paging_row.inc.php") ?>
								<!-- Paging ends starts here  -->
							</tbody>
						 </table>
						 <? }else{ ?>
							No module found...
						 <? } ?>
					</div>
					<div class="card-footer">
					   <button type="submit" name="add" class="btn btn-primary">Add Module</button>
					</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>
<script type="text/javascript">
$(document).ready(function() {
	$(".active_status").on('click',function() {
		if(confirm("Are you sure, you want to change the show status?")){
		var is_error=true;
		id = $(this).parent().attr('id');
		active_status_val = $(this).attr('alt');
		var form_data ='';
		if($(this).val !=''){	
			var set_image;
			var form_data 	= {'status_val' : active_status_val,'id' : id};
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/module/index?page=ajax_action&action=active_status_val",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result);
					if(resultObj.status == "failure"){
						alert(resultObj.message);		    								
						$(this).focus();
						is_error = true;
						return false;								
					}else{
						is_error = false;
						set_image=resultObj.active_status;
						
					}
				}
			}); 
			if(is_error==false){
				val = (active_status_val == 1) ? 0 : 1;
				change_image = (set_image == 1) ? "../images/yes.png" : "../images/no.png";
				$(this).attr('alt',val)
				$(this).attr("src",change_image);
				$(this).css("cursor:pointer;width:16px;");
			}	
		}
	  }
	});
});
</script>