<title>管理员</title>

<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
			<div class="layui-form-item">
				<div class="layui-inline">
					<div class="layui-input-inline">
						<input type="text" name="username" placeholder="姓名" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>
			</div>
		</div>

		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
				<!-- <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button> -->
				<button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
			</div>

			<table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
			<script type="text/html" id="table-useradmin-webuser">
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></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.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table'], function() {
		var $ = layui.$,
			admin = layui.admin,
			view = layui.view,
			table = layui.table,
			form = layui.form;

		form.render(null, 'layadmin-userfront-formlist');

		//监听搜索
		form.on('submit(LAY-user-front-search)', function(data) {
			var field = data.field;

			//执行重载
			table.reload('LAY-user-manage', {
				where: field
			});
		});

		//事件
		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-user-manage'),
					checkData = checkStatus.data; //得到选中的数据

				if (checkData.length === 0) {
					return layer.msg('请选择数据');
				}

				layer.prompt({
					formType: 1,
					title: '敏感操作，请验证口令'
				}, function(value, index) {
					layer.close(index);

					layer.confirm('确定删除吗？', function(index) {

						//执行 Ajax 后重载
						/*
						admin.req({
						  url: 'xxx'
						  //,……
						});
						*/
						table.reload('LAY-user-manage');
						layer.msg('已删除');
					});
				});
			},
			add: function() {
				admin.popup({
					title: '添加用户',
					area: ['350px', '370px'],
					id: 'LAY-popup-user-add',
					success: function(layero, index) {
						view(this.id).render('user/user/userform').done(function() {
							form.render(null, 'layuiadmin-form-useradmin');

							//监听提交
							form.on('submit(LAY-user-front-submit)', function(data) {
								var res = admin.useAjax("yyds/user/index", 'POST', data.field);
								if (res.code) {
									layui.table.reload('LAY-user-manage'); //重载表格
									layer.close(index); //执行关闭
								}
								return false;
							});
						});
					}
				});
			}
		};

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