<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "supplier_management";
$id	 = "";
$vat = "";
$delivery_lead_times = "";
$query_string = "";
$page_name      = "Add Supplier";
$from = $_REQUEST['from'];
$tax_code = 2;
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string","accreditation_name","accreditations_expirey_date"));
	
	
    $supplier_name				= $_REQUEST['supplier_name'];
	$supplier_code				= $_REQUEST['supplier_code'];
	$delivery_lead_times		= intval($_REQUEST['delivery_lead_times']);
	$address_1					= $_REQUEST['address_1'];
	$address_2					= $_REQUEST['address_2'];
	$address_3					= $_REQUEST['address_3'];
	$supplier_postcode			= $_REQUEST['supplier_postcode'];	
	
	
	$contact_1_title			= $_REQUEST['contact_1_title'];
	$contact_1_first_name		= $_REQUEST['contact_1_first_name'];
	$contact_1_surname			= $_REQUEST['contact_1_surname'];
	$contact_1_email_address	= utility::app_crypt($_REQUEST['contact_1_email_address'],'e');
	$contact_1_phone_no			= utility::app_crypt($_REQUEST['contact_1_phone_no'],'e');
	
	
	
	$contact_2_title		    = $_REQUEST['contact_2_title'];
	$contact_2_first_name		= $_REQUEST['contact_2_first_name'];
	$contact_2_surname			= $_REQUEST['contact_2_surname'];
	$contact_2_email_address	= utility::app_crypt($_REQUEST['contact_2_email_address'],'e');
	$contact_2_phone_no			= utility::app_crypt($_REQUEST['contact_2_phone_no'],'e');
	
	
	$contact_3_title			= $_REQUEST['contact_3_title'];
	$contact_3_first_name		= $_REQUEST['contact_3_first_name'];
	$contact_3_surname			= $_REQUEST['contact_3_surname'];
	$contact_3_email_address	= utility::app_crypt($_REQUEST['contact_3_email_address'],'e');
	$contact_3_phone_no			= utility::app_crypt($_REQUEST['contact_3_phone_no'],'e');
	$tax_code					= $_REQUEST['tax_code'];
	$default_country_code		= $_REQUEST['default_country_code'];
	
	
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];

	$sql = "supplier_name = '".$supplier_name."', supplier_code = '".$supplier_code."', delivery_lead_times = '".$delivery_lead_times."', address_1 = '".$address_1."', address_2 = '".$address_2."', address_3 = '".$address_3."', supplier_postcode = '".$supplier_postcode."', contact_1_title = '".$contact_1_title."', contact_1_first_name = '".$contact_1_first_name."', contact_1_surname = '".$contact_1_surname."', contact_1_email_address = '".$contact_1_email_address."', contact_1_phone_no = '".$contact_1_phone_no."', contact_2_title = '".$contact_2_title."',contact_2_first_name = '".$contact_2_first_name."', contact_2_surname = '".$contact_2_surname."',contact_2_email_address = '".$contact_2_email_address."', contact_2_phone_no = '".$contact_2_phone_no."', contact_3_title = '".$contact_3_title."', contact_3_first_name = '".$contact_3_first_name."', contact_3_surname = '".$contact_3_surname."', contact_3_email_address = '".$contact_3_email_address."',contact_3_phone_no = '".$contact_3_phone_no."',tax_code = '".$tax_code."',default_country_code = '".$default_country_code."',document_upload = '".$document_upload."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

    $sql= "INSERT INTO $table_name SET ".$sql;

	mysqli_query($conn,$sql);
	$supplier_id = mysqli_insert_id($conn);
	
	if(count($_REQUEST['accreditation_name']) > 0){
		foreach($_REQUEST['accreditation_name'] as $key => $accreditation){
			
			if($accreditation!=""){
				$accreditations_expirey_date = utility::setFormatedDate($_REQUEST['accreditations_expirey_date'][$key]);
				$sql_acc= "supplier_id = '".$supplier_id."',accreditation_name = '".$accreditation."',accreditations_expirey_date = '".$accreditations_expirey_date."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";
				$sql_acc= "INSERT INTO supplier_accreditation_mapping SET ".$sql_acc;
				mysqli_query($conn,$sql_acc);
			}
		}
	}
	if(isset($_FILES['document_upload']['name']) && $_FILES['document_upload']['name'] !=""){
		$path = $_FILES['document_upload']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG" || $ext == "pdf"){
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = 'supplier_documents/';
			$old_document	=	$uploaddir.$hidden_document_upload;	
			if(file_exists($old_document)){
				unlink($old_document);
			}
			$document_upload_array = explode(".",$_FILES['document_upload']['name']);
			$docu_ext = array_pop($document_upload_array);
			$doc_name = implode('.', $document_upload_array);
			//$document_upload = $id."_".$document_upload_array[0]."_".$random_number.".".$document_upload_array[1];
			$document_upload = $supplier_id."_".$doc_name."_".$random_number.".".$docu_ext;
			$document_upload = str_replace(" ","_",$document_upload);
			$uploadfile = $uploaddir . $document_upload;
			move_uploaded_file($_FILES['document_upload']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET document_upload = '$document_upload' WHERE id = '$supplier_id' ";
			mysqli_query($conn,$sql);
			
			$_SESSION['info_message'] = "File added successfully";
		}else{
			$document_upload = $hidden_document_upload;
			$_SESSION['message'] = "Please upload only pdf/jpg/jpeg/png";
		}
    }
	

	   if(isset($from) && $from == "add_new_supplier_progress"){
		$_SESSION['info_message'] = "Supplier added successfully";
		 header("Location: index?page=add&hide_top=1&hide_left=1");
		 //$_SESSION['new_sku_number_id'] = "$item_id";
          exit();
        }
	$_SESSION['info_message'] = "Supplier added successfully";
	header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	$supplier_id = utility::app_crypt($_REQUEST['supplier_id'],'d');
		if($_REQUEST['id'] != $supplier_id){
			die("Invaild URL");
		}
	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Supplier";
	$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);
	
    $supplier_name				= $row_edit['supplier_name'];
	$supplier_code				= $row_edit['supplier_code'];
	$delivery_lead_times		= $row_edit['delivery_lead_times'];
	$address_1			        = $row_edit['address_1'];
	$address_2			        = $row_edit['address_2'];
	$address_3			        = $row_edit['address_3'];
	$supplier_postcode			= $row_edit['supplier_postcode'];
	
	
	$contact_1_title			= $row_edit['contact_1_title'];
	$contact_1_first_name		= $row_edit['contact_1_first_name'];
	$contact_1_surname			= $row_edit['contact_1_surname'];
	$contact_1_email_address	= $row_edit['contact_1_email_address'];
	$contact_1_phone_no			= $row_edit['contact_1_phone_no'];
	
	
	$contact_2_title		    = $row_edit['contact_2_title'];
	$contact_2_first_name		= $row_edit['contact_2_first_name'];
	$contact_2_surname			= $row_edit['contact_2_surname'];
	$contact_2_email_address	= $row_edit['contact_2_email_address'];
	$contact_2_phone_no			= $row_edit['contact_2_phone_no'];
	
	
	$contact_3_title			= $row_edit['contact_3_title'];
	$contact_3_first_name		= $row_edit['contact_3_first_name'];
	$contact_3_surname			= $row_edit['contact_3_surname'];
	$contact_3_email_address	= $row_edit['contact_3_email_address'];
	$contact_3_phone_no			= $row_edit['contact_3_phone_no'];
	$document_upload			= $row_edit['document_upload'];	
	$tax_code					= $row_edit['tax_code'];
	$default_country_code		= $row_edit['default_country_code'];
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	//utility::addslashesOnAllInput(array("query_string","accreditation_name","accreditations_expirey_date"));
	//utility::addslashesOnAllInput(array("query_string","admin_id","accreditation_name","accreditations_expirey_date"));
	utility::addslashesOnAllInput(array("query_string","accreditation_name","accreditations_expirey_date","supplier_accreditation_mapping_id"));
	$id  						= $_REQUEST['id'];
	
    $supplier_name				= $_REQUEST['supplier_name'];
	$supplier_code				= $_REQUEST['supplier_code'];
	$delivery_lead_times		= intval($_REQUEST['delivery_lead_times']);
	$address_1					= $_REQUEST['address_1'];
	$address_2					= $_REQUEST['address_2'];
	$address_3					= $_REQUEST['address_3'];
	$supplier_postcode			= $_REQUEST['supplier_postcode'];
	
	
	$contact_1_title			= $_REQUEST['contact_1_title'];
	$contact_1_first_name		= $_REQUEST['contact_1_first_name'];
	$contact_1_surname			= $_REQUEST['contact_1_surname'];
	$contact_1_email_address	= utility::app_crypt($_REQUEST['contact_1_email_address'],'e');
	$contact_1_phone_no			= utility::app_crypt($_REQUEST['contact_1_phone_no'],'e');
	
	
	
	$contact_2_title		    = $_REQUEST['contact_2_title'];
	$contact_2_first_name		= $_REQUEST['contact_2_first_name'];
	$contact_2_surname			= $_REQUEST['contact_2_surname'];
	$contact_2_email_address	= utility::app_crypt($_REQUEST['contact_2_email_address'],'e');
	$contact_2_phone_no			= utility::app_crypt($_REQUEST['contact_2_phone_no'],'e');
	
	
	$contact_3_title			= $_REQUEST['contact_3_title'];
	$contact_3_first_name		= $_REQUEST['contact_3_first_name'];
	$contact_3_surname			= $_REQUEST['contact_3_surname'];
	$contact_3_email_address	= utility::app_crypt($_REQUEST['contact_3_email_address'],'e');
	$contact_3_phone_no			= utility::app_crypt($_REQUEST['contact_3_phone_no'],'e');
	$tax_code					= $_REQUEST['tax_code'];
	$default_country_code		= $_REQUEST['default_country_code'];
	
	
	$hidden_document_upload 	= $_REQUEST['hidden_document_upload'];
	if(isset($_FILES['document_upload']['name']) && $_FILES['document_upload']['name'] !=""){
		$path = $_FILES['document_upload']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG" || $ext == "pdf"){
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = 'supplier_documents/';
			$old_document	=	$uploaddir.$hidden_document_upload;	
			if(file_exists($old_document)){
				unlink($old_document);
			}
			$document_upload_array = explode(".",$_FILES['document_upload']['name']);
			$docu_ext = array_pop($document_upload_array);
			$doc_name = implode('.', $document_upload_array);
			//$document_upload = $id."_".$document_upload_array[0]."_".$random_number.".".$document_upload_array[1];
			$document_upload = $id."_".$doc_name."_".$random_number.".".$docu_ext;
			$document_upload = str_replace(" ","_",$document_upload);
			$uploadfile = $uploaddir . $document_upload;
			move_uploaded_file($_FILES['document_upload']['tmp_name'], $uploadfile);
			
			$_SESSION['info_message'] = "File updated successfully";
		}else{
			$document_upload = $hidden_document_upload;
			$_SESSION['message'] = "Please upload only pdf/jpg/jpeg/png";
		}
    }else{
		$_SESSION['info_message'] = "File updated successfully";
		$document_upload = $hidden_document_upload;
	}
	
	$updated_by		 		    = $_SESSION['user_id'];
	
	$sql = "supplier_name = '".$supplier_name."', supplier_code = '".$supplier_code."', delivery_lead_times = '".$delivery_lead_times."', address_1 = '".$address_1."', address_2 = '".$address_2."', address_3 = '".$address_3."', supplier_postcode = '".$supplier_postcode."', contact_1_title = '".$contact_1_title."', contact_1_first_name = '".$contact_1_first_name."', contact_1_surname = '".$contact_1_surname."', contact_1_email_address = '".$contact_1_email_address."', contact_1_phone_no = '".$contact_1_phone_no."', contact_2_title = '".$contact_2_title."',contact_2_first_name = '".$contact_2_first_name."', contact_2_surname = '".$contact_2_surname."',contact_2_email_address = '".$contact_2_email_address."', contact_2_phone_no = '".$contact_2_phone_no."', contact_3_title = '".$contact_3_title."', contact_3_first_name = '".$contact_3_first_name."', contact_3_surname = '".$contact_3_surname."', contact_3_email_address = '".$contact_3_email_address."',contact_3_phone_no = '".$contact_3_phone_no."',tax_code = '".$tax_code."',default_country_code = '".$default_country_code."',document_upload = '".$document_upload."', updated_by = '".$updated_by."'";
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	// Sync delivery_lead_times to item_management.uom for all items linked to this supplier
	if($delivery_lead_times > 0){
		mysqli_query($conn, "UPDATE item_management SET uom = '".$delivery_lead_times."' WHERE type_2 = '".$id."'");
	}
	

	if(count($_REQUEST['accreditation_name'])>0){	
		foreach($_REQUEST['accreditation_name'] as $key => $accreditation_name){
			$accreditations_expirey_date = utility::setFormatedDate($_REQUEST['accreditations_expirey_date'][$key]);
			$created_on = date("Y-m-d H:i:s");
			$created_by = $_SESSION["user_id"];		
			$supplier_accreditation_mapping_id 			= $_REQUEST['supplier_accreditation_mapping_id'][$key];		
			if(mysqli_num_rows(mysqli_query($conn,"SELECT * FROM supplier_accreditation_mapping WHERE id = '$supplier_accreditation_mapping_id' ")) > 0){
				  $sql1= "UPDATE supplier_accreditation_mapping SET accreditation_name = '$accreditation_name',accreditations_expirey_date = '$accreditations_expirey_date',updated_by = '$updated_by' WHERE id = '$supplier_accreditation_mapping_id' ";
			}else{
				 $sql1= "INSERT INTO supplier_accreditation_mapping (supplier_id,accreditation_name,accreditations_expirey_date,created_by,created_on,updated_by) VALUES('$id','$accreditation_name','$accreditations_expirey_date','$created_by','$created_on','$updated_by')";
			}
			mysqli_query($conn,$sql1);
		}
	}
	
	
	$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;
	
	$supplier_id = urlencode(utility::app_crypt($id,'e'));
	
	$_SESSION['info_message'] = "Supplier updated successfully";
	if(isset($from) && $from == "view_supplier_progress"){
		header("Location: index?page=view_supplier_progress&from=view_supplier&supplier_id=$supplier_id&id=$id");
		exit();
	}
	
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}

fieldset {
    border-top: 4px solid #dedede; 
    border-right: 1px solid #dedede;
    border-bottom: 1px solid #dedede; 
    border-left: 1px solid #dedede; 
    padding: 15px;
    margin-bottom: 10px;
}

legend {
    width: auto;
    color: #a423a4;
}

</style>
<!---------------------------------- cloning start for Accreditation --------------------------------------------->
	
<div class="col-sm-12 col-lg-12 col-12" id="accreditations_div_to_clone" style="display:none;">
		<div class="row">
			<div class="col-sm-5"> 
				 <label class="control-label" for="Accreditation Name">&nbsp;<em></em></label> 
				 <input type="text" class="form-control" id="accreditation_name" name="accreditation_name[]" value="" placeholder="Accreditation Name" autocomplete="off" required>  
			</div>
			<div class="col-sm-5"> 
				 <label class="control-label" for="Expiry Date">&nbsp;<em></em></label> 
				 <input type="text" class="form-control datepicker accreditations_expirey_date" id="accreditations_expirey_date" name="accreditations_expirey_date[]" value="" placeholder="DD-MM-YYYY" autocomplete="off" required>  
			</div>
			<div class="col-sm-2" style="margin-top:35px;"> 
				<div class="custom-trash ">
					<label class="control-label"></label> 
					<span class="fa fa-trash delete_new_accreditation" alt="" style="color: red;"></span>
				</div>
			</div>
		</div>
	</div>	
<!----------------------------------- Cloning end for Accreditation  --------------------------------------------->

<?if($_REQUEST['from'] !='add_new_supplier_progress'){?>
<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_supplier_progress"){
				$query_string = str_ireplace("page=add", "page=view_supplier_progress", $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-primary" 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>
			  <script type="text/javascript">
				$(document).ready(function(){
					setTimeout(function(){
						window.parent.jQuery("#myModal .close_button2").click();
						window.location.href = 'index?page=view_quotation_progress';
					},500);
				});
            </script>
			<?} ?>
			<!----------------------- 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-section mt-2">
							<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Details</h4>
							<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
							  <div class="row">
								<div class="form-group col-lg-4">
								  <label class="control-label" for="Supplier Name">Supplier Name<em>*</em></label>
								  <div class="">
									<input type="text" id="supplier_name" name="supplier_name" value="<?php echo $supplier_name;?>" class="form-control" placeholder="Supplier Name" required>
								  </div>
								</div>
								<div class="form-group col-lg-4">
								  <label class="control-label" for="Supplier Code">Supplier Code<em>*</em></label>
								  <div class="">
									<input type="text" id="supplier_code" name="supplier_code" value="<?php echo $supplier_code;?>" class="form-control" placeholder="Supplier Code" required>
								  </div>
								</div>
								<div class="form-group col-lg-4">
								  <label class="control-label" for="delivery_lead_times">Delivery Lead Times (Calendar Days)</label>
								  <div class="">
									<input type="number" id="delivery_lead_times" name="delivery_lead_times" value="<?php echo $delivery_lead_times;?>" class="form-control" placeholder="Number of days" min="0">
								  </div>
								</div>
							  </div>	
							  <div class="row">
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Address 1">Address 1<em>*</em></label>
								  <div class="">
									<input type="text" id="address_1" name="address_1" value="<?php echo $address_1;?>" class="form-control" placeholder="Address 1" required>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Address 2">Address 2<em>*</em></label>
								  <div class="">
									<input type="text" id="address_2" name="address_2" value="<?php echo $address_2;?>" class="form-control" placeholder="Address 2" required>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Address 3">Address 3<em>*</em></label>
								  <div class="">
									<input type="text" id="address_3" name="address_3" value="<?php echo $address_3;?>" class="form-control" placeholder="Address 3" required>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Postcode">Postcode<em></em></label>
								  <div class="">
									<input type="text" id="supplier_postcode" name="supplier_postcode" value="<?php echo $supplier_postcode;?>" class="form-control" placeholder="Postcode">
								  </div>
								</div>
								
							  </div>
							</div>
							<div class="form-section mt-2">
							<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Contacts</h4>
							<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">  
							  <div class="row">
								<div class="form-group col-lg-1">
								  <label class="control-label" for="Title">Title<em></em></label>
								  <div class="">
									<? echo Keycode::getKeycodeListValue("contact_1_title", $contact_1_title, $width = "100%", "Please Select", "id", "code", "title","","form-control",false,""); ?>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="First Name">First Name<em></em></label>
								  <div class="">
									<input type="text" id="contact_1_first_name" name="contact_1_first_name" value="<?php echo $contact_1_first_name;?>" class="form-control" placeholder="First Name">
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Surname">Surname<em></em></label>
								  <div class="">
									<input type="text" id="contact_1_surname" name="contact_1_surname" value="<?php echo $contact_1_surname;?>" class="form-control" placeholder="Surname" >
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Email address">Email address<em></em></label>
								  <div class="">
									<input type="text" id="contact_1_email_address" name="contact_1_email_address" value="<?php echo $contact_1_email_address;?>" class="form-control" placeholder="Email address">
									<span class="invalid-feedback" id="email_validate_c1">Invalid email format. Must contain '@'</span>
								  </div>
								</div>
								<div class="form-group col-lg-2">
								  <label class="control-label" for="Phone No">Phone No.<em></em></label>
								  <div class="">
									<input type="text" id="contact_1_phone_no" name="contact_1_phone_no" value="<?php echo $contact_1_phone_no;?>" class="form-control" placeholder="Phone No." pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')" >
									
								  </div>
								</div>
							  </div>
							</div>
							<div class="form-section mt-2">
							<h4 class="hide" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Contact 2</h4>
							  <div class="row">
								<div class="form-group col-lg-1">
								  <label class="control-label" for="Title">Title<em></em></label>
								  <div class="">
									<?php echo Keycode::getKeycodeListValue("contact_2_title", $contact_2_title, $width = "100%", "Please Select", "id", "code", "title", "", "form-control", false, ""); ?>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="First Name">First Name<em></em></label>
								  <div class="">
									<input type="text" id="contact_2_first_name" name="contact_2_first_name" value="<?php echo $contact_2_first_name; ?>" class="form-control" placeholder="First Name">
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Surname">Surname<em></em></label>
								  <div class="">
									<input type="text" id="contact_2_surname" name="contact_2_surname" value="<?php echo $contact_2_surname; ?>" class="form-control" placeholder="Surname">
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Email address">Email address<em></em></label>
								  <div class="">
									<input type="text" id="contact_2_email_address" name="contact_2_email_address" value="<?php echo $contact_2_email_address; ?>" class="form-control" placeholder="Email address">
									<span class="invalid-feedback" id="email_validate_c2">Invalid email format. Must contain '@'</span>
								  </div>
								</div>
								<div class="form-group col-lg-2">
								  <label class="control-label" for="Phone No">Phone No.<em></em></label>
								  <div class="">
									<input type="text" id="contact_2_phone_no" name="contact_2_phone_no" value="<?php echo $contact_2_phone_no; ?>" class="form-control" pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')" placeholder="Phone No.">
								  </div>
								</div>
							  </div>
							  
							</div>
							<div class="form-section mt-2">
							<h4 class="hide" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Contact 3</h4>
							  <div class="row">
								<div class="form-group col-lg-1">
								  <label class="control-label" for="Title">Title<em></em></label>
								  <div class="">
									<?php echo Keycode::getKeycodeListValue("contact_3_title", $contact_3_title, $width = "100%", "Please Select", "id", "code", "title", "", "form-control", false, ""); ?>
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="First Name">First Name<em></em></label>
								  <div class="">
									<input type="text" id="contact_3_first_name" name="contact_3_first_name" value="<?php echo $contact_3_first_name; ?>" class="form-control" placeholder="First Name">
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Surname">Surname<em></em></label>
								  <div class="">
									<input type="text" id="contact_3_surname" name="contact_3_surname" value="<?php echo $contact_3_surname; ?>" class="form-control" placeholder="Surname">
								  </div>
								</div>
								<div class="form-group col-lg-3">
								  <label class="control-label" for="Email address">Email address<em></em></label>
								  <div class="">
									<input type="text" id="contact_3_email_address" name="contact_3_email_address" value="<?php echo $contact_3_email_address; ?>" class="form-control" placeholder="Email address">
									<span class="invalid-feedback" id="email_validate_c3">Invalid email format. Must contain '@'</span>
								  </div>
								</div>
								<div class="form-group col-lg-2">
								  <label class="control-label" for="Phone No">Phone No.<em></em></label>
								  <div class="">
									<input type="text" id="contact_3_phone_no" name="contact_3_phone_no" value="<?php echo $contact_3_phone_no; ?>" class="form-control"  placeholder="Phone No." pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')">
								  </div>
								</div>
							  </div>
							 
							</div>
							<div class="form-section mt-2">
							<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Finance</h4>
							<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
							<div class="row">
							<div class="form-group col-lg-6">
								<label class="control-label" for="Tax Code">Tax Code<em>*</em></label>
								<div class="">
									<!--<input type="text" id="tax_code" name="tax_code" value="<?php echo $tax_code;?>" class="form-control" placeholder="Tax Code" required>-->
									<? $gsi->getTaxCodeList("tax_code",$tax_code,"100%",false,"form-control","required"); ?>
								</div>
							</div>
							<div class="form-group col-lg-6">
								<label class="control-label" for="Default Country Code">Default Country Code<em>*</em></label>
								<div class="">
									<? $gsi->getCountryCodeList("default_country_code",$default_country_code,"100%",false,"form-control select2","required"); ?>
								</div>
							</div>
							</div>
							</div>
							<div class="form-section mt-2">
							<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Accreditations</h4>
							<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
							<? 
							
								$accreditations_query = "SELECT * FROM supplier_accreditation_mapping WHERE supplier_id='".$id."' ";
								$supplier_accreditation_sql=mysqli_query($conn,$accreditations_query);
								$supplier_accreditation_result=mysqli_num_rows($supplier_accreditation_sql);
								if($supplier_accreditation_result>0){
									$j = 0;
									while($mapp_row = mysqli_fetch_array($supplier_accreditation_sql)){
							?>
							<div class="col-sm-12 col-lg-12 col-12 initial_row_for_accreditation" id="new_accreditation<?=$mapp_row['id'];?>" data-personal_count="<?=$mapp_row['id'];?>">
							<div class="row">
							<div class="col-sm-5"> 
								 <label class="control-label" for="Accreditation Name">Accreditation Name<em></em></label> 
								 <input type="text" id="accreditation_name" name="accreditation_name[<?=$j;?>]" class="form-control accreditation_name" value="<?php echo $mapp_row['accreditation_name'];?>" placeholder="Accreditation Name" >
							</div>
							<div class="col-sm-5"> 
								 <label class="control-label" for="Expiry Date">Expiry Date<em></em></label> 
								 <input type="text" class="form-control datepicker accreditations_expirey_date" id="accreditations_expirey_date" name="accreditations_expirey_date[<?=$j;?>]" value="<?php echo (isset($mapp_row['accreditations_expirey_date']) && ($mapp_row['accreditations_expirey_date']) !="") ? utility::getFormatedDate($mapp_row['accreditations_expirey_date']) : "";?>" placeholder="DD-MM-YYYY" autocomplete="off" >  
							</div>
							<div class="col-sm-2" style="margin-top:35px;"> 
								<div class="custom-trash">
									<label class="control-label"></label>
									<input type="hidden" name="supplier_accreditation_mapping_id[<?=$j;?>]" value="<?php echo $mapp_row['id'];?>">
									<?php if ($j != 0) { ?>
									<span class="fa fa-trash delete_new_accreditation" style="cursor:pointer; color: red;"  alt="<?php echo $mapp_row['id'];?>"></span>
									<?php } ?>
									<i class="fas fa-sync-alt fa-spin" id="load_new_accreditation_<?=$mapp_row['id'];?>" style="font-size: 20px;display:none;"></i>
									<i class="fa fa-check" id="status_update_personal_done_<?=$mapp_row['id'];?>" style="color:#bad23a;font-size: 20px;display:none;"></i>
								</div>
							</div>
						</div>
					</div>
					<?
					$j++;
					}}else{?>
					<div class="col-sm-12 col-lg-12 col-12 initial_row_for_accreditation" id="new_accreditation0" data-personal_count="0">
						<div class="row">
						    <div class="col-sm-5"> 
								 <label class="control-label" for="Accreditation Name">Accreditation Name<em></em></label> 
								 <input type="text" class="form-control" id="accreditation_name" name="accreditation_name[]" value="" placeholder="Accreditation Name" autocomplete="off">  
							</div>
							<div class="col-sm-5"> 
								 <label class="control-label" for="Expiry Date">Expiry Date<em></em></label> 
								 <input type="text" class="form-control datepicker accreditations_expirey_date" id="accreditations_expirey_date" name="accreditations_expirey_date[]" value="" placeholder="DD-MM-YYYY" autocomplete="off">  
							</div>
						</div>
					</div>
					<?}?>
					<div class="form-group col-sm-12 accreditations_div_to_append">	
						<label class="add_one_more_accreditation" style="margin-left:0%;cursor:pointer;color: #a423a4; "><span class="fa fa-plus"></span>Add More</label>
					</div>
							</div>
							<div class="form-section mt-2">
							<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Files</h4>
							<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
							<div class="form-group">
							     <label for="File" class="control-label">File<em></em></label><br/>
									<span style="font-weight:bold;color:#ff0000;">(Please upload only pdf/jpg/jpeg/png)</span><br/>
									<input type="file" name="document_upload" id="document_upload" accept=".pdf,.jpg,.jpeg,.png"/>
									<div style="">
									<?
									$dir = "supplier_documents/";
									if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){
									 if (is_file($dir.$document_upload)) {
											//$image_path ='<img style="width:100px;border:1px solid #777" src="../'.$dir.$document_upload.'">';
											//<br><a id="delete_image" href="">Delete</a>
											$ext = pathinfo($document_upload, PATHINFO_EXTENSION);
											if($ext == "jpg" || $ext == "jpeg" || $ext == "png"){
												$image_path ='<a href="../'.$dir.$document_upload.'" target="_blank"><img class="primary text-primary" style="width:100px;border:1px solid #777" src="../'.$dir.$document_upload.'"></a>';
											}else if($ext == "pdf"){
												$image_path ='<a href="../'.$dir.$document_upload.'" target="_blank"><span class="primary text-primary" title="'.$document_upload.'">'.utility::ellipsis($document_upload,20).'</span></a>';
											}
										}else{
											$image_path ='<img style="width:100px;border:1px solid #777" src="../'.$dir.'no-image.gif">';	
										}
										echo $image_path;
									}?>
									<input type="hidden" name="hidden_document_upload" value="<? echo $document_upload;?>">
									</div>
							</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">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">

$(function() {
    // Handle form submission
    $('#form1').submit(function() {
        // Disable the submit button and show the loader
        $("#submit").attr("disabled", true);
        $('#page_load_div').show();
    });
    
   	$('#contact_1_email_address').blur(function() {
    var email = $(this).val();
    var errorSpan = $('#email_validate_c1');
		if (email === '') {
			errorSpan.hide(); 
		} else if (email.includes('@')) {
			errorSpan.hide(); 
		} else {
			errorSpan.show(); 
		}
	});
	
	$('#contact_2_email_address').blur(function() {
        var email = $(this).val();
        var errorSpan = $('#email_validate_c2');
        if (email === '') {
			errorSpan.hide(); 
		} else if (email.includes('@')) {
			errorSpan.hide(); 
		} else {
			errorSpan.show(); 
		}
    });
	
	$('#contact_3_email_address').blur(function() {
        var email = $(this).val();
        var errorSpan = $('#email_validate_c3');
        if (email === '') {
			errorSpan.hide(); 
		} else if (email.includes('@')) {
			errorSpan.hide(); 
		} else {
			errorSpan.show(); 
		}
	});
	$('#contact_1_phone_no').blur(function() {
		var phone = $(this).val();
		var errorSpan = $('#phone_validate'); 
		var phoneRegex = /^[\d\s]+$/; 
		if (phone === '') {
			errorSpan.hide();
		} else if (phoneRegex.test(phone)) {
			errorSpan.hide(); 
		} else {
			errorSpan.show(); 
		}
	});
	
	$('#contact_2_phone_no').blur(function() {
		var phone = $(this).val();
		var errorSpan = $('#phone_validate_c2');
		var phoneRegex = /^[\d\s]+$/; 
		if (phone === '') {
			errorSpan.hide(); 
		} else if (phoneRegex.test(phone)) {
			errorSpan.hide(); 
		} else {
			errorSpan.show();
		}
    });
	
	$('#contact_3_phone_no').blur(function() {
		var phone = $(this).val();
		var errorSpan = $('#phone_validate_c3'); 
		var phoneRegex = /^[\d\s]+$/; 
		if (phone === '') {
			errorSpan.hide(); 
		} else if (phoneRegex.test(phone)) {
			errorSpan.hide(); 
		} else {
			errorSpan.show(); 
		}
    });
	
	
	$('.add_one_more_accreditation').click(function() {		
		$('#accreditations_div_to_clone').show();		
		var div_count = $('.initial_row_for_accreditation').last().attr("data-personal_count");
		div_count++;
		var cloned_div = $('#accreditations_div_to_clone').clone().addClass("initial_row_for_accreditation").attr("id","new_accreditation"+div_count).attr("data-personal_count",div_count);		
		$('.accreditations_div_to_append').before(cloned_div);	
		$('#new_accreditation'+div_count).children().find(".datepicker").datepicker({
			changeMonth: true,
			changeYear: true,
			todayHighlight:true,
			format: "dd-mm-yyyy",
			autoclose: true
		});	
		$('#accreditations_div_to_clone').hide();			
	});
	
	
		$("#form1").on("click", ".delete_new_accreditation",function() {
	    if(confirm('Are you sure you want to delete?')){		
			if($(this).attr("alt") != ""){
				var supplier_accreditation_mapping_id = $(this).attr("alt");
				$('#load_new_accreditation_'+supplier_accreditation_mapping_id).show();
				var form_data   = {'supplier_accreditation_mapping_id':supplier_accreditation_mapping_id};      
                //alert(supplier_accreditation_mapping_id);				
				$.ajax({
					async: true,
					type: "POST",
					//url: site_url+"/modules/supplier_management/index.php?page=ajax_actions&action=delete_accreditations_mapping",
					url: site_url+"/supplier_management/index?page=ajax_actions&action=delete_accreditations_mapping",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#status_update_personal_done_'+supplier_accreditation_mapping_id).show();
							$('#load_new_accreditation_'+supplier_accreditation_mapping_id).hide();
							
							setTimeout(function() {
								$('#status_update_personal_done_'+supplier_accreditation_mapping_id).hide();
								$('#new_accreditation'+supplier_accreditation_mapping_id).remove();
								//location.reload();
							},1000);
						}
					}
				});
			}else{
				$(this).parent().parent().parent().parent().remove();
			}
		}
	});
	
	
	
});
</script>