;
layui.config({
	base: "../../js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'table', 'laytpl'], function () {
	var form = layui.form,
		layer = layui.layer,
		$ = layui.jquery,
		table = layui.table;
	var funcName = "org";
	form.verify({
		namelength: [
			/^.{0,12}$/,
			"长度限制在0-12个字符噢"
		]
	});
	//工具条处理
	function toolOperating(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('真的删除吗?', function (index) {
				layer.close(index);
				var index1 = layer.msg('删除中,请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "get",
					url: 'delete_' + funcName + '?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 === 'disauth') { //授权
			layer.open({
				type: 2,
				anim: 0,
				title: "功能授权",
				area: ['500px', '520px'],
				fixed: true,
				scrollbar: true,
				maxmin: true,
				content: 'auth.html?guid=' + data.guid,
				end: function () { }
			});
		}
	}

	//列表
	var DataList = {
		id: funcName,
		elem: '#DataTable',
		url: funcName + 'List',
		page: true,
		method: "post",
		contentType: 'application/json',
		loading: true,
		cellMinWidth: 80,
		cols: [
			[{
				title: '选择',
				type: 'radio'
			}, {
				field: 'orgCode',
				title: '企业代码',
				align: 'left',
			}, {
				field: 'orgName',
				title: '企业名称',
				align: 'left',
			}, {
				field: 'principals',
				title: '法人',
				align: 'left',
			}, {
				field: 'tel',
				title: '电话号码',
				align: 'center'
			}, {
				field: 'orgAddress',
				title: '地址',
				align: 'center'
			}, {
				field: 'operating',
				title: '操作',
				toolbar: '#operatingTool',
				align: 'center',
				fixed: 'right',
				width: 240
			}]
		] //设置表头
		,
		done: function (res) {
			resetButton(layui.sessionData(window.hywa.sessionTable.tableName).userData.menus.subMenuList, $);
		}
	};

	//执行默认渲染表格
	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);

	$("#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);
			}
		});
	});
	//新增管理用户
	$("#addBtnUser").on("click", function () {
		var checkStatus = table.checkStatus(funcName); //idTest 即为基础参数 id 对应的值
		if (checkStatus.data.length) {
			layer.open({
				type: 2,
				anim: 0,
				title: "新增",
				area: ['800px', '470px'],
				fixed: true,
				scrollbar: true,
				maxmin: true,
				content: 'addFirmUser.html?fkOrgGuid=' + checkStatus.data[0].guid + '&orgName=' + checkStatus.data[0].orgName,
				end: function () {
					table.reload(funcName);
				}
			});
		} else {
			layer.msg('必须选择一个企业进行新增操作', {
				time: 4000,
				icon: 2
			});
		}
	});
});