<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12">

<div id="reminder_div" class="card">
	<div class="card-header">
		<h3 class="card-title">Reminders</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="#myModalAjaxReminder" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary add_new_reminder" style="float:right;margin-right:5px;margin-bottom:5px;">
		Add Reminder</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="reminder_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>Time</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="myModalAjaxReminder" 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="myModalLabelAjaxReminder">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 reminder_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_reminder" id="add_form_reminder">
								<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 class="col-lg-12">
									<label for="Consultant" class="control-label">Consultant</label>
										<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_reminder'){
												$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="Date" class="control-label">Date</label>
									 <input type="text" class="form-control datepicker" id="reminder_date" name="reminder_date" value="<?php (isset($reminder_date) && $reminder_date !="") ? utility::getFormatedDate($reminder_date) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" autocomplete="off" >
									 
									 <label for="Time" class="control-label">Time</label>
									<div class="bootstrap-timepicker">
										<? utility::getTimeList("reminder_time", $reminder_time, "Time", "100%","form-control");?>
									</div>
								</div>								
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12" style="margin-top:13px;">
									<input type="hidden" id="reminder_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="<?=$job_id;?>">
									<input type="hidden" id="page_action_reminder" name="page_action_reminder" value="add">
									<button type="submit" name="submit_form_reminder" id="submit_form_reminder" class="btn btn-primary">Add</button>
									<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div_for_reminder"></i>
									<i class="fa fa-check" id="reminder_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"];?>";
	
	$('#reminder_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_reminder&job_id="+job_id,
        "columns": [
			{ "data": "client" },
			{ "data": "note_text" },
			{ "data": "admin_id" },
            { "data": "note_date" },
            { "data": "time" },
			{
				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="#myModalAjaxReminder" class="reminder_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="reminder_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_reminder_'+buttonID+'"></i>';
                 }
            }
        ]
    });
	$("#reminder_div").on("click",".add_new_reminder",function(){
		flag_for_table = 0;
		$("#add_form_reminder")[0].reset();
		$("#myModalLabelAjaxReminder").text("Reminder");
		$("#page_action_reminder").val("add");
		$("#submit_form_reminder").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjaxReminder").children().find("#job_id").val(job_id);
	});	
	$("#reminder_table").on("click",".reminder_edit",function(){	
		$("#myModalLabelAjaxReminder").text("Edit Reminder");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/notes/index?page=ajax_reminder&action=get_edit_data_reminder",
			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"){
							$("#myModalAjaxReminder").children().find("#job_id").val(data_array_val);
						}else if(data_array_key == "reminder_time"){
							$("#myModalAjaxReminder").children().find("#"+data_array_key).select2("val",data_array_val);
						}
						$("#myModalAjaxReminder").children().find("#"+data_array_key).val(data_array_val);
					});
					$("#reminder_id").val(id);
					$("#page_action_reminder").val("update");
					$("#submit_form_reminder").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#reminder_table').DataTable();
	$('#reminder_table tbody').on('click', '.reminder_remove',function(){
		if(confirm("Do you want to delete.")){
			var reminder_id = $(this).attr("alt");
			$("#delete_loading_reminder_"+reminder_id).show();
			var form_data   = {'reminder_id':reminder_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/notes/index?page=ajax_reminder&action=delete_reminder",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$("#delete_loading_reminder_"+reminder_id).hide();
						flag_for_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjaxReminder").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_reminder',function(e){
	var reminder_done = $(this).find('#reminder_done');
	reminder_done.hide();
	var page_load_div_for_reminder = $(this).find('#page_load_div_for_reminder');
	page_load_div_for_reminder.show();
	var submit_form_reminder = $(this).find("#submit_form_reminder");
	submit_form_reminder.attr("disabled", true);
	var add_form_reminder = $(this);
	
	e.preventDefault(); //prevent Default functionality
	var page_action_reminder = $("#page_action_reminder").val();
	var flag = 0;
	if(page_action_reminder == "add"){
		flag = 1;
	}
	
	var form_data = $('#add_form_reminder').serialize();     
	$.ajax({
		async: true,
		type: "POST",
		url: "<?php echo $siteURL?>/notes/index?page=ajax_reminder&action=add_reminder",
		data: form_data,
		success: function(result){
			flag_for_table = 1;
			//alert(flag_for_table);
			if(flag == 1){
				add_form_reminder[0].reset();  // Reset all form data
				var reminder_message = "Reminder added successfully";
			}else if(flag == 0){
				var reminder_message = "Reminder updated successfully";
			}
			reminder_done.show();
			page_load_div_for_reminder.hide();
			submit_form_reminder.attr("disabled", false);
			$(".reminder_message").show();
			$(".reminder_message").html(reminder_message);
			setTimeout(function(){
				reminder_done.hide();
				$(".reminder_message").hide();
			},3000);
		}
	});
});
</script>