;
layui.config({
	base: "../../js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'table', 'laytpl'], function () {
	var form = layui.form,
		layer = layui.layer,
		laytpl = layui.laytpl,
		$ = layui.jquery,
		table = layui.table;
	var funcName = "user";
	form.verify({
		namelength: [
			/^.{0,12}$/,
			"长度限制在0-12个字符噢"
		]
	});
	//工具条处理
	function toolOperating(obj) {
		var data = obj.data;
		var userName = (obj.data.userName == '' || obj.data.userName == null) ? '匿名用户' : obj.data.userName;
		if (obj.event === 'del') {
			layer.confirm('真的删除用户“' + userName + '”吗?', function (index) {
				layer.close(index);
				var index1 = layer.msg('删除中,请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "get",
					url: 'delete_user?guid=' + obj.data.guid,
					success: function (res) {
						layer.close(index1);
						if (!(res.code - 0)) {
							obj.del();
							table.reload(funcName);
						}
					}
				});
			});
		} else if (obj.event === 'edit') { //修改
			layer.open({
				type: 2,
				anim: 0,
				title: "修改",
				area: ['800px', '470px'],
				fixed: true,
				scrollbar: true,
				maxmin: true,
				content: 'add_change.html?event=edit&guid=' + data.guid,
				end: function () {
					table.reload(funcName); //重新载入表格
				}
			});
		} else if (obj.event === 'view') { //查看
			layer.open({
				type: 2,
				anim: 0,
				title: "查看",
				area: ['800px', '470px'],
				fixed: true,
				scrollbar: true,
				maxmin: true,
				content: 'add_change.html?event=view&guid=' + data.guid,
				end: function () { }
			});
		} else if (obj.event === 'reset') {
			layer.confirm('真的重置“' + (obj.data.userName || '') + '”用户密码吗?', function (index) {
				layer.close(index);
				var index1 = layer.msg('重置中,请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "get",
					url: 'restPwd?guid=' + obj.data.guid,
					success: function (res) {
						layer.close(index1);
						layer.alert('密码已经重置为123123');
					}
				});
			});
		}
	}

	//列表
	var DataList = {
		id: funcName,
		elem: '#DataTable',
		url: funcName + 'List',
		page: true,
		method: "post",
		contentType: 'application/json',
		loading: true,
		cellMinWidth: 80,
		cols: [
			[ //标题栏
				{
					field: 'account',
					title: '账号',
					align: 'left',
					minWidth: 90
				}, {
					field: 'userName',
					title: '姓名',
					align: 'left',
					minWidth: 160
				}, {
					field: 'roleName',
					title: '角色',
					align: 'left',
					minWidth: 160
				}, {
					field: 'mobileNo',
					title: '电话号码',
					align: 'center',
					minWidth: 140
				}, {
					field: 'lastLoginTime',
					title: '最后登录时间',
					align: 'center',
					minWidth: 200
				}, {
					field: 'isForbid',
					title: '是否锁定',
					width: 110,
					templet: '#checkboxTpl',
					unresize: true,
					align: 'center',
					minWidth: 160
				}, {
					field: 'operating',
					title: '操作',
					toolbar: '#operatingTool',
					align: 'center',
					fixed: 'right',
					minWidth: 240
				}
			]
		] //设置表头
		,
		done: function (res) {
			resetButton(layui.sessionData(window.hywa.sessionTable.tableName).userData.menus.subMenuList, $);
			//res.button.add ? $('.add-btn-0').show() : $('.add-btn-0').hide();
		}
	};

	//执行默认渲染表格
	table.render(DataList);

	//监听产品工具事件条
	table.on('tool(DataTable)', function (obj) {
		toolOperating(obj);
	});
	//搜索响应函数
	var infoSearch = function (obj) {

		table.reload(funcName, { //执行重载
			where: obj.field
		});
		return false;
	};
	//监听搜索表单事件
	form.on("submit(DataSearch)", infoSearch);

	//监听可用操作
	form.on('checkbox(statusDemo)', function (obj) {
		$.ajax({
			url: 'forbidUser?guid=' + obj.elem.id + '&isForbid=' + this.value,
			type: 'get',
			contentType: 'application/json',
			success: function (res) {
				if (!(res.code - 0)) {
					parent.layer.msg(res.msg, {
						icon: 1
					});
					table.reload(funcName);
				} else {
					layer.msg(res.msg, {
						icon: 2
					});
					obj.elem.checked = !obj.elem.checked;
					form.render();
				}
			}
		});
	});
	$("#addBtn").on("click", function () {
		layer.open({
			type: 2,
			anim: 0,
			title: "新增",
			area: ['800px', '470px'],
			fixed: true,
			scrollbar: true,
			maxmin: true,
			content: 'add_change.html',
			end: function () {
				table.reload(funcName);
			}
		});
	});

	//类型下拉列表模板
	function typeTpl(obj) {
		laytpl(typeHtmlTpl.innerHTML).render(obj, function (html) {
			$(obj.elem).html(html);
			form.render();
		});
	}


	$("#rolesView").next().click(function (e) {
		if ($(this).hasClass('layui-form-selected')) {
			$.ajax({
				url: 'roleList',
				method: 'post',
				contentType: "application/json",
				data: JSON.stringify({}),
				success: function (res) {
					typeTpl({
						elem: "#rolesView",
						list: res.data
					});
					form.render('select');
				}
			});
		}
	});
});