<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12">
<div id="post_out_div" class="card">
	<div class="card-header">
		<h3 class="card-title">Post Out</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="#myModalAjax" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary post_out" style="float:right;margin-right:5px;margin-bottom:5px;">
					Add Post Out</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="post_out_table" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Project</th>
								<th>Post Type</th>
								<th>Date</th>
								<th>Time</th>
								<th>Graduation</th>
								<th>Gender</th>
								<th>Method</th>
								<th>Recipient</th>
								<th>Notes</th>
								<!--<th>Callback Request</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="myModalAjax" 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="box box-primary">
							<div class="box-body">
							<form action="" method="post" name="add_form" id="add_form">
								<div class="col-lg-12">
									<label for="Project" class="control-label">Project</label>
									<? echo $gsi->getOrganisationList("project_id",$project_id,"100%",true,"form-control select2","",$job_id); ?>
									
									<label for="Post Type" class="control-label">Post Type</label>
									<? echo Keycode::getKeycodeListValue("post_type", $post_type, $width = "100%", "Post Type", "id", "code", "post_type","","form-control select2",false,"required"); ?>
									
									<label for="Post Out Date" class="control-label">Post Out Date</label>
									 <input type="text" class="form-control datepicker" id="post_out_date" name="post_out_date" value="<?php (isset($post_out_date) && $post_out_date !="") ? utility::getFormatedDate($post_out_date) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="Post Out Date" autocomplete="off">
									 
									<label for="Post Out Date" class="control-label">Post Out Time</label>
									<div class="bootstrap-timepicker">
										<input type="time" id="post_out_time" name="post_out_time" class="form-control timepicker" value="<?php echo $post_out_time;?>" placeholder="Post Out Time" autocomplete="off">
									</div>
								</div>
								<div class="col-lg-12">
									<label for="Posting Method" class="control-label">Posting Method</label>
									<? echo Keycode::getKeycodeListValue("posting_method", $posting_method, $width = "100%", "Posting Method", "id", "code", "posting_method","","form-control"); ?>
								
									<label for="Recipient" class="control-label">Recipient</label>
									<? echo Keycode::getKeycodeListValue("recipient", $recipient, $width = "100%", "Recipient", "id", "code", "recipient","","form-control"); ?>
									
									<label class="control-label" for="Graduation">Graduation</label><br>
									<input type="checkbox" class="flat-red" id="graduation" name="graduation" value="1" <? echo ($graduation == 1) ? "checked" : ""; ?>>
								</div>
								<div class="col-sm-6">
									<label for="Notes" class="control-label">Notes</label>
									<textarea class="form-control" rows="4" id="post_out_notes" name="post_out_notes" placeholder="Post Out Notes"><?php echo stripslashes($post_out_notes);?></textarea>
									
									<label class="control-label" for="Gender">Gender</label><br>
									Male <input type="radio" name="gender" value="0" id="gender_male" class="flat-red gender" <? echo ($gender == 0) ? "checked" : ""; ?>>
									Female <input type="radio" name="gender" value="1" id="gender_female" class="flat-red gender" <? echo ($gender == 1) ? "checked" : ""; ?>>
								</div>
								
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12">
									<input type="hidden" id="post_out_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="">
									<input type="hidden" id="page_action_post_out" name="page_action_post_out" value="add">
									<button type="submit" name="submit_form_post_out" id="submit_form_post_out" class="btn btn-primary">Add</button>
									<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div_for_post_out"></i>
									<i class="fa fa-check" id="post_out_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_table = 0;
$(function () {
	
	/*********************************** Post Out By Ajax and JSON *************************/
	var job_id = "<?=$_REQUEST["id"];?>";
	
	$('#post_out_table').DataTable( {
        "processing": true,
        "serverSide": true,
		"paging": false,
		"lengthChange": false,
		"searching": false,
		"ordering": false,
		"info": false,
		"autoWidth": false,
        "ajax": "<?php echo $siteURL?>/enquiry_management/index?page=ajax_post_out&job_id="+job_id,
        "columns": [
			{ "data": "project" },
            { "data": "post_type" },
            { "data": "post_out_date" },
            { "data": "post_out_time" },
            { "data": "graduation" },
            { "data": "gender" },
            { "data": "posting_method" },
            { "data": "recipient" },
            { "data": "post_out_notes" },
			{
				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="#myModalAjax" class="post_out_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="post_out_remove" alt="'+buttonID+'"><i class="fa fa-fw fa-trash" style="font-size:20px;"></i></span>';
                 }
            }
        ]
    });
	$("#post_out_div").on("click",".post_out",function(){
		flag_for_table = 0;
		$("#add_form")[0].reset();
		$("#myModalLabelAjax").text("Post Out");
		$("#page_action_post_out").val("add");
		$("#submit_form_post_out").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjax").children().find("#job_id").val(job_id);
	});	
	$("#post_out_table").on("click",".post_out_edit",function(){	
		$("#myModalLabelAjax").text("Edit Post Out");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/enquiry_management/index?page=ajax_post_out&action=get_edit_data_post_out",
			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"){
							$("#myModalAjax").children().find("#job_id").val(data_array_val);
						}else if(data_array_key == "project_id"){
							result = data_array_val.split(',');
							//alert(result[0]);
							setTimeout(function(){
								$("#"+data_array_key).select2("val", result);
							},100);
						}else if(data_array_key == "post_type"){
							$("#"+data_array_key).select2("val",data_array_val);
						}else if(data_array_key == "graduation"){
							if(data_array_val == 1){
								$("#"+data_array_key).iCheck('check');
							}
						}else if(data_array_key == "gender"){
							if(data_array_val == 1){
								$("#gender_female").iCheck('check');
							}else{
								$("#gender_male").iCheck('check');
							}
						}
						$("#"+data_array_key).val(data_array_val);
					});
					$("#post_out_id").val(id);
					$("#page_action_post_out").val("update");
					$("#submit_form_post_out").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#post_out_table').DataTable();
	$('#post_out_table tbody').on('click', '.post_out_remove',function(){
		if(confirm("Do you want to delete.")){
			var post_out_id = $(this).attr("alt");
			var form_data   = {'post_out_id':post_out_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/enquiry_management/index?page=ajax_post_out&action=delete_post_out",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						flag_for_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjax").on("click",".close_button2",function(){
		//alert(flag_for_table);
		if(flag_for_table == 1){
			flag_for_table = 0;
			post_out_t.draw( true );
		}
	});
	/******************************* End Post Out By Ajax and JSON *************************/
});

$(document).on('submit','form#add_form',function(e){
	var post_out_done = $(this).find('#post_out_done');
	post_out_done.hide();
	var page_load_div_for_post_out = $(this).find('#page_load_div_for_post_out');
	page_load_div_for_post_out.show();
	var submit_form_post_out = $(this).find("#submit_form_post_out");
	submit_form_post_out.attr("disabled", true);
	var add_form = $(this);
	
	e.preventDefault(); //prevent Default functionality
	var page_action_post_out = $("#page_action_post_out").val();
	var flag = 0;
	if(page_action_post_out == "add"){
		flag = 1;
	}
	
	var form_data = $('#add_form').serialize();     
	$.ajax({
		async: true,
		type: "POST",
		url: "<?php echo $siteURL?>/enquiry_management/index?page=ajax_post_out&action=add_post_out",
		data: form_data,
		success: function(result){
			flag_for_table = 1;
			//alert(flag_for_table);
			if(flag == 1){
				var frm = add_form[0];
				for (var i=0; i < frm.length; i++){
					if (frm[i].getAttribute('id') == 'project_id' || frm[i].getAttribute('id') == 'post_type'){
						$("#"+frm[i].getAttribute('id')).select2("val","");
					}else if(frm[i].getAttribute('id') == 'graduation'){
						$("#"+frm[i].getAttribute('id')).iCheck('uncheck');
					}
				}
				add_form[0].reset();  // Reset all form data
			}
			post_out_done.show();
			page_load_div_for_post_out.hide();
			submit_form_post_out.attr("disabled", false);
			setTimeout(function(){
				post_out_done.hide();
			},500);
		}
	});
});
</script>