<?
	$form_action	= "insert";
	$button_value 	= "Add";
	################################################## START INSERT ##################################################
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){
		$form_data = mysqli_real_escape_string(json_encode($_REQUEST));
		$dynamic_form_id = $_REQUEST["dynamic_form_id"];
		mysqli_query($conn,"INSERT INTO dynamic_form_data(form_data,dynamic_form_id) VALUES('$form_data','$dynamic_form_id')");
		$new_inserted_id = mysqli_insert_id($conn);
		
		if(isset($_FILES['document_name']['name']) && $_FILES['document_name']['name'] !=""){
			$path = $_FILES['document_name']['name'];
			$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
			if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx"){
				$digits = 5;
				$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
				$uploaddir = '../dynamic_uploaded_document/';
				$document_name_array = explode(".",$_FILES['document_name']['name']);
				$document_ext = array_pop($document_name_array);
				$doc_name = implode('.', $document_name_array);
				$document_name = $doc_name."_".$random_number.".".$document_ext;
				$uploadfile = $uploaddir . $document_name;
				move_uploaded_file($_FILES['document_name']['tmp_name'], $uploadfile);
				mysqli_query($conn,"INSERT INTO dynamic_form_uploaded_file(dynamic_form_data_id,uploaded_file) VALUES('$new_inserted_id','$document_name')");
			}
		}
		
		$_SESSION['message'] = "Data inserted sucessfully";
		$redirect = "index?page=dynamic_form_creation_view&dynamic_form_id=$dynamic_form_id";
		header("Location: $redirect");
		exit();
	}
	################################################## END INSERT ####################################################
	
	################################################## START EDIT ####################################################
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
		$form_action 	= "update";
		$button_value 	= "Update";
	
		$id = $_REQUEST["id"];
		$dynamic_form_id = $_REQUEST["dynamic_form_id"];
		$sql = "SELECT * FROM dynamic_form_data WHERE id = '$id' ";
		$rs = mysqli_query($conn,$sql);
		$data = mysqli_fetch_assoc($rs);
		$form_data = json_decode($data["form_data"], true);
		
		$uploaded_document_id = utility::getTableFieldById('dynamic_form_uploaded_file','id','dynamic_form_data_id',$form_data['id']);
		$uploaded_document_name = utility::getTableFieldById('dynamic_form_uploaded_file','uploaded_file','dynamic_form_data_id',$form_data['id']);
		
		if(isset($_REQUEST['delete_uploaded_document']) && $_REQUEST['delete_uploaded_document'] == '1'){	
			$dir = "../dynamic_uploaded_document/";
			if(file_exists($dir.$uploaded_document_name)){ 
				unlink($dir.$uploaded_document_name);				
			}
			mysqli_query($conn,"DELETE FROM dynamic_form_uploaded_file WHERE dynamic_form_data_id = '$id' ");
			header("location:index?page=dynamic_form_creation_add&action=edit&dynamic_form_id=$dynamic_form_id&id=$id");
		}
	}
	################################################## END EDIT ######################################################
	
	################################################## START UPDATE ##################################################
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){
		$form_data = mysqli_real_escape_string(json_encode($_REQUEST));
		$id = $_REQUEST["id"];
		$dynamic_form_id = $_REQUEST["dynamic_form_id"];
		mysqli_query($conn,"UPDATE dynamic_form_data SET form_data = '$form_data' WHERE id = '$id' ");
		
		$hidden_document_name = $_REQUEST['hidden_document_name'];
		if(isset($_FILES['document_name']['name']) && $_FILES['document_name']['name'] !=""){
			$path = $_FILES['document_name']['name'];
			$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
			if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx"){
				$digits = 5;
				$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
				$uploaddir = '../dynamic_uploaded_document/';
				$old_document	=	$uploaddir.$hidden_document_name;	
				if(file_exists($old_document)){
					unlink($old_document);
				}
				$document_name_array = explode(".",$_FILES['document_name']['name']);
				$document_ext = array_pop($document_name_array);
				$doc_name = implode('.', $document_name_array);
				$document_name = $doc_name."_".$random_number.".".$document_ext;
				$uploadfile = $uploaddir . $document_name;
				move_uploaded_file($_FILES['document_name']['tmp_name'], $uploadfile);
				
				if(isset($hidden_document_name) && $hidden_document_name != ""){
					mysqli_query($conn,"UPDATE dynamic_form_uploaded_file SET uploaded_file = '$document_name' WHERE dynamic_form_data_id = '$id' ");
				}else{
					mysqli_query($conn,"INSERT INTO dynamic_form_uploaded_file(dynamic_form_data_id,uploaded_file) VALUES('$id','$document_name')");
				}
			}
		}
		
		$_SESSION['message'] = "Data updated sucessfully";
		$redirect = "index?page=dynamic_form_creation_view&dynamic_form_id=$dynamic_form_id";
		header("Location: $redirect");
		exit();
	}
	################################################## END UPDATE ####################################################
	$dynamic_form_id = $_REQUEST["dynamic_form_id"];
	$sql = "SELECT * FROM dynamic_form WHERE id = '$dynamic_form_id' ";
	$rs = mysqli_query($conn,$sql);
	$data = mysqli_fetch_assoc($rs);
	$form_name = $data["form_name"];
?>
<section class="content-header">
  <h1>
	Dynamic Form 
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<!-- general form elements -->
              <div class="box box-primary">
                <form class="form-horizontal" method="post" name="<?=$form_name;?>" id="<?=$form_name;?>" action="index?page=dynamic_form_creation_add&action=<?=$form_action;?>" enctype="multipart/form-data">
                  <div class="box-body">
					<?
						$sql_for_form_element = "SELECT * FROM dynamic_form_element WHERE dynamic_form_id = '$dynamic_form_id' ";
						$rs_for_form_element = mysqli_query($conn,$sql_for_form_element);
						if(mysqli_num_rows($rs_for_form_element) > 0){
							while($row = mysqli_fetch_assoc($rs_for_form_element)){
								$dynamic_form_element_id = $row["id"];
					?>
					<div class="form-group">
						<label for="<?=$row["field_label"];?>" class="col-sm-2 control-label"><?=ucfirst($row["field_label"]);?></label>
						<div class="col-sm-8">
							<? if($row["form_field"] == 1){ ?>
								<input type="text" id="<?=$row["field_name"];?>" name="<?=$row["field_name"];?>" value="<?=$form_data[$row["field_name"]];?>" class="form-control" placeholder="<?=$row["field_label"];?>" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
							<? }else if($row["form_field"] == 2){ ?>
								<select class="form-control" name="<?=$row["field_name"];?>" id="<?=$row["field_name"];?>" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
									<option value="">Select</option>
									<?
										$sql_for_form_element_option = "SELECT * FROM dynamic_form_element_option WHERE dynamic_form_element_id = '$dynamic_form_element_id' ";
										$rs_for_form_element_option = mysqli_query($conn,$sql_for_form_element_option);
										if(mysqli_num_rows($rs_for_form_element_option) > 0){
											while($row_option = mysqli_fetch_assoc($rs_for_form_element_option)){
									?>
									<option value="<?=$row_option["option_value"];?>" <? echo ($form_data[$row["field_name"]] == $row_option["option_value"]) ? "selected" : "";?>><?=$row_option["option_value"];?></option>
									<?  } } ?>
								</select>
							<? }else if($row["form_field"] == 3){ ?>
								<select multiple class="form-control select2" name="<?=$row["field_name"];?>[]" id="<?=$row["field_name"];?>" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
									<?
										$sql_for_form_element_option = "SELECT * FROM dynamic_form_element_option WHERE dynamic_form_element_id = '$dynamic_form_element_id' ";
										$rs_for_form_element_option = mysqli_query($conn,$sql_for_form_element_option);
										if(mysqli_num_rows($rs_for_form_element_option) > 0){
											while($row_option = mysqli_fetch_assoc($rs_for_form_element_option)){
									?>
									<option value="<?=$row_option["option_value"];?>" <? echo (in_array($row_option["option_value"],$form_data[$row["field_name"]]) ? "selected" : "")?>><?=$row_option["option_value"];?></option>
									<?  } } ?>
								</select>
							<? }else if($row["form_field"] == 4){ ?>
								<?
									$sql_for_form_element_option = "SELECT * FROM dynamic_form_element_option WHERE dynamic_form_element_id = '$dynamic_form_element_id' ";
									$rs_for_form_element_option = mysqli_query($conn,$sql_for_form_element_option);
									if(mysqli_num_rows($rs_for_form_element_option) > 0){
										while($row_option = mysqli_fetch_assoc($rs_for_form_element_option)){
								?>
									<?=ucfirst($row_option["option_value"]);?> <input type="radio" name="<?=$row["field_name"];?>" value="<?=$row_option["option_value"];?>" class="flat-red" <? echo ($row["is_required"] == 1) ? "required" : "";?> <? echo ($form_data[$row["field_name"]] == $row_option["option_value"]) ? "checked" : "";?>>
								<? } } ?>
							<? }else if($row["form_field"] == 5){ ?>
								<?
									$sql_for_form_element_option = "SELECT * FROM dynamic_form_element_option WHERE dynamic_form_element_id = '$dynamic_form_element_id' ";
									$rs_for_form_element_option = mysqli_query($conn,$sql_for_form_element_option);
									if(mysqli_num_rows($rs_for_form_element_option) > 0){
										while($row_option = mysqli_fetch_assoc($rs_for_form_element_option)){
								?>
									<?=ucfirst($row_option["option_value"]);?> <input type="checkbox" name="<?=$row["field_name"];?>[]" value="<?=$row_option["option_value"];?>" class="flat-red" <? echo ($row["is_required"] == 1 && mysqli_num_rows($rs_for_form_element_option) == 1) ? "required" : "";?> <? echo in_array($row_option["option_value"],$form_data[$row["field_name"]]) ? "checked" : "";?>>
								<? } } ?>
							<? }else if($row["form_field"] == 6){ ?>
								<textarea class="form-control" rows="3" id="<?=$row["field_name"];?>" name="<?=$row["field_name"];?>" placeholder="<?=$row["field_name"];?>" <? echo ($row["is_required"] == 1) ? "required" : "";?>><?=$form_data[$row["field_name"]];?></textarea>
							<? }else if($row["form_field"] == 7){ ?>
								<input type="text" class="form-control datepicker" id="<?=$row["field_name"];?>" name="<?=$row["field_name"];?>" value="<?=$form_data[$row["field_name"]];?>" placeholder="<?=$row["field_name"];?>" autocomplete="off" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
							<? }else if($row["form_field"] == 8){ ?>
								<div class="bootstrap-timepicker">
									<input type="time" id="<?=$row["field_name"];?>" name="<?=$row["field_name"];?>" class="form-control timepicker" value="<?=$form_data[$row["field_name"]];?>" placeholder="<?=$row["field_name"];?>" autocomplete="off" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
								</div>
							<? }else if($row["form_field"] == 9){ ?>
								<input type="amount" id="<?=$row["field_name"];?>" name="<?=$row["field_name"];?>" class="form-control" value="<?=$form_data[$row["field_name"]];?>" placeholder="<?=$row["field_name"];?>" <? echo ($row["is_required"] == 1) ? "required" : "";?>>
							<? }else if($row["form_field"] == 10){ ?>
								<input type="file" name="document_name" id="document_name" style="float:left;">
								<span style="float:right;">(doc type jpg/jpeg/png/pdf/doc/docx/xls/xlsx)</span>
								<div style="clear:both;"></div>
								<? if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){ ?>
									<input type="hidden" name="hidden_document_name" value="<? echo $uploaded_document_name;?>">
									<? if(isset($uploaded_document_name) && $uploaded_document_name !=""){ ?>
										<br><? echo $uploaded_document_name;?>
										&nbsp;&nbsp;&nbsp;<i class="fa fa-times" style="cursor:pointer;" id="delete_uploaded_document" alt="<?=$uploaded_document_id;?>"></i>
									<? } ?>
								<? } ?>
							<? } ?>
						</div>
					</div>
					<? } } ?>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
					<input type="hidden" name="id" id="id" value="<?=$id;?>">
					<input type="hidden" name="dynamic_form_id" id="dynamic_form_id" value="<?=$dynamic_form_id;?>">
                    <button type="submit" id="add" name="add" value="submit" class="btn btn-primary"><?=$button_value?></button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$("#delete_uploaded_document").click(function(){		
	if(confirm("Are you sure! You want to delete?")){
		var new_url = window.location+'&delete_uploaded_document=1';		
		$(location).attr('href', new_url);
		return false;
	}else{
		return false;
	}
});
</script>