<div class="collapse" id="collapseMembership">
	<div class="card card-body">
		<div class="container">
			<div class="row">
				<h4 class="card-title float-left">Professional Memberships</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="openMembershipModal();" data-trigger="hover" data-tooltip="tooltip" title="Add New Professional Memberships">
						<i class="fa fa-plus"></i> Add New
					</button>
				</div>
			</div>
		</div>

		<hr>

		<div class="table-responsive">
			<table id="table_membership" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>Details</th>
						<!-- <th>Attachment</th> -->
						<th>Created At</th>
						<?php if($btn_hidden == ''){ ?>	
							<th class="d-print-none">Action</th>
						<?php } ?>
					</tr>
				</thead>
				<tbody>						

					<?php if($professionalMemberships) foreach ($professionalMemberships as $key => $member): ?>

					<tr class="link-row">
						<td>
							<?php
							echo '<b>Professional Bodies : </b>'.$member->professional_body
							.'<br/><b>Name of Membership : </b>'.$member->certificate
							// .'<br/><b>Type of Membership : </b>'.$member->membership
							.'<br/><b>Type of Membership : </b>'.$member->membership_type_name
							.'<br/><b>Membership Number : </b>'.$member->membership_no
							// .'<br/><b>Year : </b>'.$member->year
							.'<br/><b>Date of Admitted to Membership : </b>'.$member->date_of_admitted
							.'<br/><b>Attachement : </b><a href="'.base_url().'hr/myprofile/openAttachment/'.$member->mask_id.'/2" target="_blank">'.$member->attachment_name.'</a>';
							?>
						</td>
						<!-- <td><a href="<?php //echo base_url('hr/myprofile/openAttachment/'.$member->mask_id.'/2'); ?>" target="_blank"><?php //echo $member->attachment_name; ?></a></td> -->
						<td><?php echo $member->created_at; ?></td>
						<?php if($btn_hidden == ''){ ?>	
							<?php if(strtolower($member->action_type) == 'personal data update' && date('m-Y',strtotime($member->created_at)) == date('m-Y', $live_date)){ ?>						
								<td class="text-center">
									<div class="btn-group">
										<button id="editMembership" type="button" class="btn btn-success btn-sm d-print-none" title="Edit Academic Membership" data-profilingID="<?php echo $member->profiling_id; ?>" data-userID="<?php echo $member->user_id; ?>" onclick="editMembership(<?php echo $member->id; ?>)"><span class="fa fa-edit"></span></button>
									<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteMembership(<?php echo $member->id; ?>)" title="Delete Academic Membership"><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 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">Name of Membership</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">Type of Membership</label>
								<!-- <input type="text" class="form-control" name="membership" id="membership"> -->
								<select class="form-control" name="membership" id="membership" required>
									<option value="-1" selected disabled>-- Please Select --</option>
									<?php if($membership_type) foreach($membership_type as $row){ ?>
										<option value="<?php echo $row['id']; ?>"><?php echo ucfirst($row['type']); ?></option>
									<?php } ?>
								</select>
							</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">Date of Admitted to Membership</label>
								<!-- <input type="text" class="form-control" name="membership_year" id="membership_year"> -->
								<input type="text" class="form-control datepicker" 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>

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

		$('#membership').val('-1');
		$('#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_type']);
                    // $("#membership").val(membership_datas[0]['membership']);
                    $("#membership_no").val(membership_datas[0]['membership_no']);
                    $("#membership_year").val(moment(membership_datas[0]['date_of_admitted']).format('DD-MM-YYYY'));
                    // $("#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>