<div class="collapse" id="collapseContract">
	<div class="card card-body">
		<h4 class="card-title">Contract Element</h4>
		<hr>
		<div class="container">
			<?php if ($role == 'admin'): ?>
				<div class="row">
					<div class="btn-group ml-auto">
						<a class="btn btn-info btn-sm" style="margin-bottom:15px; width: 200px;" href="<?php echo base_url('hr/StaffProfile/actionReport/'.$staff_id.'/9'); ?>" target="_blank">
							<i class="fa fa-print"></i> Print
						</a>
					</div>
				</div>
			<?php endif ?>
		</div>
		
		<div class="table-responsive">
			<table id="table_contract" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>Action type (effective date )</th>
						<th>Details</th>
						<th>Created At</th>
						<!-- <?php if($role == 'admin'): ?>
						<th>Action</th>
						<?php endif ?> -->

					</tr>
				</thead>
				<tbody>		

					<?php if($contracts) foreach ($contracts as $key => $contract): ?>					
						<tr class="link-row" data-id="" >
							<td scope="row">
								<?php
									echo $contract->action_type.'<br/><b>( '.date('d-F-Y',strtotime($contract->action_efective_date)).' )</b>';
								?>
							</td>
							<td>
								<b>Contract Type : </b><?php echo ($contract->contract_type)?$contract->contract_type:''; ?>
								<br><b>Contract duration : </b><?php echo $contract->duration.' Months'; ?>
								<br><b>Confirmation : </b><?php echo ($contract->confirmation_type)?$contract->confirmation_type:''; ?>
								<!-- <br><b>Confirmation Date : </b><?php echo ($contract->confirmation_date)?date('d-F-Y',strtotime($contract->confirmation_date)):''; ?> -->
								<br><b>Confirmation Date : </b><?php echo (mysql_to_date_format((!empty($contract->confirmation_date)?$contract->confirmation_date:null))); ?>

								<br><b>Probationary Period : </b>
								<?php 
									echo $contract->probation;
									switch ($contract->probation_duration) {
										case 0:
											echo ' Days';
											break;
										case 1:
											echo ' Weeks';
											break;
										case 2:
											echo ' Months';
											break;
										case 3:
											echo ' Years';
											break;
										default:
											break;
									}
								?>
								<br><b>Employer Notice Period : </b>
								<?php
									echo $contract->er_notice;
									switch ($contract->er_notice_duration) {
										case 0:
											echo ' Days';
											break;
										case 1:
											echo ' Weeks';
											break;
										case 2:
											echo ' Months';
											break;
										case 3:
											echo ' Years';
											break;
										default:
											break;
									}
								?>
								<br><b>Employee Notice Period : </b>
								<?php
									echo $contract->ee_notice;
									switch ($contract->ee_notice_duration) {
										case 0:
											echo ' Days';
											break;
										case 1:
											echo ' Weeks';
											break;
										case 2:
											echo ' Months';
											break;
										case 3:
											echo ' Years';
											break;
										default:
											break;
									}
								?>
								<br><b>Resign Date : </b><?php echo (mysql_to_date_format((!empty($contract->resign_date)?$contract->resign_date:null))); ?>
								<br><b>Last Working Date : </b><?php echo (mysql_to_date_format((!empty($contract->last_working_date)?$contract->last_working_date:null))); ?>
								<br><b>Physical Last Working Date : </b><?php echo (mysql_to_date_format((!empty($contract->phy_last_working_date)?$contract->phy_last_working_date:null))); ?>
							</td>
							<td><?php echo $contract->created_at; ?></td>
							<!-- <td>
								<div class="text-center">
									<div class="btn-group">
										<button class="btn btn-success btn-sm" data-toggle="modal" data-target="#contractModal" data-content="<?php //echo base64_encode(json_encode($contract)); ?>" onclick="btnEditContractElement(this)"><i class="fas fa-edit"></i></button>
										<button class="btn btn-danger btn-sm" onclick="btnRemoveContractElement(this)" data-content="<?php //echo base64_encode(json_encode($contract)); ?>" ><i class="fas fa-trash"></i></button>
									</div>
								</div>
							</td> -->
						</tr>		
					<?php endforeach ?>				

				</tbody>
			</table>
		</div><br>
	</div><br>
</div>

<div class="modal fade" id="contractModal">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="racks-modal-title">Edit Contract Element</h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body">
				<input type="hidden" class="form-control" id="modal-contract-id">
				<div class="row">
					<div class="col">
						<div class="form-group">
							<label for="bank_name">Contract Type</label>
							<select class="form-control" id="modal-contract-type">
									<?php foreach ($hiringTypes as $key => $hiring): ?>
										<option value="<?php echo $hiring->id; ?>"><?php echo $hiring->type; ?></option>
									<?php endforeach ?>
							</select>
						</div>
					</div>
					<div class="col">
						<div class="form-group">
							<label for="bank_branch">Confirmation</label>
							<select class="form-control" id="modal-contract-confirmation">
								<?php foreach ($contractConfirmations as $key => $confirm): ?>
									<option value="<?php echo $confirm->id; ?>"><?php echo $confirm->type; ?></option>
								<?php endforeach ?>
							</select>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col">
						<div class="form-group">
							<label for="bank_branch">Contract duration (Month)</label>
							<div class="input-group">
								<input type="text" class="form-control" id="modal-contract-duration-period" placeholder="Enter Contract Duration" disabled>
								<div class="input-group-append">
									<select class="form-control" id="modal-contract-duration" disabled>
										<option>Months</option>
									</select>
								</div>
							</div>
						</div>
					</div>
					<div class="col">
						<div class="form-group">
							<label for="bank_branch">Confirmation Date</label>
							<input type="text" class="datepicker form-control" id="modal-contract-confirmation-date" placeholder="Enter confirmation date">
							<input type="hidden" class="datepicker form-control" id="modal-contract-confirmation-date2">
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col">
						<div class="form-group">
							<label for="bank_branch">Probationary Period</label>
							<div class="input-group">
								<input type="text" class="form-control" id="modal-contract-probationary-period" placeholder="Enter Probationary Period">
								<div class="input-group-append">
									<select class="form-control" id="modal-contract-probationary-duration">
										<option value="0">Days</option>
										<option value="1">Weeks</option>
										<option value="2">Months</option>
										<option value="3">Years</option>
									</select>
								</div>
							</div>
						</div>
					</div>

					<div class="col">
						<div class="form-group">
							<label for="bank_branch">ER Notice Period</label>
							<div class="input-group">
								<input type="text" class="form-control" id="modal-contract-er-period" placeholder="Enter Employer Notice Period">
								<div class="input-group-append">
									<select class="form-control" id="modal-contract-er-duration">
										<option value="0">Days</option>
										<option value="1">Weeks</option>
										<option value="2">Months</option>
										<option value="3">Years</option>
									</select>
								</div>
							</div>
						</div>
					</div>
					<div class="col">
						<div class="form-group">
							<label for="bank_branch">EE Notice Period</label>
							<div class="input-group">
								<input type="text" class="form-control" id="modal-contract-ee-period" placeholder="Enter Employee Notice Period">
								<div class="input-group-append">
									<select class="form-control" id="modal-contract-ee-duration">
										<option value="0">Days</option>
										<option value="1">Weeks</option>
										<option value="2">Months</option>
										<option value="3">Years</option>
									</select>
								</div>
							</div>
						</div>	
					</div>
				</div>
				<div class="row">		
					<div class="col">
						<label>Resign Date</label>
						<input type="text" class="form-control datepicker" name="resign_date" id="resign_date" value="" onchange="calculate_resign_date()">
					</div>
					<div class="col">
						<label>Last Working Date</label>
						<input type="text" class="form-control datepicker" name="last_working_date" id="last_working_date" value="" >
					</div>
					<div class="col">
						<label>Physical Last Working Date</label>
						<input type="text" class="form-control datepicker" name="phy_last_working_date" id="phy_last_working_date" value="" >
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-danger btn-sm" style="margin-bottom:15px; width: 200px;" data-dismiss="modal"><i class="fa fa-ban"></i> Cancel</button>
				<button type="button" id="saveButtonBank" class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;" onclick="EditContractElem()"><i class="fa fa-save"></i> Submit</button>
			</div>
		</div>
	</div>
</div>

<script>
	$(document).ready(function() {
 
 	$('#modal-contract-type').change(function() {
  	if( $(this).val() == 1) {
       		$('#modal-contract-duration-period').prop( "disabled", true );
    } else {       
      $('#modal-contract-duration-period').prop( "disabled", false );
    }
  });

  $('#modal-contract-type').change(function() {

	 	$confirm_date = $('#modal-contract-confirmation-date2').val();
	 		
	  	if($(this).val() == 2) {
	  		$('#modal-contract-confirmation-date').val($confirm_date);
	  		$('#modal-contract-confirmation-date').prop("disabled", false);
	       	$('#modal-contract-duration-period').prop("readonly", false);
	    } else if($(this).val() == 4){
	    	$('#modal-contract-confirmation-date').val("");
	    	$('#modal-contract-confirmation-date').prop("disabled", true);
	    	$('#modal-contract-duration-period').prop("readonly", true);
	    } else {   
	    	$('#modal-contract-confirmation-date').val($confirm_date);
	    	$('#modal-contract-confirmation-date').prop("disabled", false);   
	      $('#modal-contract-duration-period').prop("readonly", true);
	    }
	    
	  });
 
});

	function calculate_resign_date() 
	{

		if($('#resign_date').val() == ''){

			$('#last_working_date').val('');
			$('#phy_last_working_date').val('');

		}else{
			var resign_date 				= moment($('#resign_date').val(), "DD-MM-YYYY"); 
			var notice_period 			= $('#modal-contract-ee-period').val();
			var notice_period_type 	= $('#modal-contract-ee-duration').val();

			if(notice_period_type == 0){
				var notice_period_type_f = 'days';
			}else if(notice_period_type == 1){
				var notice_period_type_f = 'weeks';
			}else if(notice_period_type == 2){
				var notice_period_type_f = 'months';
			}else{
				var notice_period_type_f = 'years';
			}

			var last_date = moment(resign_date).add(notice_period, '' + notice_period_type_f + '').format("DD-MM-YYYY"); 

			$('#last_working_date').val(last_date);
			$('#phy_last_working_date').val(last_date);
		}

	}
</script>