<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "invoice_footer_detais";
$page_name      = "Add Invoice Footer Details";
$id	 = "";
$vat = "";
$query_string = "";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $our_details 	= $_REQUEST['our_details']; 
    $our_accreditations 	= $_REQUEST['our_accreditations']; 
    $bottom_text 	= $_REQUEST['bottom_text']; 
	$created_by				= $_SESSION['user_id'];
	$created_on				= date("Y-m-d H:i:s");
	$updated_by				= $_SESSION['user_id'];
	
    $sql= "INSERT INTO $table_name (our_details,our_accreditations,bottom_text,created_by,created_on) VALUES('$our_details','$our_accreditations','$bottom_text','$created_by','$created_on')";
    mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp added sucessfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

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

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Invoice Footer Details";
	$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);

    $our_details 	= $row_edit['our_details']; 
    $our_accreditations 	= $row_edit['our_accreditations']; 
    $bottom_text 	= $row_edit['bottom_text']; 
}
################################################# END EDIT ################################################# 


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

	$updated_by				= $_SESSION['user_id'];
   
	$sql= "UPDATE $table_name SET our_details = '$our_details',our_accreditations = '$our_accreditations',bottom_text = '$bottom_text', updated_by = '$updated_by' 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", "page=view", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp updated sucessfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1><?=$page_name;?></h1>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card box-primary">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
                    <div class="form-group">
                      <label for="Our Details" class="col-sm-2 control-label">Our Details<em>*</em></label>
                      <div class="col-sm-12">
                       	<textarea rows="6" cols="42" id="our_details" name="our_details" class="textarea_style"><?=$our_details;?></textarea>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Our Details" class="col-sm-2 control-label">Accreditations<em>*</em></label>
                      <div class="col-sm-12">
                       	<textarea rows="6" cols="42" id="our_accreditations" name="our_accreditations" class="textarea_style"><?=$our_accreditations;?></textarea>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Our Details" class="col-sm-2 control-label">Bottom Text<em>*</em></label>
                      <div class="col-sm-12">
                       	<textarea rows="6" cols="42" id="bottom_text" name="bottom_text" class="textarea_style"><?=$bottom_text;?></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $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
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>

<script type="text/javascript">CKEDITOR_BASEPATH = '<?=$siteURL?>/includes/ckeditor/';</script>
<script type="text/javascript" src="<?=$siteURL?>/includes/ckeditor/ckeditor.js"></script>

<script type="text/javascript">

CKEDITOR.replace('our_details',{allowedContent: true});
CKEDITOR.replace('bottom_text',{allowedContent: true});
CKEDITOR.replace('our_accreditations',{allowedContent: true});
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
	$("#form1").on("blur","#county",function(){	
		var county = $(this).val();
		var county = $("#county").val();
		var id = $("#id").val();
		if(county != ""){
		var form_data   = {'county':county,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/county_management/index.php?page=ajax_actions&action=check_county_exist",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This county already in system");
						$("#county_check").show();
						$("#county_check").html("This county already in system.");
						//$("#email").val("");
						$("#county").focus();
					}else{
						$("#county_check").html("");
						$("#county_check").hide();
					}
				}
			});
		}
	});
});
</script>