<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "organisation_management";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Add Company";
$organisation_date = date("d-m-Y");
$from = $_REQUEST['from'];
$sector = array();
################################################## START INSERT ##################################################
/* if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string","admin_id","sector"));
    $organisation_name 		= $_REQUEST['company_name']; 
	$common_date			= date("Y-m-d");	
    $organisation_date 		= utility::setFormatedDate($_REQUEST['organisation_date']);
	$enquiry_source			= (isset($_REQUEST['enquiry_source']) && $_REQUEST['enquiry_source'] != '') ? $_REQUEST['enquiry_source'] : '0';
	$notes					= $_REQUEST['notes'];
	$is_organisation		= 1;
	$is_archive 			= (isset($_REQUEST['is_archive']) && $_REQUEST['is_archive'] !="") ? 1 : 0;
	//$organisation_date		= date("Y-m-d");	
	$organisation_by		= $_SESSION['user_id'];
	$updated_by				= $_SESSION['user_id'];
	$lead_consultant		= $_REQUEST['lead_consultant'];
	
	$sql = "organisation_name = '".$organisation_name."', title = '".$title."', first_name = '".$first_name."', last_name = '".$last_name."', phone_no = '".$phone_no."',landline_phone_no = '".$landline_phone_no."', email = '".$email."', password = '".$password."', address1 = '".$address1."', address2 = '".$address2."', address3 = '".$address3."', city = '".$city."', county = '".$county."', postcode = '".$postcode."', common_date = '".$common_date."', organisation_date = '".$organisation_date."', enquiry_source = '".$enquiry_source."', notes = '".$notes."', sector = '".$sector."', is_organisation = '".$is_organisation."', is_archive = '".$is_archive."', organisation_by = '".$organisation_by."', updated_by = '".$updated_by."'";
	$sql=utility::convert_to_crypt_data($sql,$table_name);
	$sql= "INSERT INTO $table_name SET ".$sql;
	mysqli_query($conn,$sql);
	$org_id = mysqli_insert_id($conn);
	
	if(isset($_REQUEST['admin_id']) && $_REQUEST['admin_id'] != ''){	
		foreach($_REQUEST['admin_id'] as $admin_id){
			$sql= "INSERT INTO job_admin_mapping (job_id,admin_id) VALUES('$org_id','$admin_id')";
			mysqli_query($conn,$sql);
		}
	}
	
	$_SESSION['info_message'] = "Organisation 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 Company";
	$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);
	$row_edit = utility::validate_crypt_data($row_edit);

    $company_name		= $row_edit['company_name'];
    $is_archive			= $row_edit['is_archive'];
    $lead_consultant	= $row_edit['lead_consultant'];
	
	
	$admin_id = array();
	$sql_for_job_admin = "SELECT * FROM job_admin_mapping WHERE job_id = $id";
	$result_for_job_admin = mysqli_query($conn,$sql_for_job_admin);
    while($row_for_job_admin = mysqli_fetch_array($result_for_job_admin)){
		array_push($admin_id,$row_for_job_admin["admin_id"]);
	}	
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","admin_id","sector"));
	$id= $_REQUEST['id'];
	$company_name		= $_REQUEST['company_name'];
	
	$updated_by			= $_SESSION['user_id'];
	
	$sql = "company_name = '".$company_name."', updated_by = '".$updated_by."'";
	$sql=utility::convert_to_crypt_data($sql,$table_name);
	$sql= "UPDATE $table_name SET ".$sql." 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;
	
	$_SESSION['info_message'] = "Company updated sucessfully";
	if(isset($from) && $from == "view_company_progress"){
		header("Location: index?page=view_company_progress&from=view_company&id=$id");
		exit();
	}
	
    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 class="col-sm-6">
			<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_company_details"){
				$query_string = str_ireplace("page=add", "page=view_company_details", $query_string);
			}else {
				$query_string = str_ireplace("page=add", "page=view", $query_string);
			}
				
			?>
			<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-success" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-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 ------------------------------->
			<!-- general form elements -->
              <div class="card">
                <form class="" 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="row">
				  <div class="col-lg-12 col-sm-12">
					<div class="form-group"> 
						<label class="control-label" for="company Name">Company Name<em>*</em></label> 
						<div class=""> 
							<input type="text" id="company_name" name="company_name" class="form-control" value="<?php echo $company_name;?>" placeholder="Company Name" required>
						</div> 
					</div>
                  </div>
                  </div><!-- /.row -->
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<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">Update
						<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">
$(function(){
	
	$('#form1').submit(function(){
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});
	///////////////// email verification////////////////
	
});
</script>