<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-header">文章评论</div>
		<div class="layui-card-body" lay-filter="LAY-artapp-article-articlecommentlist-list">

			<div style="padding-bottom: 10px;">
				<button class="layui-btn layuiadmin-btn" data-type="cbatchdel">删除</button>
				<button class="layui-btn layuiadmin-btn" data-type="cbatchstatus">批量审核通过</button>
			</div>
			<table id="LAY-artapp-article-articlecommentlist-list-table" lay-filter="LAY-artapp-article-articlecommentlist-list-table"></table>
			<script type="text/html" id="useravatarTpl">
				<img style="width: 100%; height: 100%;" src="{{d.user.avatar}}" alt="{{d.user.nickname}}">
			</script>
			<script type="text/html" id="usernicknameTpl">
				{{d.user.nickname}}
			</script>
			<script type="text/html" id="detailsTpl">
				{{#  if(d.puserid == 0){ }}
					{{d.details}}
				{{#  } else { }}
					回复{{d.puser.nickname}}：{{d.details}}
				{{#  } }}
			</script>
			<script type="text/html" id="commentsetTpl">
				{{#  if(d.status == 1){ }}
					<a class="layui-btn layui-btn-xs" lay-event="setstatus"><i class="layui-icon layui-icon-ok"></i>已通过</a>
				{{#  } else { }}
					<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="setstatus"><i class="layui-icon layui-icon-edit"></i>待审核</a>
				{{#  } }}
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delcomment"><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'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			form = layui.form,
			table = layui.table;
		form.render();
		//列表
		table.render({
			elem: '#LAY-artapp-article-articlecommentlist-list-table',
			url: setter.baseAdminUrl + 'article/listcomment?articleid={$articleid}',
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: '表ID', sort: true },
					{ field: 'useravatar', width: 60, title: '头像', templet: '#useravatarTpl' },
					{ field: 'usernickname', width: 150, title: '昵称', templet: '#usernicknameTpl' },
					{ field: 'details', title: '内容', templet: '#detailsTpl', edit: 'text' },
					{ field: 'status_text', width: 160, title: '状态' },
					{ field: 'createtime', width: 160, title: '发布时间', sort: true },
					{ title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#commentsetTpl' }
				]
			],
			page: true,
			limit: 50,
			cellMinWidth: 150,
			text: '对不起，加载出现异常！'
		});

		//监听单元格编辑
		table.on('edit(LAY-artapp-article-articlecommentlist-list-table)', function(obj) {
			var id = obj.data.id,
				field = obj.field,
				value = obj.value;
			admin.req({
				url: setter.baseAdminUrl + 'article/fieldcomment',
				data: { id: id, field: field, value: value },
				done: function(res) {
					layer.msg('修改成功');
					// layui.table.reload('LAY-artapp-article-articlecommentlist-list');
				}
			});
		});

		//监听工具条
		table.on('tool(LAY-artapp-article-articlecommentlist-list-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'delcomment') {
				layer.confirm('确定删除此评论吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'article/delcomment',
						data: {
							idarr: [data.id]
						},
						done: function(res) {
							obj.del();
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			} else if (obj.event === 'setstatus') {
				admin.req({
					url: setter.baseAdminUrl + 'article/statuscomment',
					data: { id: data.id },
					done: function(res) {
						layer.msg('状态修改成功');
						layui.table.reload('LAY-artapp-article-articlecommentlist-list-table');
					}
				});
			}
		});

		//事件
		var active = {
			cbatchdel: function() {
				var checkStatus = table.checkStatus('LAY-artapp-article-articlecommentlist-list-table'),
					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 + 'article/delcomment',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-artapp-article-articlecommentlist-list-table');
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			},
			cbatchstatus: function() {
				var checkStatus = table.checkStatus('LAY-artapp-article-articlecommentlist-list-table'),
					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 + 'article/statuscommentall',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-artapp-article-articlecommentlist-list-table');
							layer.msg('已审核通过');
						}
					});
					layer.close(index);
				});
			}
		};

		$('.layui-btn.layuiadmin-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

	});
</script>
