layui.config({
	base: "../../js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'laypage', 'element', 'laytpl', 'table'], function() {
	var form = layui.form,
		layer = layui.layer,
		laypage = layui.laypage,
		laytpl = layui.laytpl,
		$ = layui.jquery,
		table = layui.table,
		element = layui.element;
	form.verify({
		namelength: [
			/^.{0,12}$/,
			"长度限制在0-12个字符噢"
		],
		nameVerify: [
			/^[a-zA-Z][a-zA-Z0-9_]*$/,
			'以英文字母开头，只能包含英文字母、数字、下划线'
		]
	});
	//工具条处理
	function toolOperating(name, obj) {
		var data = obj.data;
		if (obj.event === 'list_li') {

		} else if (obj.event === 'del') {
			layer.confirm('真的删除吗？', function(index) {
				layer.close(index);
				var index1 = layer.msg('删除中，请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "DELETE",
					url: "delete_newAddUser?userId=" + obj.data.userId,
					success: function(res) {
						layer.close(index1);
						if (!(res.code - 0)) {
							obj.del();
							table.reload(name);
						}
					}
				});
			});
		} else if (obj.event === 'edit') { //修改
			layer.open({
				type: 2,
				title: "编辑",
				area: ['800px', '550px'],
				maxmin: true,
				content: 'add_change.html?id=' + data.userId,
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var submit = layero.find('iframe').contents().find("#formSubmit");
					submit.click();
				},
			});
		} else if (obj.event === 'reset') {
			layer.confirm('确定要把' + data.name + '的密码重置为123456吗？', function(index) {
				layer.close(index);
				var index1 = layer.msg('正在重置中，请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				data.password = 123456;
				$.ajax({
					type: 'POST',
					url: 'userAddNew',
					contentType: 'application/json',
					data: JSON.stringify(data),
					success: function(res) {
						if (!(res.code - 0)) {
							layer.close(index1);
							layer.msg(res.msg, {
								icon: 1
							});
						} else {
							layer.msg(res.msg, {
								icon: 2
							});
						}
					}
				});
			});
		}
	}
	//用户列表
	let tableId = 'userDataTable';
	var userDataList = {
		id: 'userDataTable',
		elem: '#userDataTable',
		url: 'userList',
		page: true,
		loading: true,
		cellMinWidth: 80,
		toolbar: '#toolbar',
		defaultToolbar: ['filter'],
		cols: [
				[ //标题栏
					{
						field: 'name',
						title: '账号',
						align: 'center',
						event: 'list_li',
						sort: true //排序
					}, {
						field: 'realName',
						title: '姓名',
						align: 'center',
						event: 'list_li',
						sort: true //排序
					}, {
						field: 'roleName',
						title: '角色',
						align: 'center',
						width: 250,
						sort: true //排序
					}, {
						field: 'sex',
						title: '性别',
						width: 60,
						//templet: '<div>{{# return !(d.sex-0)?"男":"女"}}</div>',
						align: 'center'
					}, {
						field: 'phone',
						title: '电话号码',
						width: 120,
						align: 'center'
					}, {
						field: 'loginTime',
						title: '最后登录时间',
						width: 180,
						align: 'center'
					}, {
						field: 'status',
						title: '是否可用',
						width: 110,
						templet: '#checkboxTpl',
						unresize: true,
						align: 'center'
					}, {
						field: 'operating',
						title: '操作',
						toolbar: '#operatingTool',
						align: 'center',
						fixed: 'right',
						width: 120
					}
				]
			] //设置表头
			,
		done: function() {
			if (!layui.data('auth').data.add) {
				$('.add-btn').hide();
			}
		}
	};

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

	//监听产品工具事件条
	table.on('tool(userDataTable)', function(obj) {
		toolOperating(tableId, obj);
	});
	//搜索响应函数
	var infoSearch = function(obj) {
		table.reload(tableId, { //执行重载
			where: obj.field
		});
		return false;
	};
	//监听搜索表单事件
	form.on("submit(userDataSearch)", infoSearch);

	//监听可用操作
	form.on('checkbox(statusDemo)', function(obj) {
		console.log(obj)
		let ids = {
			userId: obj.elem.id,
			status: obj.value
		}
		$.ajax({
			url: 'forbidUser',
			type: 'post',
			data: JSON.stringify(ids),
			contentType: 'application/json',
			success: function(res) {
				if (!(res.code - 0)) {
					parent.layer.msg(res.msg, {
						icon: 1
					});
					table.reload(tableId);
				} else {
					layer.msg(res.msg, {
						icon: 2
					});
					obj.elem.checked = !obj.elem.checked;
					form.render();
				}
			}
		});
	});


	//监听头部工具栏
	table.on('toolbar(' + tableId + ')', function(obj) {
		switch (obj.event) {
			case 'add':
				layer.open({
					type: 2,
					title: "添加",
					area: ['800px', '550px'],
					maxmin: true,
					content: 'add_change.html',
					btn: ['确定', '取消'],
					yes: function(index, layero) {
						var submit = layero.find('iframe').contents().find("#formSubmit");
						submit.click();
					},
				});
				break;
		};
	});
});
