<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "nominal_codes";
$page_name      = "Add Nominal Code";
$id	 = "";
$query_string = "";

################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $nominal_code 				= $_REQUEST['nominal_code']; 
    $nominal_code_description 	= $_REQUEST['nominal_code_description']; 
	$created_by 				= $_SESSION['admin_id']; // adjust session key as per your auth setup
	$created_on 				= date('Y-m-d H:i:s');
	
    $sql= "INSERT INTO $table_name (nominal_code, nominal_code_description, created_by, created_on) 
		   VALUES('$nominal_code','$nominal_code_description','$created_by','$created_on')";
    mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp added successfully";
    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 Nominal Code";
	$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);

	$nominal_code 				= $row_edit['nominal_code']; 
    $nominal_code_description 	= $row_edit['nominal_code_description']; 
	$created_by 				= $row_edit['created_by'];
	$created_on 				= $row_edit['created_on'];
	$updated_by 				= $row_edit['updated_by'];
	$updated_on 				= $row_edit['updated_on'];
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id							= $_REQUEST['id'];
	$nominal_code 				= $_REQUEST['nominal_code']; 
    $nominal_code_description 	= $_REQUEST['nominal_code_description']; 
	$updated_by 				= $_SESSION['admin_id']; // adjust session key as per your auth setup
	$updated_on 				= date('Y-m-d H:i:s');

	$sql= "UPDATE $table_name SET 
				nominal_code = '$nominal_code',
				nominal_code_description = '$nominal_code_description',
				updated_by = '$updated_by',
				updated_on = '$updated_on'
			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 successfully";
    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 row">
                    <div class="col-lg-4">
                      <label for="nominal_code" class="control-label">Nominal Code<em>*</em></label>
                      <div class="">
                        <input type="text" id="nominal_code" name="nominal_code" value="<?php echo $nominal_code;?>" class="form-control" placeholder="Enter Nominal Code" required>
                      </div>
                    </div>
					<div class="col-lg-4">
                      <label for="nominal_code_description" class="control-label">Nominal Code Description<em>*</em></label>
                      <div class="">
                      <input type="text" class="form-control" id="nominal_code_description" name="nominal_code_description" value="<?php echo $nominal_code_description;?>" placeholder="Enter Description" required>
                      </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">
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
});
</script>