<div class="container">
	<div class="row">
		<div class="col">
			<h4><b>PROFESSIONAL LICENSES</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openLicenseModal()" title=" Add New License"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
			<small><i>Please add/update your professional licenses.</i></small>
		</div>
	</div>
	<div class="row">
		<div class="col">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>NAME OF LICENSE</th>
						<th>LICENSES NO</th>
						<th>LICENSING BODY/AUTHORITY</th>
						<th>REGISTRATION DATE</th>
						<th>RENEWAL DATE</th>
						<th>ATTACHMENT</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if($professional_license) foreach ($professional_license as $key => $row): ?>	
					<tr>
						<td><?php echo ($row->profession)?strtoupper($row->profession):'N/A';?></td>
						<td><?php echo ($row->license_no)?strtoupper($row->license_no):'N/A';?></td>
						<td><?php echo ($row->license_body_authority)?strtoupper($row->license_body_authority):'N/A';?></td>
						<!-- <td><?php echo ($row->register_date)?strtoupper(date('d-F-Y',strtotime($row->register_date))):'N/A';?></td> -->
						<td><?php echo (!empty(mysql_to_date_format($row->register_date)))?strtoupper(mysql_to_date_format($row->register_date)):'N/A';?></td>

						<!-- <td><?php echo ($row->renewal_date)?strtoupper(date('d-F-Y',strtotime($row->renewal_date))):'N/A'; ?></td> -->
						<td><?php echo (!empty(mysql_to_date_format($row->renewal_date)))?strtoupper(mysql_to_date_format($row->renewal_date)):'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="editLicense" type="button" class="btn btn-default btn-sm d-print-none" title="Edit License" data-profilingID="<?php echo $row->profiling_id; ?>" data-userID="<?php echo $row->user_id; ?>" onclick="editLicense(<?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="deleteLicense(<?php echo $row->id; ?>)" title="Delete License"><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="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">Profession</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 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_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 class="col-12 col-sm-6">
								<label for="license_renewal_date" class="col-form-label">Latest Renewal Date</label>
								<input type="text" class="form-control datepicker" name="license_renewal_date" id="license_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>
<?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 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_registration_date", $('#license_registration_date').val());
		postData.append("license_renewal_date", $('#license_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_renewal_date").val(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_registration_date", $('#license_registration_date').val());
		postData.append("license_renewal_date", $('#license_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>