<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-header">自定义表单【{$form.title}】提交记录</div>
		<div class="layui-card-body">
			<table id="loglist-table" lay-filter="loglist-table"></table>
			<script type="text/html" id="tabletoolbar">
				<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm" lay-event="batchdel">删除</button>
					<button class="layui-btn layui-btn-sm" lay-event="export">导出全部</button>
				</div>
			</script>
			<script type="text/html" id="useravatarTpl">
				{{#  if(d.user!=null && d.user.avatar != ''){ }}
				<div id="pic{{d.id}}" lay-event="pic">
					<img style="width: 100%; height: 100%;" src="{{d.user.avatar}}">
				</div>
				{{#  } }}
			</script>
			{foreach $form->formItem as $key=>$vo }
			<script type="text/html" id="item{$vo->id}Tpl">
				{{#  if({$vo->itype}==8){ }}
					请点击详情查看图片
				{{#  }else { }}
					<div>{{d.formLogval[{$key}].ivalue}}</div>
				{{#  } }}
			</script>
			{/foreach}
			<script type="text/html" id="setTpl">
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-layer"></i>详情</a>
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
			</script>
		</div>
	</div>
</div>

<script>
	layui.config({
		base: '/static/echoui/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'form', 'set', 'table', 'selectN', 'selectM'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			form = layui.form,
			table = layui.table,
			selectN = layui.selectN,
			selectM = layui.selectM;
		form.render();


		table.render({
			elem: '#loglist-table',
			url: setter.baseAdminUrl + 'form/listlog',
			where: {
				formid: {$form->id}
			},
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: '表ID', sort: true },
					{ field: 'useravatar', width: 60, title: '头像', templet: '#useravatarTpl' },
					{ field: 'userrealname', title: '姓名', width: 120, templet: '<div>{{d.user!=null ? d.user.realname : ""}}</div>' },
					{ field: 'usermobile', title: '手机号', width: 150, templet: '<div>{{d.user!=null ? d.user.mobile : ""}}</div>' },
					{ field: 'remark', title: '备注（点击编辑）', minWidth: 300, edit: 'text' },
					{ field: 'createtime', title: '创建时间', width: 170 },
					{foreach $form->formItem as $key=>$vo }
					{ field: 'item{$vo->id}', title: '{$vo->title}', templet: '#item{$vo->id}Tpl', hide: true },
					{/foreach}
					{ title: '操作', width: 170, align: 'center', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			toolbar: '#tabletoolbar',
			cellMinWidth: 300,
			text: '对不起，加载出现异常！'
		});

		table.on('edit(loglist-table)', function(obj) {
			var id = obj.data.id,
				field = obj.field,
				value = obj.value;
			admin.req({
				url: setter.baseAdminUrl + 'form/fieldlog',
				data: { id: id, field: field, value: value },
				done: function(res) {
					layer.msg('修改成功');
				}
			});
		});

		//头工具栏事件
		table.on('toolbar(loglist-table)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			switch (obj.event) {
				case 'batchdel':
					var checkData = checkStatus.data;
					if (checkData.length === 0) {
						return layer.msg('请选择数据');
					}
					var idarr = [];
					for (var i = 0; i < checkData.length; i++) {
						idarr.push(checkData[i].id);
					}
					layer.confirm('删除后不可恢复，确定删除此提交记录吗？', function(index) {
						admin.req({
							url: setter.baseAdminUrl + 'form/dellog',
							data: {
								idarr: idarr
							},
							done: function(res) {
								table.reload('loglist-table');
								layer.msg('已删除');
							}
						});
					});
					break;
				case 'export':
					var url = setter.baseAdminUrl + 'form/exportlog?formid=' + {$form->id};
					window.location.href = url;
					break;
			};
		});
		
		table.on('tool(loglist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('确定删除此提交记录吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'form/dellog',
						data: {
							idarr: [data.id]
						},
						done: function(res) {
							obj.del();
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			} else if (obj.event === 'detail') {
				var index = layer.open({
					type: 2,
					title: '记录详情',
					content: 'logdetail.html?logid=' + data.id,
					maxmin: true,
					area: ['750px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'pic') {
				layer.photos({
					photos: '#pic' + data.id,
					anim: 5,
				});
			}
		});
	});
</script>
