<!-- contract element -->
<section id="sect-contract-element" class="border p-3 d-none">
	<div class="row title-row" data-toggle="collapse" href="#collapseContractElement" role="button" aria-expanded="false">
		<div class="col-12">
			<h3 id="sect-contract-element-header">Contract Element</h3>
		</div>
	</div>
	<div id="collapseContractElement">
			<div class="row">
				<div class="col-12 col-lg-6">
					<label><b>Contract Type</b></label>
					<div class="input-group mb-3">
					  	<select class="form-control" name="contract_type" id="contract_type">
							<?php foreach ($hiringTypes as $key => $hiring): ?>
								<?php if (!empty($contract_element->contract_hiring_type_id)):  ?>
									<option value="<?php echo $hiring->id; ?>" <?php echo($hiring->id == $contract_element->contract_hiring_type_id)?'selected="selected"':'';?>><?php echo $hiring->type; ?></option>
								<?php else: ?>
									<option value="<?php echo $hiring->id; ?>"><?php echo $hiring->type; ?></option>
								<?php endif ?>
							<?php endforeach ?>
						</select>
					  <div class="input-group-append">
					    <button class="btn btn-warning btn-sm" type="button" data-toggle="modal" data-target="#newContractType"><i class="fas fa-plus"></i></button>
					  </div>
					</div>
				</div>
				<div class="col-12 col-lg-6">
					<label><b>Contract Duration(Month)</b></label>
					<div class="input-group">
					  <input type="number" class="form-control" name="contract_duration" id="contract_duration" value="<?php echo(!empty($contract_element->duration))?$contract_element->duration:'';?>" readonly>
					  <div class="input-group-append">
					    <select class="form-control" name="contract_duration_period" id="contract_duration_period" readonly>

					    	<?php if (!empty($contract_element->duration)): ?>
					    		<option value="2" <?php echo($contract_element->duration == 2)?'selected="selected"':'';?> >Months</option>
					    	<?php else: ?>
					    		<option value="2">Months</option>
					    	<?php endif ?>

					    </select>
					  </div>
					</div>
				</div>
				
			</div>

			<div class="row">
				<div class="col-12 col-lg-6">
					<label><b>Confirmation</b></label>
					<div class="input-group mb-3">
					  	<select class="form-control" name="contract_confirmation" id="contract_confirmation"> <!-- onchange="setReminder();" -->
							<?php foreach ($contractConfirmations as $key => $confirm): ?>
								<?php if (!empty($contract_element->confirmation)): ?>
									<option value="<?php echo $confirm->id; ?>" <?php echo($contract_element->confirmation == $confirm->id)?'selected="selected"':''; ?>><?php echo $confirm->type; ?></option>
								<?php else: ?>
									<option value="<?php echo $confirm->id; ?>"><?php echo $confirm->type; ?></option>
								<?php endif ?>
							<?php endforeach ?>
						</select>
					  <div class="input-group-append">
					    <button class="btn btn-warning btn-sm" type="button" data-toggle="modal" data-target="#confirmationType"><i class="fas fa-plus"></i></button>
					  </div>
					</div>
				</div>
				<div class="col-12 col-lg-6">
					<label><b>Confirmation Date</b></label>
					<input type="text" class="form-control datepicker" name="contract_confirmation_date" id="contract_confirmation_date" value="<?php echo(!empty($contract_element->confirmation_date))?date('d-m-Y',strtotime($contract_element->confirmation_date)):'';?>" >
				</div>
			</div>

			<div class="row">		
				<div class="col-12 col-lg-4">
					<label><b>Probationary Period</b></label>
					<div class="input-group">
					  <input type="number" class="form-control" name="contract_probationary_period" value="<?php echo(!empty($contract_element->probation))?$contract_element->probation:'';?>" onchange="confirmDateChange()">
					  <div class="input-group-append">
					    <select class="form-control" name="contract_probationary_period_type" onchange="confirmDateChange()">
					    	<?php if (!empty($contract_element->probation_duration)): ?>
					    		<option value="0" <?php echo($contract_element->probation_duration == 0)?'selected="selected"':'';?> >Days</option>
					    		<option value="1" <?php echo($contract_element->probation_duration == 1)?'selected="selected"':'';?> >Weeks</option>
					    		<option value="2" <?php echo($contract_element->probation_duration == 2)?'selected="selected"':'';?> >Months</option>
					    		<option value="3" <?php echo($contract_element->probation_duration == 3)?'selected="selected"':'';?> >Years</option>
					    	<?php else: ?>
					    		<option value="0">Days</option>
					    		<option value="1">Weeks</option>
					    		<option value="2">Months</option>
					    		<option value="3">Year</option>
					    	<?php endif ?>

					    </select>
					  </div>
					</div>
				</div>
				<div class="col-12 col-lg-4">
					<label><b>Employer Notice Period</b></label>
					<div class="input-group">
					  <input type="number" class="form-control" name="contract_employer_notice_period" value="<?php echo(!empty($contract_element->er_notice))?$contract_element->er_notice:'';?>">
					  <div class="input-group-append">
					    <select class="form-control" name="contract_employer_notice_type">
					    	<?php if (!empty($contract_element->er_notice_duration)): ?>
					    		<option value="0" <?php echo($contract_element->er_notice_duration == 0)?:'selected="selected"'; ?>>Days</option>
					    		<option value="1" <?php echo($contract_element->er_notice_duration == 1)?:'selected="selected"'; ?>>Weeks</option>
					    		<option value="2" <?php echo($contract_element->er_notice_duration == 2)?:'selected="selected"'; ?>>Months</option>
					    		<option value="3" <?php echo($contract_element->er_notice_duration == 3)?:'selected="selected"'; ?>>Years</option>
					    	<?php else: ?>
					    		<option value="0">Days</option>
					    		<option value="1">Weeks</option>
					    		<option value="2">Months</option>
					    		<option value="3">Year</option>
					    	<?php endif ?>
					    	
					    </select>
					  </div>
					</div>
				</div>
				<div class="col-12 col-lg-4"> 
					<label><b>Employee Notice Period</b></label>
					<div class="input-group">
					  <input type="number" class="form-control" name="contract_employee_notice_period" id="contract_employee_notice_period" value="<?php echo(!empty($contract_element->ee_notice))?$contract_element->ee_notice:''; ?>">
					  <div class="input-group-append">
					    <select class="form-control" name="contract_employee_notice_type" id="contract_employee_notice_type">
					    	<?php if (!empty($contract_element->ee_notice_duration)): ?>
					    		<option value="0" <?php echo($contract_element->ee_notice_duration == 0)?'selected="selected"':''; ?>>Days</option>
					    		<option value="1" <?php echo($contract_element->ee_notice_duration == 1)?'selected="selected"':''; ?>>Weeks</option>
					    		<option value="2" <?php echo($contract_element->ee_notice_duration == 2)?'selected="selected"':''; ?>>Months</option>
					    		<option value="3" <?php echo($contract_element->ee_notice_duration == 3)?'selected="selected"':''; ?>>Year</option>
					    	<?php else: ?>
					    		<option value="0">Days</option>
					    		<option value="1">Weeks</option>
					    		<option value="2">Months</option>
					    		<option value="3">Year</option>
					    	<?php endif ?>
					    </select>
					  </div>
					</div>
				</div>
			</div>

			<div class="row">		
				<div class="col-12 col-lg-4">
					<label><b>Resign Date</b></label>
					<input type="text" class="form-control datepicker" name="resign_date" id="resign_date" value="" onchange="calculate_resign_date()">
				</div>
				<div class="col-12 col-lg-4">
					<label><b>Last Working Date</b></label>
					<input type="text" class="form-control datepicker" name="last_working_date" id="last_working_date" value="" >
				</div>
				<div class="col-12 col-lg-4">
					<label><b>Physical Last Working Date</b></label>
					<input type="text" class="form-control datepicker" name="phy_last_working_date" id="phy_last_working_date" value="" >
				</div>
			</div>
	</div>
</section>
<!-- contract element -->

<script>
	$(document).ready(function() {
 		
	 	$('#contract_type').change(function() {

	 	$action_effective_date = $('input[name="efective_date"]').val();
	 		
	  	if($(this).val() == 2) {
	  		$('#contract_confirmation_date').val($action_effective_date);
	  		$('#contract_confirmation_date').prop("disabled", false);
	       	$('#contract_duration').prop("readonly", false);
	    } else if($(this).val() == 4){
	    	$('#contract_confirmation_date').val("");
	    	$('#contract_confirmation_date').prop("disabled", true);
	    	$('#contract_duration').prop("readonly", true);
	    } else {   
	    	$('#contract_confirmation_date').val($action_effective_date);
	    	$('#contract_confirmation_date').prop("disabled", false);   
	      	$('#contract_duration').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 		= $('#contract_employee_notice_period').val();
			var notice_period_type 	= $('#contract_employee_notice_type').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>
