<?php $ci = get_instance();?>
<!DOCTYPE html>
<html lang="en">
<head>
	<?php $this->load->view("includes/header");?>
</head>

<body>
	<?php $this->load->view("includes/menu");?>
	
	<section class="content">
		<?php $this->load->view("includes/nav");?>
		
		<div class="wraper container-fluid xs_container-fluid">
<!--			<div class="page-title"> -->
<!--				<h3 class="title">Panels</h3> -->
<!--			</div>-->
			
			<div class="row">
				<div class="col-md-12">
					<?php $this->load->view("includes/success_msg");?>
					
					<div class="panel panel-default xs_panel" style="padding-left: 10px; padding-right: 10px;">
						<div class="panel-heading">
							<center>
								<h3 class="panel-title">
									<i class="fa fa-history fa-fw"></i>
									Audit Trail - Invoice
								</h3> 
							</center>
							
						</div>
						
						<div class="panel-body">
							<form id="form" action="" method="GET">
								<table class="FG_table" style="width: 60%; margin: 0 auto;">
									<tr>
		      							<td colspan="2" style="width: 50%;"></td>
		      							<td colspan="2" style="width: 50%;"></td>
		      						</tr>
		      						<tr>
										<th class="FG_th"><span style="color: red;">*</span> Category: </th>
										<td class="FG_td form-group" colspan="5">
											<div class="has-feedback">
												<select name="CATEGORY" id="category" onChange="change_category();" class="form-control select2" required="required">
													<option value="">--- Select a value ---</option>
													<option value="at_invoice">Invoice</option>
													<option value="at_invitem">Invoice Sub-Item</option>
												</select>
												<span class="form-control-feedback" style="right: 15px;">
												<i class="fa fa-spinner fa-pulse resource_loader"></i>
											</span>
										</div>
										</td>
									</tr>
									<tr id="date">	
										<th class="FG_th"><span style="color: red;">*</span> Date:</th>
										<td class="FG_td">
											<div class="has-feedback" style="">
												<input type="text" name="DATE_FROM" value="<?php echo $this->input->get('DATE_FROM') != ""? date("Y-m-d", strtotime($_GET['DATE_FROM'])):date("Y-m-d");?>" class="form-control datepicker" style="padding-right: 30px;">
												<span class="fa fa-calendar form-control-feedback" style="top: 10px;"></span>
											</div>
										</td>
										<td style="padding: 0px 7px 0px 7px; text-align: center;">To </td>
										<td class="FG_td">
											<div class="has-feedback" style="">
												<input type="text" name="DATE_TO" value="<?php echo $this->input->get('DATE_TO') != ""? date("Y-m-d", strtotime($_GET['DATE_TO'])):date("Y-m-d");?>" class="form-control datepicker" style="padding-right: 30px;">
												<span class="fa fa-calendar form-control-feedback" style="top: 10px;"></span>
											</div>
										</td>
									</tr>	
								<tr id="user">
									<th class="FG_th">User:</th>
										<td class="FG_td form-group" colspan="5">
											<div class="has-feedback">
												<select name="USER[]" class="form-control select2" placeholder="All" multiple></select>
												<span class="form-control-feedback" style="right: 15px;">
													<i class="fa fa-spinner fa-pulse resource_loader"></i>
												</span>
											</div>
										</td>
									</tr>
		      						<tr>
				      					<th class="FG_th">Font Size:</th>
				      					<td class="FG_td">
				      						<div class="input-group" style="max-width: 130px;">
						      					<input type="text" name="FONT_SIZE" value="<?php echo (double)$this->input->get('FONT_SIZE') > 0? (double)$_GET['FONT_SIZE']:12;?>" class="form-control" style="text-align: right;" autocomplete="OFF">
						      					<span class="input-group-addon">px</span>
						      				</div>
				      					</td>
		      						</tr>
		      					</table>
		      				
										
								<table class="FG_table" style="width: 100px; margin: 0 auto; padding-top: 30px;">
									<tr>
										<td style="padding-right:5px;" class="FG_td">
											<button type="submit" id="edit_btn_submit" class="btn btn-primary btn-xs-block btn_submit" style="min-width: 80px;">
												<i class=""></i>
												Generate
											</button>
										</td>
										<td style="padding-right:5px;" class="FG_td">
											<a href="javascript: browser_print('#ajax_container');" id="btn_print" class="btn btn-primary" style="margin-left: 10px; min-width:82px;">
											<i class="fa fa-print"></i>
											Print
											</a>
										</td>
									</tr>
								</table>
								</form>
							
							<div id="ajax_container" style="margin-top: 20px;"><!-- AJAX --></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php $this->load->view("includes/footer");?>
	</section>
	
	<script type="text/javascript">
		$(document).ready(function(){
			
			$(".datepicker").datepicker({
				format: "yyyy-mm-dd",
				autoclose: true,
				todayHighlight: true,
			});

//			$("#form [name='BILLING_DATE_FROM']").datepicker({
//				format: "yyyy-mm-dd",
//				autoclose: true,
//				todayHighlight: true,
//			});
//
//			$("#form [name='BILLING_DATE_TO']").datepicker({
//				format: "yyyy-mm-dd",
//				autoclose: true,
//				todayHighlight: true,
//			});

			$("#fontSize").val(<?php echo $this->input->get('FONTSIZE')?>);
			$("#appointment").val("<?php echo $this->input->get('APPOINTMENT')?>");
			
			$(".select2").select2();
			$("#category").select2("val", "<?php echo $this->input->get("CATEGORY")?>");
			
			json_resource(function(){
				load_data();
			});
			
			change_category();
		});

		function change_category(){
			var category_id = $("#category").val();
			clear_params(function(){
				switch(category_id){
					case "at_invoice":
						$("#date").show("fast");
						$("#users").show("fast");
						//$("#row_appointment").show("fast");
						//$("#reserve_date").show("fast");
						break;
					case "at_invitem":
						$("#date").show("fast");
						$("#users").show("fast");
						//$("#row_appointment").show("fast");
						//$("#reserve_date").show("fast");
						break;

				}
			});
		}
		
		function clear_params(callback){
			$("#date").hide("fast");
			$("#users").hide("fast");
			//$("#row_appointment").hide("fast");
			//$("#reserve_date").hide("fast");
			
			if(callback != undefined) callback();
		}

		function json_resource(callback){
			$(".resource_loader").show("fast");

			var params = "";
			params += "&t[]=WEB_USER";
			params += "&c[]=<T>.CODE IS NOT NULL AND <T>.CODE != ''";
			params += "&s[]=<T>.CODE";
			
			var site = "<?php echo base_url("api/get_resource");?>";
			site += "?" + params;

			$.getJSON(encodeURI(site), function(result){
				if(result != ""){

					//WEB USER---
					<?php $field = "USER[]";?>
					var selector = "#form [name='<?php echo $field;?>']";
					var string = "";
					$(result.WEB_USER).each(function(index, value){
						string += "<option value='" + value.PKKEY + "'>" + value.NAME + "</option>";
					});
					$(selector).html(string);

					var value = [];
					<?php if(is_array($this->input->get(str_replace("[]", "", $field)))):?>
						<?php foreach($this->input->get(str_replace("[]", "", $field)) as $v):?>
							value.push("<?php echo $v;?>");
						<?php endforeach;?>
					<?php endif;?>
					$(selector).select2("val", value);
					//----------
				}
			})
			.done(function(){
			})
			.fail(function(){
			})
			.always(function(){
				$(".resource_loader").hide("fast");
				if(callback != undefined) callback();
			});
		}
		
		//Global Variables---
		var timer_check_unique;
		var checking_unique;

		var row_n = 1;
		var timer_filter;
		//-------------------
	
		function load_data(callback){
			//Loader---
			$("#ajax_container").html("<?php echo $this->text_ajax_loader;?>");
			//---------
			
			var fontsize = parseFloat($("#fontSize").val());
			
			var params = "";
//			params += "&DATE_FROM=" + $("#DATE_FROM").val();
//			params += "&DATE_TO=" + $("#DATE_TO").val();
//			params += "&TIME_FROM=" + $("#timeFrom").val();
//			params += "&TIME_TO=" + $("#timeTo").val();
			params += "&FONTSIZE=" + $("#fontSize").val();
			//params += "&APPOINTMENT=" + $("#appointment").val();
//			params += "&START_DATE=" + $("#startDate").val();
//			params += "&MOVEMENT_DATE=" + $("#movementDate").val();
//			params += "&ADJ_DATE=" + $("#adjDate").val();
//			params += "&START_DATE=" + $("#startDate").val();
//			params += "&TNKNOS=" + $("#tnkNos").val();
//			params += "&TNKNO=" + $("#tnkNo").val();
			
			$("#form [name]").each(function(){
				var name = $(this).attr("name");
				var value = $(this).val();
				
				if($(this).is(":checkbox")){
					if($(this).is(":checked")) params += "&" + name + "=" + value;
				}
				else{
					if($.isArray(value)){
						$(value).each(function(x, v){
							if(v != null){
								params += "&" + name + "=" + v;
							}
						});
					}
					else{
						if(value != null){
							params += "&" + name + "=" + value;
						}
					}
				}
			});
			
			$.ajax({
				type: "POST",
				url: "<?php echo base_url($this->router->fetch_class());?>/load_data?<?php echo $_SERVER['QUERY_STRING'];?>",
				data: params,
				success: function(result, status, xhr){
					$("#ajax_container").html(result);
					enable_btn("#btn_print");
				},
				error: function(xhr,status,error){
					$("#ajax_container").html("<?php echo $this->text_ajax_fail;?>");
				},
				complete: function(xhr,status){
					enable_btn("#btn_generate");
				},
			});
		}

	</script>
</body>
</html>
