<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
$document_template_id = $_REQUEST["document_template_id"];
if($document_template_id) {
    $where	.= " AND document_template_id = '$document_template_id' ";
}
###########################################  STRAT FILTER CONITIONS    #####################################
$section 	= isset($_REQUEST['section']) ? $_REQUEST['section'] : "";
if($section) {
    $where	.= " AND section LIKE '%$section%' ";
    $next_page_condition.="&section=$section";
}

###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  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		= "document_template_section";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Section Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY ord ASC LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

#################################   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']."' ");
	$_SESSION['info_message'] = "Section deleted sucessfully";
	header("location: $file_name?page=view_section".$next_page_condition.$paging);
	exit;
}
#################################  END DELETE    #########################################
?>
<section class="content-header">
  <h1>
	<?=$page_header;?>
  </h1>
</section>
<section class="content">
	<div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-primary">
				<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">
				<div class="card-body">
				<!-- search form -->
				<?php include_once('section_search_options.inc.php');?>
				<!-- /.search form -->
					<form action="index?page=add_section" method="post" name="form1" id="form1">
					<div class="card-footer" style="padding-left: 0px;">
					   <button type="submit" name="add" class="btn btn-primary">Add Section</button>
					</div>
					<? if($result>0){?>
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<th>&nbsp;&nbsp;&nbsp;</th>
							<th>Section</th>
							<th>Description</th>
							<th>Edit</th>
							<th>Delete</th>
						  </tr>
						</thead>
						<tbody class="sortable">
							<?
								while($row =mysqli_fetch_array($sql)){ 
							?>
						  <tr id="<?=$row['id'];?>">
							<td>
								<span class="handle">
									<i class="fa fa-ellipsis-v"></i>
									<i class="fa fa-ellipsis-v"></i>
								</span>
							</td>
							<td><?=$row['section'];?></td>
							<td><?=$row['description'];?></td>
							<td><a href="index?page=add_section&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /> </a></td>
							<td><a href="index?page=view_section&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>" 
							onclick=" javascript:return confirm('Are you sure! You want to delete');"><img
							src="../images/del.gif" border="0" /> </a></td>
						  </tr>
							<? } ?>
							<!-- Paging starts here  -->
							<? 
								$next_page_condition .="&page=view_section";
								include("paging/paging_row.inc.php") ?>
							<!-- Paging ends starts here  -->
						</tbody>
					 </table>
					 <? }else{ ?>
						No section found...
					 <? } ?>
				</div>
				<div class="card-footer">
					<input type="hidden" name="document_template_id" value="<?=$document_template_id;?>">
					<button type="submit" name="add" class="btn btn-primary">Add Section</button>
                </div>
				</form>
			</div>
		</div>
	</div>
	</div>
</section>
<style>
	.handle{
		cursor:move;
	}
</style>
<script type="text/javascript">
$(document).ready(function() {
	$(".sortable").sortable({
		handle : '.handle',
		update: function() {
           var order = $(this).sortable('toArray').toString();
		   //alert(order);
		   var form_data   = {'order':order};       
			$.ajax({
				async: true,
				type: "POST",
				url: site_url+"/modules/template_document/index.php?page=ajax_actions&action=set_order",
				data: form_data,
				success: function(r){    
					//alert(r);
				}
			});
		}
	});
});
</script>