<?php
$collapse_add_area = "collapse";
$where 	= " WHERE 1=1 AND note_type = 1 ";
$query_string = "";
$button_value_reminder 	= "Add";
$table_name		= "notes";
$next_page_condition 	= "";
$paging ="";
$page_action_reminder 	= "add";
if($_REQUEST['id']){
	$job_id = $_REQUEST['id'];
	$where 	.= " AND job_id = $job_id ";
	$next_page_condition.="&job_id=".$job_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}


###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; 
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Reminder Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY note_date DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);
?>

<section class="content-header">
  <h1>
	<? echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>		
</section>

	<section class="content">
		<div class="row">
			<div class="col-xs-12">
				<div class="box">

				<div class="box-body">
				<div class="row" style="padding:0px 10px;">
					<?php include_once('add_reminder_for_clients.inc.php');?>
				</div>
				<div class="table-responsive">
					<table id="reminder_for_clients_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>
				</div>
			</div>
		</div>
	</section>
<script type="text/javascript">
$(document).ready(function(){
	$('#add_reminder_for_clients').click(function(){
		$('#reminder_for_clients_area').slideToggle();
		var frm = document.getElementsByName('add_reminder_for_clients_form')[0];
		frm.reset();
		$("#page_action_reminder").val("add");
		$("#submit_form_reminder").html("Add");
	});
	
	$('#reminder_for_clients_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_for_clients&id=<?= $_REQUEST['id'];?>",
        "columns": [
            { "data": "client" },
			{ "data": "description" },
			{ "data": "consultant" },
            { "data": "date" },
            { "data": "time" },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="reminder_for_clients_edit" alt="'+buttonID+'"><i class="fa fa-edit" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="edit_reminder'+buttonID+'"></i>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="reminder_for_clients_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_reminder_'+buttonID+'"></i>';
                 }
            }
        ]
	});
	
	var reminder_for_clients_t = $('#reminder_for_clients_table').DataTable();
	$('#reminder_for_clients_table tbody').on('click', '.reminder_for_clients_edit',function(){
		/*$("html,body").animate({
			scrollTop: $("#reminder_for_clients_area").offset().top - 110},
		'slow');*/
		
		var reminder_for_clients_id = $(this).attr("alt");
		$("#edit_reminder"+reminder_for_clients_id).show();
		var form_data   = {'reminder_for_clients_id':reminder_for_clients_id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/notes/index?page=ajax_reminder_for_clients&action=edit_reminder_for_clients",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				console.log(resultObj);
				if(resultObj.status == "1"){
					var time=resultObj.reminder_time;
					var admin=resultObj.admin_id; 
					$('#reminder_for_clients_area').slideDown();
					$("#reminder_text").val(resultObj.reminder_text);
					$("#r_admin_id").val(resultObj.admin_id);
					$("#reminder_date").val(resultObj.reminder_date);
					$("#reminder_time").select2("val", time);
					
					$("#edit_reminder"+reminder_for_clients_id).hide();
					$("#page_action_reminder").val("update");
					$("#reminder_id").val(reminder_for_clients_id);
					$("#submit_form_reminder").html("Update");
				}
			}
		});
	});
	
	$('#reminder_for_clients_table tbody').on('click', '.reminder_for_clients_remove',function(){
		var reminder_for_clients_edit_id = $("#id").val();
		var reminder_for_clients_id = $(this).attr("alt");
		$("#delete_reminder_"+reminder_for_clients_id).show();
		if(reminder_for_clients_edit_id == reminder_for_clients_id){
			alert("This Id open in edit mode so you can not delete it.");
			return false;
		}
		if(confirm("Do you want to delete.")){
			var form_data   = {'reminder_for_clients_id':reminder_for_clients_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/notes/index?page=ajax_reminder_for_clients&action=delete_reminder_for_clients",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$('#delete_reminder_'+reminder_for_clients_id).hide();
						reminder_for_clients_t.draw( false );
					}
				}
			});	
		}
	});
	
	$(document).on('submit','form#add_reminder_for_clients_form',function(e){
		$(this).find('#reminder_for_clients_done').hide();
		$(this).find('#page_load_div_for_reminder_for_clients').show();
		$(this).find("#add_master_data").attr("disabled", true);
		
		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 = $('form').serialize();     
		$.ajax({
			async: true,
			type: "POST",
			url: site_url+"/modules/notes/index.php?page=ajax_reminder_for_clients&action=add_reminder_for_clients",
			data: form_data,
			success: function(result){ 
				if(flag == 0){
					$("#page_action_reminder").val("add");
					$("#submit_form_reminder").html("Add");
					//$('#reminder_for_clients_area').slideToggle();
				}
				$("#id").val("");
				//$("#job_id").val("");
				var frm = document.getElementsByName('add_reminder_for_clients_form')[0];
				for (var i=0; i < frm.length; i++){
					if (frm[i].getAttribute('id') == 'reminder_for_clients_id'){
						$("#"+frm[i].getAttribute('id')).select2("val","");
					}else if(frm[i].getAttribute('class') == 'checkbox_div flat-red section_checkbox'){
						$(".section_checkbox").iCheck('uncheck');
					}
				}
				frm.reset();  // Reset all form data	
					
				$('#reminder_for_clients_done').show();
				$('#page_load_div_for_reminder_for_clients').hide();
				$("#add_master_data").attr("disabled", false);
				
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					reminder_for_clients_t.draw( false );
				}
				setTimeout(function() {
					$('#reminder_for_clients_done').hide();
				},1000);
				setTimeout(function() {
					$('#add_reminder_for_clients').trigger("click");
				},1000);
			}
		});
	}); 
	
	/* $("#search_for_reminder_for_clients").on("keyup", function() {
		var value = $(this).val().toLowerCase();
		$("#reminder_for_clients_table tbody tr").filter(function() {
		  $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
		});
	}); */
});	
</script>		