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

		<hr>
		<div class="table-responsive">
			<table id="table_license" 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(!empty($professionalLicense)) foreach ($professionalLicense as $key => $license): ?>	
						<tr class="link-row">
							<td>
								<?php 
									echo '<b>Name of License : </b>'.$license->profession
									.'<br/><b>License No : </b>'.$license->license_no
									.'<br/><b>Licensing Body/Authority : </b>'.$license->license_body_authority
									.'<br/><b>Register Date : </b>'.mysql_to_date_format((!empty($license->register_date))?$license->register_date:null)
									.'<br/><b>Latest Renewed Date : </b>'.mysql_to_date_format((!empty($license->renewal_date))?$license->renewal_date:null)
									.'<br/><b>Next Renewal Date : </b>'.mysql_to_date_format((!empty($license->next_renewal_date))?$license->next_renewal_date:null)
									.'<br/><b>Attachment : </b><a href="'.base_url().'hr/myprofile/openAttachment/'.$license->mask_id.'/3" target="_blank">'.$license->attachment_name.'</a>'; 
									// .'<br/><b>Renewal Date : </b>'.date('d-F-Y',strtotime($license->renewal_date)); 
								?>
							</td>
							<!-- <td><a href="<?php //echo base_url('hr/myprofile/openAttachment/'.$license->mask_id.'/3'); ?>" target="_blank"><?php //echo $license->attachment_name; ?></a></td> -->
							<td><?php echo $license->created_at; ?></td>
							<?php if($btn_hidden == ''){ ?>	
								<?php if(strtolower($license->action_type) == 'personal data update' && date('m-Y',strtotime($license->created_at)) == date('m-Y', $live_date)){ ?>
									<td class="text-center">
										<div class="btn-group">
											<button id="editLicense" type="button" class="btn btn-success btn-sm d-print-none" title="Edit License" data-profilingID="<?php echo $license->profiling_id; ?>" data-userID="<?php echo $license->user_id; ?>" onclick="editLicense(<?php echo $license->id; ?>)"><span class="fa fa-edit"></span></button>
											<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteLicense(<?php echo $license->id; ?>)" title="Delete License"><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="licenseModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title">Add/Edit License</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
			<form id="licenseForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="license_profession">Name of License</label>
								<input type="text" class="form-control" name="license_profession" id="license_profession">
							</div>
							<div class="col-12 col-sm-6">
								<label for="license_no">License No</label>
								<input type="text" class="form-control " name="license_no" id="license_no">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="license_no">Licensing Body/Authority</label>
								<input type="text" class="form-control " name="license_body_authority" id="license_body_authority">
							</div>
							<div class="col-12 col-sm-6">
								<label for="license_registration_date" class="col-form-label">Initial Registration Date</label>
								<input type="text" class="form-control datepicker" name="license_registration_date" id="license_registration_date">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="license_renewal_date" class="col-form-label">Latest Renewed Date</label>
								<input type="text" class="form-control datepicker" name="license_renewal_date" id="license_renewal_date">
							</div>
							<div class="col-12 col-sm-6">
								<label for="license_next_renewal_date" class="col-form-label">Next Renewal Date</label>
								<input type="text" class="form-control datepicker" name="license_next_renewal_date" id="license_next_renewal_date">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="license_attachments" class="col-form-label">Attachment</label>
								<input type="file" class="form-control" name="license_attachments" id="license_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="saveLicenseButton" onclick="savelicense();" class="btn btn-primary">Save</button>
					</div>
				</div>
			</form>
        </div>
    </div>
</div>

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

		$('#licenseModal').find('#racks-modal-title').text('Add License');
        $('#saveLicenseButton').attr('onclick', 'saveLicense()');
		$('#licenseModal').modal('show');
	}

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

        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("license_profession", $('#license_profession').val());
		postData.append("license_no", $('#license_no').val());
		postData.append("license_body_authority", $('#license_body_authority').val());
		postData.append("license_registration_date", $('#license_registration_date').val());
		postData.append("license_renewal_date", $('#license_renewal_date').val());
		postData.append("license_next_renewal_date", $('#license_next_renewal_date').val());

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

		$.ajax({
			url:'<?php echo base_url('hr/Myprofile/saveLicense') ?>',
			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 editLicense(license_id)
	{
		clearForm("#licenseForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#licenseModal').find('#racks-modal-title').text('Edit License');
		$('#saveLicenseButton').attr('onclick', 'save_editLicense('+ license_id +')');
		$('#licenseModal').modal('show');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getLicense'); ?>',
			data: {
				'license_id' 	: license_id,
				'profiling_id' 	: profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					var license_datas = data.license;
					console.log(license_datas);
					if(license_datas.length > 0)
					{
						var register_date = moment(license_datas[0]['register_date']).format('DD-MM-YYYY');
						var renewal_date = moment(license_datas[0]['renewal_date']).format('DD-MM-YYYY');
					}else{
						var register_date = '00-00-0000';
						var renewal_date = '00-00-0000';
					}

                    $("#license_profession").val(license_datas[0]['profession']);
                    $("#license_no").val(license_datas[0]['license_no']);
                    $("#license_registration_date").val(register_date);
                    $("#license_body_authority").val(license_datas[0]['license_body_authority']);
                    $("#license_renewal_date").val(renewal_date);
                    $("#license_next_renewal_date").val(next_renewal_date);
                    // $("#license_attachments").val(license_datas[0]['attachment_name']);
				}
			}
		});
	}

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

        postData.append("id", id);
        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("license_profession", $('#license_profession').val());
		postData.append("license_no", $('#license_no').val());
		postData.append("license_body_authority", $('#license_body_authority').val());
		postData.append("license_registration_date", $('#license_registration_date').val());
		postData.append("license_renewal_date", $('#license_renewal_date').val());
		postData.append("license_next_renewal_date", $('#license_next_renewal_date').val());

		if($('#license_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#license_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#license_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_editLicense'); ?>',
			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 deleteLicense(ID)
	{
		toastConfirm("Are you sure you want to delete this license?", function(){
        var profiling_id = $("#profiling_id_new").val();

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