		var token = $.cookie("token");
		var table = $('#table1');
		var code;
		var jsname = $('.searJsz').val();
		//搜索功能
		$('.search').click(function() {
			jsname = $('.searJsz').val()
			initTable(token, jsname)
		})
		//按回车搜索
		$('.searJsz').bind('keypress', function (event) {
  			if (event.keyCode == "13") {
  				jsname = $('.searJsz').val();
  				initTable(token, jsname);
   				return false;
			}
		});
		//========================添加功能开始=========================
		$('.add').click(function() {
			$('.addModel').css('display', 'block');
		})
		//关闭按钮
		$('.addBtr').click(function() {
			$('.addModel').css('display', 'none');
			clean();
		})
		//保存按钮
		$('.addCun').click(function() {
			if($('.add_jsz').val() == '') {

				$('.addJsz_kong').css('display', 'inline-block')
			} else {
				$('.addModel').css('display', 'none');
				ad();
			}

		})

		function ad() {
			$.ajax({
				type: "post",
				url: huayi.config.callcenter_url + "RoleInfo/AddRole",
				async: true,
				dataType: 'json',
				data: {
					token: token,
					rolename: $('.add_jsz').val(), //角色名称
					roleremark: $('.add_bz').val() //备注
				},
				success: function(data) {
					if(data.state.toLowerCase() == 'success') {
						layer.msg("添加成功！");
						table.bootstrapTable('refresh');
						clean()
						 $('.add_jsz').val(''); //角色名称
					 $('.add_bz').val('');
					}
				}
			});

		}
		//*************************添加功能结束=========================

		//========================修改功能开始=========================

		$('.change').click(function() {
			if(jsid) {
				$('.changeModel').css('display', 'block');
				cg(jsid)
			} else {
				layer.confirm('请选择修改行！', {
					btn: ['确定']
				});
				return;
			}

		})
		//关闭按钮
		$('.changeBtr').click(function() {
			$('.changeModel').css('display', 'none');
			clean();
		})
		//保存按钮
		$('.changeCun').click(function() {
			if($('.change_jsz').val() == '') {
				$('.change_kong').css('display', 'inline-block')
			} else {
				$('.changeModel').css('display', 'none');
				cc(jsid);
			}

		})

		function cg(jsid) {
			$.ajax({
				type: "get",
				url: huayi.config.callcenter_url + "RoleInfo/GetRole",
				async: true,
				dataType: 'json',
				data: {
					token: token,
					roleid: jsid //角色id
				},
				success: function(data) {
					var con = data.data;
					if(data.state.toLowerCase() == 'success') {
						$('.change_jsz').val(con.F_RoleName);
						$('.change_bz').val(con.F_Remark);
					}
				}
			});

		}

		function cc(jsid) {
			$.ajax({
				type: "post",
				url: huayi.config.callcenter_url + "RoleInfo/EditRole",
				async: true,
				dataType: 'json',
				data: {
					token: token,
					roleid: jsid, //角色id
					rolename: $('.change_jsz').val(), //角色名称
					roleremark: $('.change_bz').val() //备注
				},
				success: function(data) {
					if(data.state.toLowerCase() == 'success') {
						layer.msg("修改成功！");
						table.bootstrapTable('refresh');
						clean();
					}
				}
			});
		}
		//*************************修改功能结束=========================
		//========================删除功能开始=========================
		$('.del').click(function() {
			if(jsid) {
				$('.delModel').css('display', 'block');
			} else {
				layer.confirm('请选择要删除的行！', {
					btn: ['确定']
				});
				return;
			}
		})
		//关闭按钮
		$('.delBtr').click(function() {
			$('.delModel').css('display', 'none');
			clean();
		})
		//取消按钮
		$('.return').click(function() {
			$('.delModel').css('display', 'none');
		})
		//确定按钮
		$('.sure').click(function() {
			$('.delModel').css('display', 'none');
			del(jsid);
		})

		function del(jsid) {
			$.ajax({
				type: "post",
				url: huayi.config.callcenter_url + "RoleInfo/DelRole",
				async: true,
				dataType: 'json',
				data: {
					token: token,
					id: jsid //角色id
				},
				success: function(data) {
					if(data.state.toLowerCase() == 'success') {
						layer.msg("删除成功！");
						table.bootstrapTable('refresh');
						clean();
					} else {
						alert(data.message);

					}
				}
			});

		}
		//*************************删除功能结束=========================
		//右边树形结构
		tree(jsid);

		function tree(jsid) {
			$.get(huayi.config.callcenter_url + "RoleF/GetRoleFunction", {
				"token": $.cookie("token"),
				"RoleID": jsid
			}, function(result) {
				result = $.parseJSON(result);
				$(result.data).each(function(i, n) {
					if(n.ischecked == true) {
						n.checked = "true";
					}
				})
				$.fn.zTree.init($("#treeDemo"), setting1, result.data); //实例化树形图
			});
		}
		var setting1 = {
			check: {
				enable: true,
				//				chkboxType: {"Y":"", "N":""},
			},
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pid",
					rootPId: 0
				}
			},
			callback: {
				onCheck: zTreeOnClick
			}
		};

		function zTreeOnClick(event, treeId, treeNode) {
			var name = treeNode.name;
		};

		//保存权限
		function pro(app, jsid) {
			if(jsid == "" || jsid == undefined) {
				layer.confirm('请选择角色！', {
					btn: ['确定']
				});
				return;
			}
			$.ajax({
				type: "post",
				url: huayi.config.callcenter_url + "RoleF/SaveRoleFunction",
				async: true,
				dataType: 'json',
				data: {
					token: token,
					RoleID: jsid, //角色id
					functionIds: app
				},
				success: function(data) {
					if(data.state.toLowerCase() == 'success') {
						clean();
						layer.confirm('保存权限成功！', {
							btn: ['确定']
						});
						return;
					}
				}
			});
		}

		$('.protect').click(function() {
			var app = [];
			var ztree = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
			$.each(ztree, function(i, n) {
				app.push(n.id);
			});
			pro(app, jsid);
		})
		//左边表格
		initTable(token, jsname);

		function initTable(token, jsname) {
			//先销毁表格
			table.bootstrapTable('destroy');
			//初始化表格,动态从服务器加载数据
			table.bootstrapTable({
				method: "get", //使用get请求到服务器获取数据
				url: huayi.config.callcenter_url+"RoleInfo/GetRoleList", //获取数据的Servlet地址
				contentType: 'application/x-www-form-urlencoded',
//				url: "jueSeQuanXian.json", //获取数据的Servlet地址
				striped: true, //表格显示条纹
				pagination: true, //启动分页
				pageSize: 10, //每页显示的记录数
				pageNumber: 1, //当前第几页
				pageList: [10,15,20,30, 50, 100], //记录数可选列表
				search: false, //是否启用查询
				showColumns: false, //显示下拉框勾选要显示的列
				showRefresh: false, //显示刷新按钮
				sidePagination: "server", //表示服务端请求
				//设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
				//设置为limit可以获取limit, offset, search, sort, order
				queryParamsType: "undefined",
				queryParams: function queryParams(params) { //设置查询参数
					var param = {
						pageindex: params.pageNumber,
						pagesize: params.pageSize,
						token: token,
						name: jsname.replace(/\ +/g,"") //角色名称
					};
					return param;
				},
				onLoadSuccess: function() { //加载成功时执行
					//layer.msg("加载成功");
				},
				onLoadError: function() { //加载失败时执行
					//layer.msg("加载数据失败", { time: 1500, icon: 2 });
				}
			});
		}
		/*选中行添加背景颜色*/
		var jsid;
		table.on("click-row.bs.table", function(e, row, ele) {
			$('.success').removeClass('success'); //去除之前选中的行的，选中样式
			$(ele).addClass('success'); //添加当前选中的 success样式用于区别
			jsid = row.F_RoleId;
			tree(jsid);
		});

		function setCode(val, row, index) {
            return index + 1;  
		}
		
		//清除
		function clean() {
			jsid = 0;
			$(".add_jsz").val("");
			$(".add_bz").val("");
			$(".change_jsz").val("");
			$(".change_bz").val("")
		}
