<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12">
<div id="notes_div" class="card">
	<div class="card-header">
		<h3 class="card-title">Notes</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="#myModalAjaxforNotes" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary add_notes" style="float:right;margin-right:5px;margin-bottom:5px;">
		Add Notes</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="notes_table" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Client</th>
								<th style="width:25%;">Description</th>
								<th>Consultant</th>
								<th>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="myModalAjaxforNotes" 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="myModalLabelAjaxforNotes">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 notes_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_notes" id="add_form_notes">
								<div class="col-lg-12">
									<label for="Client" class="control-label">Client</label>
									<? $gsi->getEnqQuoteJobList("job_id",$job_id,"100%",false,"form-control select2 job_id","required",$source); ?>
									
									<label for="Note Date" class="control-label">Note Date</label>
									 <input type="text" class="form-control datepicker" id="note_date" name="note_date" value="<?php (isset($note_date) && $note_date !="") ? utility::getFormatedDate($note_date) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" autocomplete="off" >
								</div>
								<div class="col-lg-12">
									<label for="Consultant" class="control-label">Consultant</label>
										<?
											// $admin_ids = "";
											// Admin::getAdminList("admin_id",$admin_id,"100%",false,"form-control","required",$admin_ids); 
										?>
										<div id="admin_id_div">
										<? 
											$admin_ids = "";
											$sql_for_admin = "SELECT * FROM job_admin_mapping WHERE job_id = $job_id";
											$rs_for_admin = mysqli_query($conn,$sql_for_admin);
											if(mysqli_num_rows($rs_for_admin) > 0){
												$c=0;
												while($row_for_admin = mysqli_fetch_assoc($rs_for_admin)){
													if($c==0){
														$default_admin = $row_for_admin["admin_id"];								
													}
													$c=$c+1;
													$admin_ids .= $row_for_admin["admin_id"].",";
												}
												$admin_ids = substr($admin_ids,0,-1);
											}
											if($c==1 && $_REQUEST['action'] != 'get_edit_data_notes'){
												$admin_id = $default_admin;
											}
											Admin::getAdminList("admin_id",$admin_id,"100%",false,"form-control","required",$admin_ids); 
										?>
									   </div>
								</div>
								<div class="col-lg-12">
									<label for="Description" class="control-label">Description</label>
									<textarea class="form-control" rows="4" id="note_text" name="note_text" placeholder="Description"><?php echo stripslashes($note_text);?></textarea>
								</div>
								
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12" style="margin-top:13px;">
									<input type="hidden" id="notes_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="">
									<input type="hidden" id="page_action_notes" name="page_action_notes" value="add">
									<button type="submit" name="submit_form_notes" id="submit_form_notes" class="btn btn-primary">Add</button>
									<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div_for_notes"></i>
									<i class="fa fa-check" id="notes_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 () {
	$("#admin_id").change(function(){	
		if($("#job_id").val() == ""){
			alert("First select Contact");
			$(this).val("");
			return false;
		}
	});	
	$("#add_form_notes").on("change","#job_id",function(){
		var job_id = $(this).val();
		if(job_id != ""){
		var form_data   = {'job_id':job_id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/notes/index.php?page=ajax_actions&action=get_admin_by_job",
				data: form_data,
				success: function(result){  
					if(result){
						$("#admin_id_div").html(result);
					}else{
						alert("No consultant assign to this contact. First assign consultant.");
						$("#admin_id_div").html(result);
					}
				}
			});
		}
	});
	/*********************************** Post Out By Ajax and JSON *************************/
	var job_id = "<?=$_REQUEST['id'];?>";
	
	$('#notes_table').DataTable( {
        "processing": true,
        "serverSide": true,
		"paging": false,
		"lengthChange": false,
		"searching": false,
		"ordering": false,
		"info": false,
		"autoWidth": false,
        "ajax": "<?php echo $siteURL?>/notes/index?page=ajax_notes&job_id="+job_id,
        "columns": [
			{ "data": "client" },
            { "data": "note_text" },
            { "data": "admin_id" },
            { "data": "note_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="#myModalAjaxforNotes" class="notes_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="notes_remove" alt="'+buttonID+'"><i class="fa fa-trash" style="font-size:20px;"></i></span><i class="fas fa-sync-alt fa-spin" style="display:none;" id="delete_loading_note_'+buttonID+'"></i>';
                 }
            }
        ]
    });
	$("#notes_div").on("click",".add_notes",function(){
		flag_for_table = 0;
		$("#add_form_notes")[0].reset();
		$("#myModalLabelAjaxforNotes").text("Notes");
		$("#page_action_notes").val("add");
		$("#submit_form_notes").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjaxforNotes").children().find("#job_id").val(job_id);
	});	
	$("#notes_table").on("click",".notes_edit",function(){	
		$("#myModalLabelAjaxforNotes").text("Edit Notes");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/notes/index?page=ajax_notes&action=get_edit_data_notes",
			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"){
							$("#myModalAjaxforNotes").children().find("#job_id").val(data_array_val);
						}
						$("#"+data_array_key).val(data_array_val);
					});
					$("#notes_id").val(id);
					$("#page_action_notes").val("update");
					$("#submit_form_notes").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#notes_table').DataTable();
	$('#notes_table tbody').on('click', '.notes_remove',function(){
		if(confirm("Do you want to delete.")){
			var notes_id = $(this).attr("alt");
			$("#delete_loading_note_"+notes_id).show();
			var form_data   = {'notes_id':notes_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/notes/index?page=ajax_notes&action=delete_notes",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$("#delete_loading_note_"+notes_id).hide();
						flag_for_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjaxforNotes").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_notes',function(e){
	var notes_out_done = $(this).find('#notes_out_done');
	notes_out_done.hide();
	var page_load_div_for_notes = $(this).find('#page_load_div_for_notes');
	page_load_div_for_notes.show();
	var submit_form_notes = $(this).find("#submit_form_notes");
	submit_form_notes.attr("disabled", true);
	var add_form_notes = $(this);
	
	e.preventDefault(); //prevent Default functionality
	var page_action_notes = $("#page_action_notes").val();
	var flag = 0;
	if(page_action_notes == "add"){
		flag = 1;
	}
	
	var form_data = $('#add_form_notes').serialize();     
	$.ajax({
		async: true,
		type: "POST",
		url: "<?php echo $siteURL?>/notes/index?page=ajax_notes&action=add_notes",
		data: form_data,
		success: function(result){
			flag_for_table = 1;
			//alert(flag_for_table);
			if(flag == 1){
				add_form_notes[0].reset();  // Reset all form data
				var notes_message = "Notes added successfully";
			}else if(flag == 0){
				var notes_message = "Notes updated successfully";
			}
			notes_out_done.show();
			page_load_div_for_notes.hide();
			submit_form_notes.attr("disabled", false);
			$(".notes_message").show();
			$(".notes_message").html(notes_message);
			setTimeout(function(){
				notes_out_done.hide();
				$(".notes_message").hide();
			},3000);
		}
	});
});
</script>