/**
 * 空调加时报表
 * 2018/4/20 by liufang
 */
$(function(){
	var ERROK = 200;
	template.config("escape", false);
	var contextPath =  $("#contextPath").val();
	var table;
	var tpl = {
			"freeTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-free-con">\
					<div class="title-wp">\
						<span>空调加时费用明细</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-free-wp">\
						<div class="free-con">\
							<table class="layui-hide" id="LAY_table_free" lay-filter="user"></table>  \
						</div>\
					</div>\
				</div>\
			</div>	'
	}
	var appSend = {
			init: function(){
				this.render();
				this.bindEvent();
			},
			render: function(){
				this.rendertable();
				
			},
			bindEvent: function(){
				var _this = this;
				layui.use('laydate', function(){
					  var laydate = layui.laydate;
//					  var
					  //执行一个laydate实例
					  laydate.render({
					    elem: '#reportTime' ,//提交开始时间
				    	type: 'month',
				    	value: new Date().getFullYear()+"-"+(new Date().getMonth()>8 ? (new Date().getMonth()+1):("0"+(new Date().getMonth()+1)))
					  });
					  
					
					});
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform input,#conditionform select").val("");
					 _this.againRenderTable();
				});
				//导出excel
				$(".export-btn").on("click",function(){
					_this.exportAirAjax();
				});
			},
			rendertable: function(){
				var _this = this;
				layui.use('table', function(){
					   table = layui.table;
					  
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user'
				    	 ,skin: 'nob' //行边框风格
			    		  ,even: true //开启隔行背景
			    		  ,size: 'lg' //小尺寸的表格
					    ,url:contextPath + "/air/reportList"
					    ,cols: [[
					      {
				    		   field:'billId', 
				    		   title: '序号', 
//				    		   width: 80,
				    		   templet: '#indexTpl',
				    	  },{
				    		  field:'virtualName',
				    		  title: '企业名称',
				    		 
			    		  },{
			    			  field:'roomNames', 
			    			  title: '房间号', 
//		    			  },{
//		    				  field:'fullName', 
//		    				  title: '企业账号', 
		    				  
	    				  },{
		    				  field:'billNo', 
		    				  title: '订单数', 
//		    				  minWidth:200
	    				  },{
		    				  field:'applyTime', 
		    				  title: '统计时间', 
//		    				  minWidth:120
	    			
	    				  },{
	    					  field:'freeTotal', 
	    					  title: '空调加时费用总计', 
//	    					  minWidth:120,
//	    					  templet: function(data){
//		    						 return "<span data-id="+data.billId+" class='check-blue-btn check-detail'>"+data.freeTotal+"<span>"
//	    					  }
	  					  },{
							  field:'id', 
							  title: '操作', 
							  fixed: "right",
							  minWidth: 150,
							  templet: function(data){
								  var str = '';
    							  str+= "<span data-virtualid="+data.virtualId+" data-reporttime="+data.applyTime+" class='check-blue-btn  check-btn'>费用明细<span>"
	    						 return str;
	    					  }
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						   
						    method: 'post',
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
						    	where:{
						    		reportTime: new Date().getFullYear()+"-"+(new Date().getMonth()>8 ? (new Date().getMonth()+1):("0"+(new Date().getMonth()+1)))
						    	},
					    	request: {
					    	    pageName: 'pageNo' //页码的参数名称，默认：page
					    	    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
					    	  }	,
						    done: function(res, curr, count){
						        //如果是异步请求数据方式，res即为你接口返回的信息。
						    	//查看费用明细
						    	$(".check-btn").on("click",function(){
						    		var reportTime = $(this).data("reporttime");
						    		var virtualId = $(this).data("virtualid");
						    		_this.showFreeDetailDialog(reportTime,virtualId);
						    	})
						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  _this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					where: {
						reportTime: $("#conditionform #reportTime").val(),
						companyName: $("#conditionform .companyName").val(),
						
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				}); 
			},
			
			/**
			 * 导出订单汇总请求
			 * @param orgId 校区id
			 * @param callback
			 */
			exportAirAjax: function(){
				var _this = this;
				$.ajax({
	                type: "post",
	                url: contextPath + "/air/exportReportList",
	                data:{
	                	reportTime: $("#conditionform #reportTime").val(),
						companyName: $("#conditionform .companyName").val()
	                },
	                success: function (data) {
	                    
	                    if (data.code == ERROK) {
//	                    	location.href = data.data;
	                    	location.href =  $("#contextPath").val() + "/upload/AirAddTime.xls";
	                    } else {
	                        layer.msg(data.msg)
	                    }
	                },
	                error: function (error) {
	                    layer.msg('error')
	                }
	            });
			},
			/**
			 * 查看费用明细
			 * @param reportTime 月份
			 * @param virtualId 企业id
			 */
			showFreeDetailDialog: function(reportTime,virtualId){
				var _this = this;
				$("body").append(template.render(tpl.freeTpl));
				//关闭弹窗
				$(".close-btn").on("click",function(){
					$(".dialog-wp").remove();
				});
				_this.renderFreeDetailTable(reportTime,virtualId)
			},
			/**
			 * 渲染table
			 * @param reportTime 月份
			 * @param virtualId 企业id 
			 */
			renderFreeDetailTable: function(reportTime,virtualId){
				var _this = this;
				var freeTable;
				layui.use('table', function(){
					freeTable = layui.table;
					  
					  //方法级渲染
					freeTable.render({
					    elem: '#LAY_table_free'
					    ,url:contextPath + "/air/reportDetailList"
					    ,skin:"nob"
					    ,cols: [[
					      {
				    		  field:'code',
				    		  title: '订单号',
				    		  width:180
			    		  },{
		    				  field:'billSource', 
		    				  title: '订单来源', 
//		    				  minWidth:50,
		    				  templet: function(data){
	    						  if(data.billSource == 1){
	    							  return "空调加时"
	    						  }else if(data.billSource == 2){
	    							  return '预定会议室'
	    						  }
	    					  }
	    				  },{
		    				  field:'roomNames', 
		    				  title: '申请房号', 
//		    				  minWidth:50
	    				  },{
		    				  field:'applyArea', 
		    				  title: '申请面积(m²)', 
//		    				  minWidth:100
	    				  },{
		    				  field:'useDisplayTime', 
		    				  title: '使用时间', 
		    				  width: 230
	    				  },{
		    				  field:'useLengthTime', 
		    				  title: '使用时长(小时)', 
		    				  width:180
	    				  },{
		    				  field:'priceDisplay', 
		    				  title: '单价', 
		    				  width:180
	    				  },{
		    				  field:'applyUser', 
		    				  title: '申请人', 
//		    				  minWidth:50
	    				  },{
		    				  field:'freeTotal', 
		    				  title: '订单金额', 
//		    				  minWidth:50
	    				  }
						  ]]
						    ,id: 'testReload'
						    ,page: false,
						    method: 'post',
						    even: true,
						    where:{
						    	reportTime: reportTime,
						    	virtualId: virtualId
						    },
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
					    	request: {
					    	    pageName: 'pageNo' //页码的参数名称，默认：page
					    	    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
					    	  }	,
						   
					  })
					});
			}
	};
	appSend.init();
})