<div class="container-fluid" style="width: 1250px;">
	
	<section style="padding-top: 1rem; padding-bottom: 1rem;">
		<div class="row float-right">
			<button type="button" class="btn btn-info" onclick="history.go(-1)"><i class="fas fa-backward"></i><br>Back</button>
			<button type="button" class="btn btn-success" onclick="editMainDetails();"><i class="fa fa-save"></i><br>Save</button>
			<button type="button" class="btn btn-danger" onclick="history.go(-1)"><i class="fas fa-ban"></i><br>Cancel</button>
		</div>
	</section>


	<div class="jumbotron" style="background-color: white;">
		<div class="row">
			<div class="col text-center">
				<img class="image-responsive" src="<?php echo base_url('assets/images/logo/pc.png'); ?>" alt="" style="height: 150px; width: 150px;">
			</div>
			<div class="col-9">
				<h1 class="display-6">Personal Details</h1>
				<hr class="my-4">
			</div>
		</div>

		<div class="row">
			<div class="col-12">
				<form id="mainInfo">
					<div class="form-row">
						<div class="col">

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Title</label>
									<select type="text" class="form-control form-control-sm" id="title_id">
										<option value="-1">Please Select</option>
										<?php foreach ($titles as $key => $title): ?>
											<option value="<?php echo $title->id;?>" <?php echo ($rsStaff['title_id'] == $title->id)?'selected':''; ?> ><?php echo $title->title;?></option>
										<?php endforeach ?>
									</select>
								</div>
							</div>

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm"> Name</label>
									<input type="text" id="name_id" data-id="" class="form-control form-control-sm" value="<?php echo $rsStaff['name']; ?>" >
								</div>
							</div>

							<div class="form-row mb-2">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Gender</label><br>
									<div class="row">									
										<div class="col">
											<div class="custom-control custom-radio custom-control-inline">
												<input type="radio" id="customRadioInline5" name="gender" class="custom-control-input" value="1" <?php echo ($rsStaff['gender'] == 'Male')?'checked':''; ?> >
												<label class="custom-control-label" for="customRadioInline5">Male</label>	
											</div>
										</div>
										<div class="col">
											<div class="custom-control custom-radio custom-control-inline">
												<input type="radio" id="customRadioInline6" name="gender" class="custom-control-input" value="2" <?php echo ($rsStaff['gender'] == 'Female')?'checked':''; ?>>
												<label class="custom-control-label" for="customRadioInline6">Female</label>
											</div>
										</div>										
									</div>	
																								
								</div>							
							</div>

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">NRIC/Passport No</label>
									<input type="text" id="nric_id" class="form-control form-control-sm" value="<?php echo $rsStaff['ic_no'] ?>">
								</div>
							</div>

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Passport Expired Date</label>
									<?php
										$tmp = strtotime($rsStaff['passport_expired_date']);
										if($tmp != 0){
											$date = DateTime::createFromFormat('Y-m-d', $rsStaff['passport_expired_date']);
											$passExp = $date->format('d-m-Y');
										}
									?>	
									<input type="text" id="passport_expired_id" class="form-control form-control-sm datepicker" value="<?php echo ($tmp != 0)?$passExp:''; ?>">	
								</div>	
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Working Permit Expired Date</label>
									<?php
										$tmp = strtotime($rsStaff['passport_expired_date']);
										if($tmp != 0){
											$date = DateTime::createFromFormat('Y-m-d', $rsStaff['passport_working_expired_date']);
											$passWExp = $date->format('d-m-Y');
										}

									?>
									<input type="text" id="working_expired_id" class="form-control form-control-sm datepicker" value="<?php echo ($tmp != 0)?$passWExp:'';?>">	
								</div>						
							</div>

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Place of Issue</label>
									<select id="issue_country_id" class="form-control form-control-sm">
										<?php foreach ($countries as $key => $country): ?>
											<option value="<?php echo $country->id; ?>" <?php echo ($country->id == $rsStaff['issue'])?'selected':''; ?> ><?php echo $country->country;?></option>
										<?php endforeach ?>
									</select>
								</div>
							</div>

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Birthdate</label>
									<?php
									$tmp = strtotime($rsStaff['birth_date']);
									if($tmp == 0){
										$rsStaff['birth_date'] = date('Y-m-d');
									}
										$date = DateTime::createFromFormat('Y-m-d', $rsStaff['birth_date']);
									?>
									
									<input type="text" id="birthdate_id" class="form-control form-control-sm datepicker" value="<?php echo $date->format('d-m-Y'); ?>">
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Birthplace</label>
									<input type="text" id="birthplace_id" class="form-control form-control-sm" value="<?php echo $rsStaff['birth_place']; ?>">
								</div>
							</div>

							<div class="form-row">
								<div class="col">
									<label class="col-form-label-sm">Hobby</label>
									<input type="text" class="form-control form-control-sm" id="staff_hobby" value="<?php echo $rsStaff['hobby']; ?>">
								</div>
							</div>
						</div>
						<div class="col">
							<label for="inputEmail3" class="col-form-label-sm">Address</label>
							<input type="hidden" id="add_id" value="<?php echo (!empty($addresses[0]))?$addresses[0]->id:''; ?>">
							<input type="text" id="address_id" class="form-control form-control-sm" value="<?php echo (!empty($addresses[0]))?$addresses[0]->address:''; ?>">
							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Country</label>
									<select class="form-control form-control-sm" id="country_id" onchange="getcountrydata()">
										<option value="-1">Please Select</option>
										<?php foreach ($countries as $key => $country): ?>
											<?php
												if(!empty($addresses[0])){
													if($addresses[0]->country_id == $country->id){
														?>
														<option value="<?php echo $country->id; ?>" selected ><?php echo $country->country;?></option>
														<?php
													}else{
														?>
														<option value="<?php echo $country->id; ?>" ><?php echo $country->country;?></option>
														<?php
													}
												}else{
													?>
														<option value="<?php echo $country->id; ?>" ><?php echo $country->country;?></option>
													<?php
												}
											?>
										<?php endforeach ?>
									</select>
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">State</label>
									<select id="state_id" class="form-control form-control-sm" disabled>
										<option value="-1">Please select</option>
									</select>
								</div>		
							</div>
							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Postcode</label>
									<input type="text" id="postcode_id" class="form-control form-control-sm" value="<?php echo (!empty($addresses[0]))?$addresses[0]->postcode:''; ?>">
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">City</label>
									<input type="text" class="form-control form-control-sm" id="city_id" value="<?php echo (!empty($addresses[0]))?$addresses[0]->city:''; ?>">
								</div>
							</div>						
							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Nationality</label>
								</div>
							</div>
							<div class="form-row">
								<div class="col">
									<select class="form-control form-control-sm" id="nationality_id">
											<option value="-1">Please Select</option>
										<?php foreach ($countries as $key => $country): ?>
											<option value="<?php echo $country->id; ?>" <?php echo ($country->id == $rsStaff['citizen_id'])?'selected':''; ?> ><?php echo $country->country;?></option>
										<?php endforeach ?>
									</select>
								</div>
							</div>
							
							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Religion</label>
									<select type="text" id="religion_id" class="form-control form-control-sm">
										<option value="-1">please select</option>
										<?php foreach ($religions as $key => $religion): ?>
											<option value="<?php echo $religion->id; ?>" <?php echo ($rsStaff['religion_id'] == $religion->id)?'selected':''; ?>><?php echo $religion->name; ?></option>
										<?php endforeach ?>
									</select>
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Race</label>
									<select type="text" id="race_id" class="form-control form-control-sm">
										<option value="-1">Please select</option>
										<?php foreach ($races as $key => $race): ?>
											<option value="<?php echo $race->id;?>" <?php echo ($rsStaff['race_id'] == $race->id)?'selected':''; ?>><?php echo $race->race;?></option>
										<?php endforeach ?>
									</select>
								</div>
							</div>
							<label for="staticEmail" class="col-form-label-sm">Residential Status</label>
							<select type="text" id="residential_id" class="form-control form-control-sm">
								<option value="-1">Please select</option>
								<option value="0" <?php echo ($rsStaff['residential_id'] == 0)?'selected':''; ?> >Resident</option>
								<option value="1" <?php echo ($rsStaff['residential_id'] == 1)?'selected':''; ?> >Non Resident</option>
							</select>
							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Marital Status</label>
									<select type="text" id="marital_id" class="form-control form-control-sm">
										<option value="-1">Please select</option>
										<option value="0"  <?php echo ($rsStaff['marital_status'] == 0)?'selected':''; ?> >Single</option>
										<option value="1"  <?php echo ($rsStaff['marital_status'] == 1)?'selected':''; ?> >Married</option>
										<option value="2"  <?php echo ($rsStaff['marital_status'] == 2)?'selected':''; ?> >Divorce</option>
									</select>
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Married Date</label>
									<?php
										$tmp = strtotime($rsStaff['married_date']);
										if($tmp == 0){
											$married_date = '';
										}else{
											$date = DateTime::createFromFormat('Y-m-d', $rsStaff['married_date']);
											$married_date = $date->format('d-m-Y');
										}
										
									?>
									<input id="married_date_id" type="text" class="form-control form-control-sm datepicker" value="<?php echo($rsStaff['marital_status'] == 1 && $tmp != 0)?$married_date:'';?> ">	
								</div>
							</div>	

							<div class="form-row">
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">SALIHIN Email</label>
									<input id="staff_email_id" type="text" class="form-control form-control-sm" value="<?php echo $rsStaff['email_secondary']?>" >	
								</div>
								<div class="col">
									<label for="inputEmail3" class="col-form-label-sm">Personal Email</label>
									<input id="staff_personal_email_id" type="text" class="form-control form-control-sm" value="<?php echo $rsStaff['email']?>" disabled>	
								</div>
							</div>

						</div>
					</div>					
				</form>
			</div>
		</div>

	</div>
</div>
<script>
	$(function(){
		getcountrydata();
	});

	function editMainDetails()
	{	
		var postData = new FormData();
		postData.append('token','<?php echo $_SESSION['token'];?>');
		postData.append('user_id','<?php echo $this->uri->segment(4); ?>');
		postData.append('title',$('#title_id').val());
		postData.append('name',$('#name_id').val());
		postData.append('gender',$('input[name="gender"]:checked').val());
		postData.append('nric',$('#nric_id').val());
		postData.append('passport_expired_date',$('#passport_expired_id').val());
		postData.append('passport_working_expired_date',$('#working_expired_id').val());
		postData.append('passport_issue',$('#issue_country_id').val());
		postData.append('birthdate',$('#birthdate_id').val());
		postData.append('age',$('#age').val());
		postData.append('birthplace',$('#birthplace_id').val());
		postData.append('add_id',$('#add_id').val());
		postData.append('address',$('#address_id').val());
		postData.append('postcode',$('#postcode_id').val());
		postData.append('city',$('#city_id').val());
		postData.append('country',$('#country_id').val());
		postData.append('state',$('#state_id').val());
		postData.append('nationality',$('#nationality_id').val());
		postData.append('religion',$('#religion_id').val());
		postData.append('race',$('#race_id').val());
		postData.append('residential',$('#residential_id').val());
		postData.append('marital',$('#marital_id').val());
		postData.append('married_date',$('#married_date_id').val());
		postData.append('salihin_email',$('#staff_email_id').val()); 
		postData.append('personal_email',$('#staff_personal_email_id').val());//personal email
		postData.append('hobby',$('#staff_hobby').val());

		$.ajax({ 
			url: "<?php echo base_url('hr/api/staffprofiling/editMainInfo'); ?>",
			type:'post',
	        dataType:'json', //return type value
	        data:postData,
	        processData: false,
	        contentType: false,
			success: function(data) {
				window.location.replace("<?php echo base_url('/hr/StaffProfile/staffDetail/').$this->uri->segment(4);?>");
			}
		});
	}

	function changeList(obj) {
	var id = obj.value;

	$.ajax({
		type: "POST",
		url: "<?php echo base_url() ?>CRM/customer/getState",
		data: "region=" + id,
		dataType: "json",
		success: function(content) {

			if (content.status == "success") {
				var items = [];
				items.push('<option>Please Select</option>');
				for (var i = 0; i < content.message.length; i++) {
					items.push('<option value="' + content.message[i].id + '">'
							+ content.message[i].state + '</option>');
				}
				jQuery("#main_state").empty();
				jQuery("#main_state").append(items.join('</br>'));
			} else {
				$("#error").html('<p>' + content.message + '</p>');
			}
		}
	});
		return false;
	}

	function  getcountrydata() {
		var postData = new FormData();
		postData.append('token','<?php echo $_SESSION['token'];?>');
		postData.append('country_id',$('#country_id').val());
		$('#state_id').children('option:not(:first)').remove();
		$.ajax({
			url: "<?php echo base_url('hr/api/staffprofiling/getcountrydata'); ?>",
			type:'post',
	        dataType:'json', 
	        data:postData,
	        processData: false,
	        contentType: false,
			success: function(data) {
				if(data){
					if(data.length > 0){
						$.each(data,function(key,value){
							console.log(value);
							//this part need to be altered
							var state_id = <?php echo(!empty($addresses[0]))?$addresses[0]->state_id:0; ?>;
							if(value.state_id == state_id){
								$('#state_id').append('<option value="'+value.state_id+'" selected>'+value.state+'</option>');
							}else{
								$('#state_id').append('<option value="'+value.state_id+'">'+value.state+'</option>');

							}
						});
						$('#state_id').removeAttr('disabled');
					}else{
						$('#state_id').attr('disabled','disabled');
					}
				}
			}
		});
	}
</script>