<div class="container">
	<div class="row">
		<div class="col">
			<h4><b>PROFESSIONAL MEMBERSHIP</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openMembershipModal()" title=" Add New Membership"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
			<small><i>Please add/update your professional membership.</i></small>
		</div>
	</div>
	<div class="row">
		<div class="col">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>PROFESSIONAL BODIES</th>
						<th>NAME OF MEMBERSHIP</th>
						<th>TYPE OF MEMBERSHIP</th>
						<th>MEMBERSHIP NUMBER</th>
						<th>YEAR</th>
						<th>ATTACHMENT</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if($professional_membership) foreach ($professional_membership as $key => $row): ?>
					<tr>
						<td><?php echo ($row->professional_body)?strtoupper($row->professional_body):'N/A'; ?></td>
						<td><?php echo ($row->certificate)?strtoupper($row->certificate):'N/A'; ?></td>
						<td><?php echo ($row->membership)?strtoupper($row->membership):'N/A';  ?></td>
						<td><?php echo ($row->membership_no)?strtoupper($row->membership_no):'N/A'; ?></td>
						<td><?php echo ($row->year)?strtoupper($row->year):'N/A';?></td>
						<td><?php echo ($row->attachment_name)?strtoupper($row->attachment_name):'N/A'; ?></td>
						<td class="text-center d-print-none <?php echo $btn_hidden; ?>">
							<div class="btn-group">
								<button id="editMembership" type="button" class="btn btn-default btn-sm d-print-none" title="Edit Academic Membership" data-profilingID="<?php echo $row->profiling_id; ?>" data-userID="<?php echo $row->user_id; ?>" onclick="editMembership(<?php echo $row->id; ?>)"><span class="fa fa-edit" style="color: #F0AD4E;"></span></button>
								<button type="button" class="btn btn-default btn-sm d-print-none" onclick="deleteMembership(<?php echo $row->id; ?>)" title="Delete Academic Membership"><span class="fa fa-trash" style="color: #D9534F;"></span></button>
							</div>
						</td>
					</tr>
					<?php endforeach ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<!-- Modal -->
<div class="modal fade d-print-none" role="dialog" aria-hidden="true" id="membershipModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title"></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
			<form id="membershipForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="membership_bodies">Professional Bodies</label>
								<input type="text" class="form-control" name="membership_bodies" id="membership_bodies">
							</div>
							<div class="col-12 col-sm-6">
								<label for="membership_certificate">Professional Certificate</label>
								<input class="form-control " name="membership_certificate" id="membership_certificate">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="membership" class="col-form-label">Membership</label>
								<input type="text" class="form-control" name="membership" id="membership">
							</div>
							<div class="col-12 col-sm-6">
								<label for="membership_no" class="col-form-label">Membership No</label>
								<input type="text" class="form-control" name="membership_no" id="membership_no">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="membership_year" class="col-form-label">Year</label>
								<input type="text" class="form-control" name="membership_year" id="membership_year">
							</div>
							<div class="col-12 col-sm-6">
								<label for="membership_attachments" class="col-form-label">Attachment</label>
								<input type="file" class="form-control" name="membership_attachments" id="membership_attachments">
							</div>
						</div>
				</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="saveMembershipButton" class="btn btn-primary">Save</button>
					</div>
				</div>
			</form>
        </div>
    </div>
</div>
<?php  
//1. script for modal address
//2. script to ajax load for update address
//3. script to refresh table address
//4. script document ready to refresh table 
//5. script to save and refresh table after save 
?>

<script type="text/javascript">
	function openMembershipModal()
	{
		clearForm("#membershipForm");

		$('#membershipModal').find('#racks-modal-title').text('Add Membership');
        $('#saveMembershipButton').attr('onclick', 'saveMembership()');
		$('#membershipModal').modal('show');
	}

	function saveMembership()
	{
        var postData = new FormData();

        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("membership_bodies", $('#membership_bodies').val());
		postData.append("membership_certificate", $('#membership_certificate').val());
		postData.append("membership", $('#membership').val());
		postData.append("membership_no", $('#membership_no').val());
		postData.append("membership_year", $('#membership_year').val());

		if($('#membership_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#membership_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#membership_attachments').prop('files')[0]['name']);
		}else{
			postData.append("attachment_file", null);
			postData.append("attachment_name", null);
		}

		$.ajax({
			url:'<?php echo base_url('hr/Myprofile/saveMembership') ?>',
			data: postData,
			type: 'post',
			dataType: 'json',
			processData: false,
			contentType: false,
			success: (data) => {
				if(data.status == "success") 
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

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

	function editMembership(membership_id)
	{
		clearForm("#membershipForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#membershipModal').find('#racks-modal-title').text('Edit Membership');
		$('#saveMembershipButton').attr('onclick', 'save_editMembership('+ membership_id +')');
		$('#membershipModal').modal('show');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getMembership'); ?>',
			data: {
				'membership_id' 	: membership_id,
				'profiling_id' 		: profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					var membership_datas = data.membership;

                    $("#membership_bodies").val(membership_datas[0]['professional_body']);
                    $("#membership_certificate").val(membership_datas[0]['certificate']);
                    $("#membership").val(membership_datas[0]['membership']);
                    $("#membership_no").val(membership_datas[0]['membership_no']);
                    $("#membership_year").val(membership_datas[0]['year']);
                    // $("#membership_attachments").val(membership_datas[0]['attachment_name']);
				}
			}
		});
	}

	function save_editMembership(id) 
	{
        var postData = new FormData();

        postData.append("id", id);
        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("membership_bodies", $('#membership_bodies').val());
		postData.append("membership_certificate", $('#membership_certificate').val());
		postData.append("membership", $('#membership').val());
		postData.append("membership_no", $('#membership_no').val());
		postData.append("membership_year", $('#membership_year').val());

		if($('#membership_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#membership_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#membership_attachments').prop('files')[0]['name']);
		}else{
			postData.append("attachment_file", null);
			postData.append("attachment_name", null);
		}

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

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

	function deleteMembership(ID)
	{
		toastConfirm("Are you sure you want to delete this membership?", function(){
        var profiling_id = $("#profiling_id_new").val();

			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/Myprofile/deleteMembership'); ?>',
				data: {
					'id' : 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);
					}
				}
			});
		})
	}

</script>