/**
 * 区域设置
 * */

var token = $.cookie("token");
var table = $('#table1');
var userId; //左侧用户id
$(function() {
	//左边表格
	initTable();
	//右边树形结构
	tree();
	//搜索功能
	//	$('#sc_btns').click(function() {
	//		initTable();
	//	});

	/*选中行添加背景颜色*/
	table.on("click-row.bs.table", function(e, row, ele) {
		$('.success').removeClass('success'); //去除之前选中的行的，选中样式
		$(ele).addClass('success'); //添加当前选中的 success样式用于区别
		userId = row.F_UserId;
		tree();
	});

});

function tree() {
	$.get(huayi.config.callcenter_url + "CusRegionCategory/GetRegionFunction", {
		userid: userId,
		token: $.cookie("token"),
	}, 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": "ps",
			"N": "ps"
		},
	},
	data: {
		key: {
			//name: "text"
		},
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "pid",
			rootPId: 0
		}
	},
	callback: {
		onCheck: zTreeOnClick
	}
};

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

//保存区域设置
function btn_save() {
	if(userId === undefined){
		layer.confirm('您还没有设置区域',{
			icon: 2,
			btn: ['确定']
		});
		return 
	}
	
	var arr = [-1];
	var ztree = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
	if(ztree.length > 0){
		arr = [];
		$.each(ztree, function(i, n) {
			arr.push(n.id);
		});
	}
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "CusRegionCategory/SaveRegionUserFunction",
		dataType: 'json',
		data: {
			useraccountid: userId, //是	string	会员id
			regionIds: arr, //是	int[]	项目楼id
			token: token,
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				layer.confirm('区域设置保存成功！', {
					btn: ['确定']
				});
				return;
			}
		}
	});
}
//用户列表
function initTable() {
	//先销毁表格
	table.bootstrapTable('destroy');
	//初始化表格,动态从服务器加载数据
	table.bootstrapTable({
		method: "get", //使用get请求到服务器获取数据
		url: huayi.config.callcenter_url + "UserAccount/GetList", //获取数据的Servlet地址
		contentType: "application/x-www-form-urlencoded",
		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,
			};
			return param;
		},
		onLoadSuccess: function() { //加载成功时执行
			//layer.msg("加载成功");
		},
		onLoadError: function() { //加载失败时执行
			//layer.msg("加载数据失败", { time: 1500, icon: 2 });
		}
	});
}

function setCode(val, row, index) {
	var page = table.bootstrapTable("getPageCode");
	return page.pageSize * (page.pageNumber - 1) + index + 1;
}