<div class="collapse" id="collapseAddress">
	<div class="card card-body">
		<div class="container">
			<div class="row">
				<h4 class="card-title float-left">Addresses</h4>
				<div class="btn-group ml-auto float-right">
					<button type="button" class="btn btn-primary btn-sm d-print-none <?php echo $btn_hidden; ?>" style="margin-bottom:15px; width: 200px;" onclick="openAddressModal();" data-trigger="hover" data-tooltip="tooltip" title="Add New Addresses">
						<i class="fa fa-plus"></i> Add New
					</button>
				</div>
			</div>
		</div>
		<hr>
		
		<div class="table-responsive">
			<table id="table_address" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>TYPE</th>
						<th>ADDRESS</th>
						<th>COUNTRY</th>
						<th>STATE</th>
						<th>CITY</th>
						<th>POSTCODE</th>
						<th>CONTACT</th>
						<th>CREATED AT</th>
						<?php if($btn_hidden == ''){ ?>	
							<th class="d-print-none">ACTION</th>
						<?php } ?>
					</tr>
				</thead>
				<tbody>
					<?php if ($addresses) foreach ($addresses as $key => $address): ?>
					<tr>
						<td>
							<?php
								if ($address->type == 1) {
								 	echo 'PERMANENT RESIDENCE';
								}elseif ($address->type == 2) {
									echo 'TEMPORARY RESIDENCE';
								}elseif ($address->type == 3) {
									echo 'CORRESPONDENCE RESIDENCE';
								}else{
									echo 'N/A';
								}
							?>
						</td>
						<td><?php echo ($address->address)?strtoupper($address->address):'N/A'; ?></td>
						<td><?php echo ($address->country_name)?strtoupper($address->country_name):'N/A'; ?></td>
						<td><?php echo ($address->state_name)?strtoupper($address->state_name):'N/A'; ?></td>
						<td><?php echo ($address->city)?strtoupper($address->city):'N/A'; ?></td>
						<td><?php echo ($address->postcode)?strtoupper($address->postcode):'N/A'; ?></td>
						<td><?php echo ($address->contact_no)?strtoupper($address->contact_no):'N/A'; ?></td>
						<td><?php echo $address->created_at; ?></td>
						<?php if($btn_hidden == ''){ ?>	
							<?php if(strtolower($address->action_type) == 'personal data update' && date('m-Y',strtotime($address->created_at)) == date('m-Y', $live_date)){ ?>					
								<td class="text-center">
									<div class="btn-group" role="group">
									 	<button id="editAddressButton" type="button" class="btn btn-success btn-sm d-print-none" onclick="editAddress(<?php echo $address->id; ?>)" title="Edit Address" data-profilingID="<?php echo $address->profiling_id; ?>" data-userID="<?php echo $address->user_id; ?>"><span class="fa fa-edit"></span></button>
									 	<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteAddress(<?php echo $address->id; ?>)" title="Delete Address"><span class="fa fa-trash"></span></button>
									</div>
								</td> 
							<?php } else { ?>
								<td class="text-center">
									<div class="btn-group" role="group">
									 	<button class="btn btn-success btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to update"><i class="fas fa-edit" ></i></button>
									 	<button class="btn btn-danger btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to delete"><i class="fas fa-trash-alt"></i></button>
									</div>
								</td> 
							<?php } ?>
						<?php } ?>
					</tr>
					<?php endforeach ?>
				</tbody>
			</table>
		</div>
	</div><br>
</div>

<!-- Modal -->
<div class="modal fade" id="addressModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title">Add Address</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
			<div class="modal-body">
				<form id="addressForm" class="form">
					<div class="form-group">
						<label for="address_type">Type</label>
						<select class="select form-control" name="address_type" id="address_type">
							<?php foreach ($residenceType as $key => $type): ?>
								<option value="<?php echo $type->id; ?>"><?php echo $type->type; ?></option>
							<?php endforeach ?>	
						</select>
					</div>
					<div class="form-group">
						<label for="address_place">Address</label>
						<input class="form-control" rows="5" name="address_place" id="address_place" autocomplete="off">
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="address_country" class="col-form-label">Country</label>
							<select class="select form-control" name="address_country" id="address_country" onchange="change_state_input()">
								<?php foreach ($countries as $key => $country): ?>
									<option value="<?php echo $country->id; ?>"><?php echo $country->country; ?></option>
								<?php endforeach ?>
							</select>
						</div>
						<div class="col-12 col-sm-6">
							<label for="address_state" class="col-form-label">State</label>
							<select class="select form-control" name="address_state" id="address_state">
								<?php if($states) foreach ($states as $key => $state): ?>
				                	<option value="<?php echo $state->id; ?>"><?php echo $state->state; ?></option>
				                <?php endforeach ?>
							</select>
						</div>
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="address_city" class="col-form-label">City</label>
							<input type="text" class="form-control" autocomplete="off" name="address_city" id="address_city">
						</div>
						<div class="col-12 col-sm-6">
							<label for="address_postcode" class="col-form-label">Postcode</label>
							<input type="text" class="form-control" name="address_postcode" id="address_postcode" autocomplete="off">
						</div>
					</div>
					<div class="form-group">
						<label for="address_contact_number">Contact Number</label>
						<div class="input-group mb-3">
							<input type="text" class="form-control" name="address_contact_number" id="address_contact_number">
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<div class="btn-group float-right">
					<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
					<button type="button" id="saveAddressButton" onclick="saveAddress();" class="btn btn-primary">Save</button>
				</div>
			</div>
        </div>
    </div>
</div>

<script type="text/javascript">

	function openAddressModal()
	{

		clearForm("#addressForm");

		$('#addressModal').find('#racks-modal-title').text('Add Address');
        $('#saveAddressButton').attr('onclick', 'saveAddress()');
		$('#addressModal').modal('show');
	}

	function saveAddress()
	{
		var postData = {
			'profiling_id' 	: $("#profiling_id_new").val(),
			'type' 			: $("#address_type").val(),
			'address' 		: $("#address_place").val(),
			'postcode' 		: $("#address_postcode").val(),
			'city' 			: $("#address_city").val(),
			'state_id' 		: $("#address_state").val(),
			'country_id' 	: $("#address_country").val(),
			'contact_no' 	: $("#address_contact_number").val()
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/saveAddress'); ?>',
			data: postData,
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

					location.replace(data.redirect);
				}
			}
		});
	}

	function editAddress(address_id)
	{
		clearForm("#addressForm");
		
		var profiling_id = $("#editAddressButton").attr('data-profilingID');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getAddress'); ?>',
			data: {
				'address_id' : address_id,
				'profiling_id' : profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if ($.trim(data.status) == "success")
                {
                    var addressData = data.address;

                    $("#address_type").val(addressData[0]['type']);
                    $("#address_place").val(addressData[0]['address']);
                    $("#address_postcode").val(addressData[0]['postcode']);
                    $("#address_city").val(addressData[0]['city']);
                    $("#address_state").val(addressData[0]['state_id']);
                    $("#address_country").val(addressData[0]['country_id']);
                    $("#address_contact_number").val(addressData[0]['contact_no']);
                }
			}
		});

		$('#addressModal').find('#racks-modal-title').text('Edit Address');
        $('#saveAddressButton').attr('onclick', 'save_editAddress('+ address_id +')');
		$('#addressModal').modal('show');
	}

	function save_editAddress(address_id) 
	{
		var postData = {
			'id'		: address_id,
			'type' 		: $("#address_type").val(),
			'address' 	: $("#address_place").val(),
			'postcode' 	: $("#address_postcode").val(),
			'city' 		: $("#address_city").val(),
			'state_id' 	: $("#address_state").val(),
			'country_id' : $("#address_country").val(),
			'contact_no' : $("#address_contact_number").val()
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/save_editAddress'); ?>',
			data: postData,
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

					location.replace(data.redirect);
				}
			}
		});
	}

	function deleteAddress(address_id)
	{
		toastConfirm("Are you sure you want to delete this address?", function(){

			var profiling_id = $("#profiling_id_new").val();

			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/Myprofile/deleteAddress'); ?>',
				data: {
					'id' : address_id,
					'profiling_id' : profiling_id
				},
				dataType: 'JSON',
				success: function(data) {
					if(data.status == 'success')
					{
						localStorage.setItem("status",data.status);
						localStorage.setItem("msg",data.msg);

						location.replace(data.redirect);
					}
				}
			});
		})
	}

	function change_state_input()
	{
		var country_id = $("#address_country").val();

        $('#address_state').html('');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getState'); ?>',
			data: {
				'country_id' : country_id,
			},
			dataType: 'JSON',
			success: function(data) {
				if ($.trim(data.status) == "success")
                {
                    var state = data.states;
                    
                    $.each(state, function(index, content) 
                    {
					  	$('#address_state').append($('<option>', { 
						    value: content.id,
						    text : content.state 
						})).select2({theme: 'bootstrap4'});
					  	// $("#address_state").select2('data', {value: value.id, text: value.state});
					});
                    // $("#address_state").val(addressData[0]['state_id']);
                }
			}
		});
	}
</script>