$(document).ready(function() {
	load();
})

function load(){
	var heightTemp=screen.height;
	var length;
	if(heightTemp==768){
		length=8;
	}else{
		length=11;
	}
	$.ajax({
		method : "post",
		url : "/pqs9900/device/loadpushInfo",
		data : {},
		beforeSend: function () {  
            i = ityzl_SHOW_LOAD_LAYER();  
        },
        error:function(data) {
			//***********关闭loading
        	ityzl_CLOSE_LOAD_LAYER(i); 
        },
		success : function(data) {
			var info = JSON.parse(data);
			var table=$('#nodelist').DataTable({
				scrollX : true,
				lengthChange : false,
				destroy : true,
				searching: false,
				
				language : {
					url : '../json/chine.json'
				},
				pageLength:length,
				data : info,
			    "columnDefs" : [{
//				    // 定义操作列,######以下是重点########
			    	"targets" : 7,//操作按钮目标列
				    "data" : null,
				    "orderable" : false,
			    	"render" : function(row) {
						return null;
			    	}
				    }],
			    "createdRow": function ( row, data, index ) {
			    	$btnView = $('<button type="button" class="btn themeBGColor glyphicon glyphicon-edit" id="btn_view">推送</button>');
			    	$btnExp = $('<button type="button" class="btn btn-danger glyphicon glyphicon-trash" id="btn_exp" style="margin-left:10px">删除</button>');
			    	if(data[5] == "未推送"){
				    	$('td', row).eq(7).append($btnView,$btnExp);
			    	}else{
			    		$('td', row).eq(7).append("无");
			    	}
			    },
			    'fnDrawCallback': function(table) {    
				    $("#nodelist_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");    
				    var oTable = $("#nodelist").dataTable(); 
				    $('#dataTable-btn').click(function(e) {
				        if($("#changePage").val() && $("#changePage").val() > 0) {    
				            var redirectpage = $("#changePage").val() - 1;    
				        } else {    
				            var redirectpage = 0;    
				        }    
				        oTable.fnPageChange(redirectpage);
				    });    
				}
			});
			new $.fn.dataTable.Buttons($("#nodelist").dataTable(), {
				buttons: [
					 {
				            extend: 'csv',//导出格式
				            title:'前置机列表',//导出csv名称
				            exportOptions:{ 
					        	columns :[1,2,3,4,5,6,7],//导出需要的列
					        	format:{
					        		body:function(data,rID,cID){//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
					        				return data;
					        		}
					        	}
					        }
				     }
		        ]
			} );
			
			table.buttons().container().css("display","inline-block");
			table.buttons().container().css("float","right");
			table.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
			table.buttons().container().appendTo('.header');			

			//***********关闭loading
        	ityzl_CLOSE_LOAD_LAYER(i); 
		}
	}); 
}

$('#nodelist').on("click", "#btn_view", function() {
	objTable=$('#nodelist').DataTable();
	if (objTable != null) {
		var item = objTable.row($(this).closest('tr')).data();
		var nodeIndex = item[0];
		
		layer.open({
			  type: 2,
			  title: '修改前置机信息',
			  shadeClose: true,
			  area : ['380px', '300px'],
	          offset: '0px',
			  maxmin : false,
			  content: 'modnode?nodeIndex='+nodeIndex, 
			  scrollbar: false
		});
	}
});  

$('#nodelist').on("click", ".btn-danger", function() {
	objTable=$('#nodelist').DataTable();
	if (objTable != null) {
		var item = objTable.row($(this).closest('tr')).data();
		var nodeIndex = item[0];
		
		layer.confirm('是否确认？',function(){
			$.ajax({
				method : "post",
				url : "/pqs9900/device/delNodeInfo",
				data : {
					nodeIndex:nodeIndex
				},
				async:false,
				success : function(data) {
					if(data.code==200){
						layer.msg('删除成功!',{icon:1,time:1000});
						load();
					}else{
						layer.msg('删除失败！',{icon:2,time:1000});
					}
				}
			})
		})
	}
})

$("#addnode").click(function(){
	layer.open({
		  type: 2,
		  title: '新增前置机',
		  shadeClose: true,
          offset: '0px',
		  area: ['380px', '300px'],
		  maxmin : false,
		  content: 'addnodeinfo', 
		  scrollbar: false,
		  end:function(){
			  load();
		  }
		});
})