var windowId, detailWindowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;

$(function() {
	initGrid();//加载列表
});

function initGrid(){
	var toolbar = [{
        text:'查询',
        iconCls:'icon-search',
        handler:function(){doSearch();}
    },{
        text:'增加',
        iconCls:'icon-add',
        handler:function(){createUserInfo();}
    },
//    {
//        text:'修改',
//        iconCls:'icon-edit',
//        handler:function(){doEdit()}
//    },
    {
        text:'删除',
        iconCls:'icon-info-delete',
        handler:function(){deleteUserInfo();}
    },{
        text:'导出',
        iconCls:'icon-export',
        handler:function(){doExport();}
    }];
	$('#searchGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		url : 'coptableAction!getSystemUserList.do',
			columns : [ [ { 
				field: 'BId',
				checkbox:'true',
				width:50
			},{
				field : 'BIANHAO',
				title : '用户标识',
				hidden: false,
				width : 100,
				formatter:function formatFdIdChannelId(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick=editUserInfo("+ num +");>"+record.BIANHAO +"</a>";
			}
			},{
				field : 'XINGMING',
				title : '用户名称',
				width : 150
			},{
				field : 'DWMC',
				title : '单位名称',
				hidden: false,
				width : 250
			},{
				field : 'SHOUJI',
				title : '手机号码',
				width : 200
			},{
				field : 'BGSNXDH',
				title : '办公室内线电话',
				width : 120
			}] ]
		});
}

function doSearch() {
	$('#searchGrid').datagrid('load', {
		adminName : $('#adminName').textbox('getValue'),
		telphone  : $('#telphone').textbox('getValue'),
		phone     : $('#phone').textbox('getValue')
	});
}

function saveNodeInfo(){
	var frame	=	top.$("#"+windowId).children('iframe')[0];
	var win		=	frame.contentWindow||frame.window;
	var jq		=	win.$;
	var textboxs		=	jq('.easyui-textbox');
	var comboboxs		=	jq('.easyui-combobox');
	
	var adminName 	= 	jq(textboxs[0]).textbox('getValue') ;
	var dwmc		=	jq(textboxs[1]).textbox('getValue') ;
	var shouji 		= 	jq(textboxs[2]).textbox('getValue') ;
	var zhaidian	= 	jq(textboxs[3]).textbox('getValue') ;
	var bgsnxdh 	= 	jq(textboxs[4]).textbox('getValue') ;
	var bgswxdh	    = 	jq(textboxs[5]).textbox('getValue') ;
	var zgzt	    = 	jq(textboxs[6]).textbox('getValue') ;
	var jinghao	    = 	jq(textboxs[7]).textbox('getValue') ;
	var password 	= 	jq(textboxs[8]).textbox('getValue') ;
	var password2 	= 	jq(textboxs[9]).textbox('getValue') ;
	var bianhao 	= 	jq(textboxs[10]).textbox('getValue') ;
	
//	var isSuper 	= 	jq(comboboxs[0]).combobox('getValue') ;
//	var status 		= 	jq(comboboxs[1]).combobox('getValue') ;
	
	if(adminName==""){
		top.$.dlg.alertInfo('用户名称不能为空! ');
		return;
	}if(password==""){
		top.$.dlg.alertInfo('密码不能为空! ');
		return;
	}if(password2==""){
		top.$.dlg.alertInfo('请输入确认密码! ');
		return;
	}
	if(password!=password2){
		top.$.dlg.alertInfo('两次密码不一致! ');
		return;
	}
	if(shouji!=""){
		var r =/^1[3|4|5|8][0-9]\d{4,8}$/;   
		if(!r.test(shouji)){
			top.$.dlg.alertInfo('手机请输入11位纯数字 ');
			return;
		}
	}
	if(zhaidian!=""){
		var r =/^[0-9]+[0-9]*]*$/;   
		if(!r.test(zhaidian)){
			top.$.dlg.alertInfo('电话请输入纯数字 ');
			return;
		}if(zhaidian.length<6){
			top.$.dlg.alertInfo('电话请输入至少6位纯数字 ');
			return;
		}
	}
	if(toEdit){//修改
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'coptableAction!coptableUserUpdate.do',
			 data:{
				 xingming: adminName,
				 dlmm    : password,
				 zhaidian: zhaidian,
				 shouji  : shouji,
				 dwmc    : dwmc,
				 bgsnxdh : bgsnxdh,
				 bgswxdh : bgswxdh,
				 zgzt    : zgzt,
				 jinghao : jinghao,
				 bianhao : bianhao
			 },
			 success:function(value){
				 if(value==1){
					 top.$("#"+windowId).dialog('close'); doSearch();
				 }else{
					 top.$.dlg.alertInfo({0:'用户名称已存在!',2:'用户修改失败!'}[value]);
				};
			 }
		 });
	}else{
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'coptableAction!coptableUserInsert.do',
			 data:{
				 xingming: adminName,
				 dlmm    : password,
				 zhaidian: zhaidian,
				 shouji  : shouji,
				 dwmc    : dwmc,
				 bgsnxdh : bgsnxdh,
				 bgswxdh : bgswxdh,
				 zgzt    : zgzt,
				 jinghao : jinghao
			 },
			 success:function(value){
				 alert(value)
				 if(value==1){
					 alert(123);
					 top.$("#"+windowId).dialog('close'); doSearch();
				 }else{
					 top.$.dlg.alertInfo({0:'用户名称已存在!',2:'新增用户失败!'}[value]); 
				 }
				 
			 }
		 });
	}
}

function createUserInfo(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'用户管理',
		 width	:  	650,
		 height	: 	300,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/frame/system/systemUserDetail.jsp"
	 });
}

function editUserInfo(index){
	toEdit=true;
	var selRows = $('#searchGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'用户管理',
		 width	:  	650,
		 data   :   selRows,
		 height	: 	300,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/frame/system/systemUserDetail.jsp"
	 });
}

function deleteUserInfo(){
	var selRows = $('#searchGrid').datagrid('getSelected');  
	 if(selRows===null){
		 top.$.dlg.alertInfo('请先选择一行! ');
		 return;
	 }else{
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'coptableAction!deleteCoptable.do',
				 data:{adminID	:	selRows.BIANHAO},
				 success:function(flag){
					 if(flag == 1){
						 doSearch();
					 }
				 }
			 });
		}); 
	}
}
//导出
function doExport(){
	var totalSize = $('#searchGrid').datagrid('getData').total;
	var model = {};
	model.adminName = $('#adminName').textbox('getValue');
	model.telphone = $('#telphone').textbox('getValue');
	model.phone = $('#phone').textbox('getValue');
	if(totalSize>5000){
		var qd = confirm("记录过多支持前5000条,确定要将记录导出Excel为表格吗？");
		if(qd){
			downloadExcelUser(model);
		}
	}else{
		var qd = confirm("确定要将记录导出Excel为表格吗？");
		if(qd){
			downloadExcelUser(model);
		}
	}
}
function downloadExcelUser(sParam){
	var downLoadExcelPar = "adminName="+sParam.adminName+
    "&telphone="+ sParam.telphone+
    "&phone="+ sParam.phone;
	document.excelForm.action = 'coptableAction!exoprtExcel.do?'+downLoadExcelPar;
	document.excelForm.submit(); 
}