
<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12">
<div id="contract_div" class="card">
	<div class="card-header">
		<h3 class="card-title">Job Contract</h3>

		<div class="card-tools">
			<div class="input-group input-group-sm" style="width: 150px;">
				<a href="" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforContract" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary add_new_contract" style="float:right;margin-right:5px;margin-bottom:5px;">
		Add Contract</a>
			</div>
		</div>
	</div>
	


	
			<div class="card-body table-responsive" style="height: auto;">
				<div class="box box-primary" id="overflow_div">
					<!-- Start  records header -->
					<table id="contract_table" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Job Id</th>
								<th>Policy type</th>
								<th>Contract status</th>
								<th>Contract value</th>
								<th>Renewal date</th>
								<th>Edit</th>
								<th>Delete</th>
							</tr>
						</thead>
					</table>
				</div>
			</div>
	<!---------- Post Out ---------------->
</div>
</div>
</div>
</div>
</section>

<!-- Modal -->
<div class="modal fade" id="myModalAjaxforContract" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <div class="col-lg-12">
		<div class="row">
			<div class="col-lg-8">
				<h4 class="modal-title" id="myModalLabelAjax">Modal title</h4>
			</div>
			<div class="col-lg-4">
				<button type="button" class="close close_button2" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
		</div>
		</div>
      </div>
      <div class="modal-body modal_iframe2">
			<section class="content">
				<div class="row">
					<div class="col-lg-12">
					<div class="alert alert-info alert-dismissable contract_message" style="display:none;">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					 </div>
						<div class="box box-primary">
							<div class="box-body">
							<form action="" method="post" name="add_form_contract" id="add_form_contract">
								<div class="col-lg-12">
									<label for="Policy Type" class="control-label">Policy Type</label>
									<? echo Keycode::getKeycodeListValue("policy_type", $policy_type, $width = "100%", "Policy Type", "id", "code", "policy_type","","form-control"); ?>
								</div>
								<div class="col-lg-12">
									<label for="Contract Status" class="control-label">Contract Status</label>
									<? echo Keycode::getKeycodeListValue("contract_status", $contract_status, $width = "100%", "Contract Status", "id", "code", "contract_status","","form-control"); ?>
								</div>
								<div class="col-lg-12">
									<label for="Contract value" class="control-label">Contract value</label>
									<input type="text" class="form-control" id="contract_value" name="contract_value" value="<?php echo $contract_value;?>" required>
								</div>
								<div class="col-lg-12">
									<label for="Date" class="control-label">Renewal date</label>
									<input type="text" class="form-control datepicker" id="renewal_date" name="renewal_date" value="<?php (isset($renewal_date) && $renewal_date !="") ? utility::getFormatedDate($renewal_date) : utility::getFormatedDate(date($config->dateFormat));?>" required>
								</div>							
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12">
									<input type="hidden" id="contract_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="<?=$job_id;?>">
									<input type="hidden" id="page_action_contract" name="page_action_contract" value="add">
									<button type="submit" name="submit_form_contract" id="submit_form_contract" class="btn btn-primary">Add</button>
									<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div_for_contract"></i>
									<i class="fa fa-check" id="contract_done" style="color:#bad23a;font-size: 20px;display:none;"></i>
								</div>
							</form>
							</div>
						</div>
					</div>
				</div>
			</section>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default close_button2" data-dismiss="modal">Close</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script type="text/javascript">
var flag_for_contract_table = 0;
$(function () {
	/*********************************** Post Out By Ajax and JSON *************************/
	var job_id = "<?=$_REQUEST['id'];?>";
	
	$('#contract_table').DataTable( {
        "processing": true,
        "serverSide": true,
		"paging": false,
		"lengthChange": false,
		"searching": false,
		"ordering": false,
		"info": false,
		"autoWidth": false,
        "ajax": "<?php echo $siteURL?>/job_contract/index?page=ajax_contract&job_id="+job_id,
        "columns": [
			{ "data": "job_id" },
			{ "data": "policy_type" },
			{ "data": "contract_status" },
			{ "data": "contract_value" },
            { "data": "renewal_date" },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforContract" class="contract_edit" alt="'+buttonID+'"><i class="fa fa-edit" style="font-size:20px;"></i></span>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="contract_remove" alt="'+buttonID+'"><i class="fa fa-trash-o" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="delete_loading_contract_'+buttonID+'"></i>';
                 }
            }
        ]
    });
	$("#contract_div").on("click",".add_new_contract",function(){
		flag_for_contract_table = 0;
		$("#add_form_contract")[0].reset();
		$("#myModalLabelAjaxContract").text("Contract");
		$("#page_action_contract").val("add");
		$("#submit_form_contract").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjaxforContract").children().find("#job_id").val(job_id);
	});	
	$("#contract_table").on("click",".contract_edit",function(){	
		$("#myModalLabelAjaxContract").text("Edit Contract");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/job_contract/index?page=ajax_contract&action=get_edit_data_contract",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					$.each(resultObj.data_array,function(data_array_key,data_array_val){
						if(data_array_key == "job_id"){
							$("#myModalAjaxforContract").children().find("#job_id").val(data_array_val);
						}
						$("#"+data_array_key).val(data_array_val);
					});
					$("#contract_id").val(id);
					$("#page_action_contract").val("update");
					$("#submit_form_contract").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#contract_table').DataTable();
	$('#contract_table tbody').on('click', '.contract_remove',function(){
		if(confirm("Do you want to delete.")){
			var contract_id = $(this).attr("alt");
			$("#delete_loading_contract_"+contract_id).show();
			var form_data   = {'contract_id':contract_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/job_contract/index?page=ajax_contract&action=delete_contract",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$("#delete_loading_contract_"+contract_id).hide();
						flag_for_contract_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjaxforContract").on("click",".close_button2",function(){
		//alert(flag_for_contract_table);
		if(flag_for_contract_table == 1){
			flag_for_contract_table = 0;
			post_out_t.draw( true );
		}
	});
	/******************************* End Post Out By Ajax and JSON *************************/
});

$(document).on('submit','form#add_form_contract',function(e){
	var contract_done = $(this).find('#contract_done');
	contract_done.hide();
	var page_load_div_for_contract = $(this).find('#page_load_div_for_contract');
	page_load_div_for_contract.show();
	var submit_form_contract = $(this).find("#submit_form_contract");
	submit_form_contract.attr("disabled", true);
	var add_form_contract = $(this);
	
	e.preventDefault(); //prevent Default functionality
	var page_action_contract = $("#page_action_contract").val();
	var flag = 0;
	if(page_action_contract == "add"){
		flag = 1;
	}
	
	var form_data = $('#add_form_contract').serialize();     
	$.ajax({
		async: false,
		type: "POST",
		url: "<?php echo $siteURL?>/job_contract/index?page=ajax_contract&action=add_contract",
		data: form_data,
		success: function(result){
			flag_for_contract_table = 1;
			//alert(flag_for_contract_table);
			if(flag == 1){
				add_form_contract[0].reset();  // Reset all form data
				var contract_message = "Contract added successfully";
			}else if(flag == 0){
				var contract_message = "Contract updated successfully";
			}
			contract_done.show();
			page_load_div_for_contract.hide();
			submit_form_contract.attr("disabled", false);
			$(".contract_message").show();
			$(".contract_message").html(contract_message);
			setTimeout(function(){
				contract_done.hide();
				$(".contract_message").hide();
			},500);
			$('.close_button2').trigger("click");
		}
	});
});
</script>