<div class="collapse" id="collapseWorkingExperience">
	<div class="card card-body">
		<div class="container">
			<div class="row">
				<h4 class="card-title float-left">Experience In SALIHIN</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;" data-trigger="hover" data-tooltip="tooltip" title="Add New Experience In SALIHIN" onclick="openWorkingExperienceModal()">
						<i class="fa fa-plus"></i> Add New
					</button>
				</div>
			</div>
		</div>
		<hr>
		<div class="table-responsive">
			<table id="table_working_experience" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>Date</th>
						<th>Task Description</th>
						<th>Nature of Engagement</th>
						<th>Engagement Period</th>
						<!-- <th>Years of Experience Based on Position</th> -->
						<th>Created At</th>
						<?php if($btn_hidden == ''){ ?>	
							<th class="d-print-none">Action</th>
						<?php } ?>

					</tr>
				</thead>
				<tbody>	

					<?php if($working_experience) foreach ($working_experience as $key => $wp): 

						// $engagement_diff = $wp->engagement_period_to->diff($wp->engagement_period_from)->format('%a');
					// debug($wp);
						$date_from	= new DateTime(($wp->engagement_period_from > 0)?date('Y-m-d H:i:s',strtotime($wp->engagement_period_from)):'0000-00-00 00:00:00');
						$date_to	= new DateTime(($wp->engagement_period_to > 0)?date('Y-m-d H:i:s',strtotime($wp->engagement_period_to)):'0000-00-00 00:00:00');
						$engagement_diff = $date_to->diff($date_from)->format('%a');

						$years 	= floor(($engagement_diff / 365)); // days / 365 days
						$months = floor(($engagement_diff % 365) / 30);
						$days  	= ($engagement_diff % 365) % 30;

						$year_text = ($years > 0)?$years.' year ':'';
						$month_text = ($months > 0)?$months.' month ':'';
						$day_text = ($days > 0)?$days.' day ':'';

					?>
					<tr class="link-row" data-id="<" >
						<td><?php echo date('d-m-Y',strtotime($wp->date_from)).' - '.date('d-m-Y',strtotime($wp->date_to)); ?></td>
						<td><?php echo nl2br($wp->experience); ?></td>
						<td><?php echo nl2br($wp->nature_engagement); ?></td>
						<td><?php echo $year_text.$month_text.$day_text; ?></td>
						<!-- <td><?php //echo $wp->position_period; ?></td> -->
						<td><?php echo $wp->created_at; ?></td>
						<?php if($btn_hidden == ''){ ?>
							<?php if($wp->action_type == 'Update Profile' && date('m-Y',strtotime($wp->created_at)) == date('m-Y')){ ?>						
								<td class="text-center">
									<div class="btn-group">
										<button id="editQualification" class="btn btn-success btn-sm d-print-none" title="Edit Experience In SALIHIN" data-profilingID="<?php echo $wp->profiling_id; ?>"  onclick="editWorkingExperience(<?php echo $wp->id; ?>)"><span class="fa fa-edit"></span></button>
										<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteWorkingExperience(<?php echo $wp->id; ?>)" title="Delete Experience In SALIHIN"><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 d-print-none" 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 d-print-none" 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><br>
</div><br>
</div>

<div class="modal fade" id="workingExperienceModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title">Add Experience In SALIHIN</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="workingExperienceForm" class="form">
					<div class="row form-group">
						<div class="col-12 col-sm-6">
							<label for="year">Date From</label>
							<input type="text" class="form-control" name="date_from" id="date_from" readonly>
						</div>

						<div class="col-12 col-sm-6">
							<label for="year">Date To</label>
							<input type="text" class="form-control" name="date_to" id="date_to" readonly>
						</div>
					</div>
					<div class="form-group">
						<label for="working_experience">Task Description</label>
						<textarea class="form-control" rows="5" cols="5" name="working_experience" id="working_experience" autocomplete="off"></textarea>
					</div>
					<div class="form-group">
						<label for="nature_engagement">Nature of Engagement</label>
						<textarea class="form-control" rows="5" cols="5" name="nature_engagement" id="nature_engagement" autocomplete="off"></textarea>
					</div>
					<div class="form-group row">
						<div class="col-12 col-sm-6">	
							<label for="engagement_period_from">Engagement Period From</label>
							<input type="text" class="form-control datepicker" name="engagement_period_from" id="engagement_period_from">
						</div>
						<div class="col-12 col-sm-6">	
							<label for="engagement_period_to">Engagement Period To</label>
							<input type="text" class="form-control datepicker" name="engagement_period_to" id="engagement_period_to">
						</div>

						<!-- <div class="col-12 col-sm-4">	
							<label for="position_period">Position Years of Experience</label>
							<input type="number" class="form-control" name="position_period" id="position_period">
						</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="saveWorkingExperienceButton" onclick="saveWorkingExperience();" class="btn btn-primary">Save</button>
				</div>
			</div>
        </div>
    </div>
</div>

<script type="text/javascript">

	function openWorkingExperienceModal()
	{
		clearForm("#workingExperienceForm");

		// var date_from 	= '<?php //echo '01-'.date('m-Y',strtotime(''.date('Y-m-d').' -6 months')); ?>';
		// var date_to 	= '<?php //echo date('t-m-Y',strtotime(''.date('Y-m-d').' -1 months')); ?>';
		var date_from 	= '<?php echo '01-'.date('m-Y',strtotime('2021-08-01 -7 months')); ?>';
		var date_to 	= '<?php echo date('t-m-Y',strtotime('2021-08-01 -1 months')); ?>';

		$('#date_from').val(date_from);
		$('#date_to').val(date_to);
		$('#workingExperienceModal').find('#racks-modal-title').text('Add Experience In SALIHIN');
        $('#saveWorkingExperienceButton').attr('onclick', 'saveWorkingExperience()');
		$('#workingExperienceModal').modal('show');
	}

	function saveWorkingExperience()
	{
		var postData = {
			'profiling_id' 				: $("#profiling_id_new").val(),
			'date_from' 				: $("#date_from").val(),
			'date_to' 					: $("#date_to").val(),
			'working_experience' 		: $("#working_experience").val(),
			'nature_engagement' 		: $("#nature_engagement").val(),
			'engagement_period_from' 	: $("#engagement_period_from").val(),
			'engagement_period_to' 		: $("#engagement_period_to").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/saveWorkingExperience'); ?>',
			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 editWorkingExperience(workingExperience_id)
	{
		clearForm("#workingExperienceForm");
		
		var profiling_id = $("#editQualification").attr('data-profilingID');

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

                    $("#date_from").val(moment(workingExperienceData[0]['date_from']).format("DD-MM-YYYY"));
                    $("#date_to").val(moment(workingExperienceData[0]['date_to']).format("DD-MM-YYYY"));
                    $("#working_experience").val(workingExperienceData[0]['experience']);
                    $("#nature_engagement").val(workingExperienceData[0]['nature_engagement']);
					$("#engagement_period_from").val(workingExperienceData[0]['engagement_period_from']);
					$("#engagement_period_to").val(workingExperienceData[0]['engagement_period_to']);
                }
			}
		});

		$('#workingExperienceModal').find('#racks-modal-title').text('Edit Experience In SALIHIN');
        $('#saveWorkingExperienceButton').attr('onclick', 'save_editWorkingExperience('+ workingExperience_id +')');
		$('#workingExperienceModal').modal('show');
	}

	function save_editWorkingExperience(workingExperience_id) 
	{
		var postData = {
			'profiling_id' 				: $("#profiling_id_new").val(),
			'id'						: workingExperience_id,
			'date_from' 				: $("#date_from").val(),
			'date_to' 					: $("#date_to").val(),
			'working_experience' 		: $("#working_experience").val(),
			'nature_engagement' 		: $("#nature_engagement").val(),
			'engagement_period_from' 	: $("#engagement_period_from").val(),
			'engagement_period_to' 		: $("#engagement_period_to").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/save_editWorkingExperience'); ?>',
			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 deleteWorkingExperience(workingExperience_id)
	{
		toastConfirm("Are you sure you want to delete this working Experience?", function(){

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

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