<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view_section";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "document_template_section";
$page_name      = "Add Section";
$id	 = "";
$vat = "";
$query_string = "";
$document_template_id = $_REQUEST["document_template_id"];
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $document_template_id 	= $_REQUEST['document_template_id']; 
    $section 	= $_REQUEST['section']; 
    $description 	= $_REQUEST['description']; 
	/////////////////// getting maximum order of module //////////////////////////////////
	$sql_sub_cat_ord = "SELECT max(ord) FROM module";
	$rs_sub_cat_ord = mysqli_query($conn,$sql_sub_cat_ord);
	if(mysqli_num_rows($rs_sub_cat_ord) > 0){
		$row_sub_cat_ord = mysqli_fetch_array($rs_sub_cat_ord);
		$max_sub_cat_ord 	= $row_sub_cat_ord['0'];
	}
	$max_sub_ord = $max_sub_cat_ord +1;
	////////////////////////////////////////////////////////////////////////////
	
    $sql= "INSERT INTO $table_name (document_template_id,section,description,ord) VALUES('$document_template_id','$section','$description','$max_sub_ord')";
    mysqli_query($conn,$sql);
	
	$_SESSION['info_message'] = "Template section added sucessfully";
    header("Location: $file_name&document_template_id=$document_template_id");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit County";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

	$document_template_id 	= $row_edit['document_template_id']; 
	$section 	= $row_edit['section']; 
	$description 	= $row_edit['description']; 
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id						= $_REQUEST['id'];
	$document_template_id 	= $_REQUEST['document_template_id'];
	$section 				= $_REQUEST['section'];
	$description 			= $_REQUEST['description'];
	

	$sql= "UPDATE $table_name SET document_template_id = '$document_template_id',section = '$section',description = '$description' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add_section", "page=view_section", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "Template section updated sucessfully";
    header("Location: $file_name&document_template_id=$document_template_id$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<section class="content-header">
  <h1>
	<?=$page_name;?>
  </h1>
</section>
<section class="content">
	<div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add_section&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
					<div class="form-group">
						<div class="" style="color:red;">
							<b>Please use following string between [] as placeholder in template document like [first_name].</b>
							<?
								$client_detail_array = gsi::$clientDetailArray;
								$client_detail_sring = implode(", ",$client_detail_array);
								echo $client_detail_sring;
							?>
						</div>
					</div>
                    <div class="form-group">
                      <label for="Section" class="control-label">Section</label>
                      <div class="">
                        <input type="text" id="section" name="section" value="<?php echo $section;?>" class="form-control" placeholder="Section">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Description" class="control-label">Description</label>
                      <div class="">
						<textarea class="form-control" name="description" id="description" rows="10" placeholder="Description"><?php echo $description;?></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" name="document_template_id" value="<?=$document_template_id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
	</div>
</section>