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

###########################################  	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		= "dynamic_form_data";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Dynamic Data Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id 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')
{
	$id = $_REQUEST['id'];
	$dynamic_form_id = utility::getTableFieldById($table_name,'dynamic_form_id','id',$id);
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$id."' ");
	$sql = "SELECT * FROM dynamic_form_uploaded_file WHERE dynamic_form_data_id = '".$id."' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$uploaded_file = $row["uploaded_file"];
		$dir = "../dynamic_uploaded_document/";
		if(file_exists($dir.$uploaded_file)){ 
			unlink($dir.$uploaded_file);				
		}
		mysqli_query($conn,"DELETE FROM dynamic_form_uploaded_file WHERE dynamic_form_data_id = '".$id."' ");
	}
	$_SESSION['info_message'] = "Data deleted sucessfully";
	header("location: $file_name?page=dynamic_form_creation_view&dynamic_form_id=$dynamic_form_id".$next_page_condition.$paging);
	exit;
}
#################################  END DELETE    #########################################
?>
<section class="content-header">
  <h1>
	<?=$page_header;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info 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="box">
				<div class="box-body">
				<!-- search form -->
				<?php include_once('dynamic_form_creation_view_search_options.inc.php');?>
				<!-- /.search form -->
					<form action="index?page=dynamic_form_creation_add&dynamic_form_id=<?=$dynamic_form_id;?>" method="post" name="form1" id="form1">
					<div class="box-footer" style="padding-left:0px;">
					   <button type="submit" name="add" class="btn btn-primary">Add Data</button>
					</div>
					<? if($result>0){?>
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<?
							$sql_for_form_element = "SELECT * FROM dynamic_form_element WHERE dynamic_form_id = '$dynamic_form_id' AND show_on_listing = '1' ";
							$rs_for_form_element = mysqli_query($conn,$sql_for_form_element);
							if(mysqli_num_rows($rs_for_form_element) > 0){
								$field_name_array = array();
								while($row_for_form_element = mysqli_fetch_assoc($rs_for_form_element)){
									array_push($field_name_array,$row_for_form_element["field_name"]);
							?>
							<th><? echo ucfirst($row_for_form_element["field_label"]);?></th>
							<? } } ?>
							<th>Edit</th>
							<th>Delete</th>
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
									$form_data = json_decode($row["form_data"], true);
							?>
						  <tr>
							<? foreach($field_name_array as $field_name){ ?>
							<td>
								<?php 
									if(is_array($form_data[$field_name])){
										echo implode(",",$form_data[$field_name]);
									}else{
										echo ucfirst($form_data[$field_name]); 
									}
								?>
							</td>
							<? } ?>
							<td><a href="index?page=dynamic_form_creation_add&action=edit&dynamic_form_id=<?=$row["dynamic_form_id"];?>&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /> </a></td>
							<td><a href="index?page=dynamic_form_creation_view&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=dynamic_form_creation_view";
								include("paging/paging_row.inc.php") ?>
							<!-- Paging ends starts here  -->
						</tbody>
					 </table>
					 <? }else{ ?>
						No data found...
					 <? } ?>
				</div>
				<div class="box-footer">
                   <button type="submit" name="add" class="btn btn-primary">Add Data</button>
                </div>
				</form>
			</div>
		</div>
	</div>
</section>