<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-form-pane  layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-user-userlist-search">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">昵称/姓名</label>
					<div class="layui-input-block">
						<input type="text" name="keywords" placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">用户组</label>
					<div class="layui-input-block">
						<select name="groupsid" lay-search>
							<option value="">全部用户组（可搜索）</option>
							{volist name="groupslist" id="vo"}
							<option value="{$vo.id}">{$vo.title}</option>
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">分组</label>
					<div class="layui-input-block">
						<select name="groupid" lay-search>
							<option value="">全部分组（可搜索）</option>
							{volist name="grouplist" id="vo"}
							<option value="{$vo.id}">{$vo.name}</option>
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">门店</label>
					<div class="layui-input-block">
						<select name="agentid" lay-search>
							<option value="">全部门店（可搜索）</option>
							{volist name="agentlist" id="vo"}
							<option value="{$vo.id}">{$vo.title}</option>
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">状态</label>
					<div class="layui-input-block">
						<select name="status">
							<option value="0">全部状态</option>
							<option value="1">待审核</option>
							<option value="2">已通过</option>
							<option value="3">未通过</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<button class="layui-btn" lay-submit lay-filter="LAY-user-userlist-search-btn">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
					<button class="layui-btn" lay-submit lay-filter="LAY-user-userlist-export-btn">
						导出
					</button>
				</div>
			</div>
		</div>

		<div class="layui-card-body">
			<table id="LAY-user-userlist-table" lay-filter="LAY-user-userlist-table"></table>
			<script type="text/html" id="avatarTpl">
				{{#  if(d.avatar != ''){ }}
						<div id="pic{{d.id}}" lay-event="pic">
							<img style="width: 100%; height: 100%;" src="{{d.avatar}}">
						</div>
						{{#  } }}
					</script>
			<script type="text/html" id="groupsidTpl">
				{{d.groupsid == 0 ? '' : d.userGroups.title}}
			</script>
			<script type="text/html" id="groupidTpl">
				{{d.groupid == 0 ? '无' : d.workerGroup.name}}
			</script>
			<script type="text/html" id="agentidTpl">
				{{d.brokerid == 0 ? '' : d.broker.agent.title}}
					</script>
			<script type="text/html" id="brokeridTpl">
				{{d.brokerid == 0 ? '' : d.broker.title}}
					</script>
			<script type="text/html" id="setTpl">
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="auths"><i class="layui-icon layui-icon-rate-half"></i>授权</a>
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="follow"><i class="layui-icon layui-icon-engine"></i>跟进</a>
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></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();

		var agentbroker = selectN({
			elem: '#agentbroker',
			search: [true, true],
			tips: ["请选择代理门店（可搜索）", "请选择经纪人（可搜索）"],
			data: {$agentlist|raw},
			selected: [0, 0],
			field: { idName: 'id', titleName: 'title', childName: 'broker' }
		});

		table.render({
			elem: '#LAY-user-userlist-table',
			url: setter.baseWorkerUrl + 'user/listuser',
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: '表ID', sort: true },
					{ field: 'avatar', width: 60, title: '头像', templet: '#avatarTpl' },
					{ field: 'nickname', title: '昵称', edit: 'text' },
					{ field: 'realname', title: '姓名', edit: 'text' },
					{ field: 'mobile', title: '手机号' },
					{ field: 'status_text', title: '状态', minWidth: 80, align: 'center' },
					{ field: 'isvip_text', title: '是否VIP', minWidth: 80, align: 'center' },
					{ field: 'authstatus_text', title: '实名认证', minWidth: 100, align: 'center' },
					{ field: 'groupsid', title: '用户组', templet: '#groupsidTpl' },
					{ field: 'groupid', title: '分组', templet: '#groupidTpl' },
					{ field: 'agentid', title: '代理门店', templet: '#agentidTpl' },
					{ field: 'brokerid', title: '经纪人', templet: '#brokeridTpl' },
					{ field: 'createtime', title: '注册时间', width: 170, sort: true },
					{ field: 'followstatus_text', title: '跟进状态', minWidth: 150, align: 'center' },
					{ title: '操作', minWidth: 220, align: 'center', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			toolbar: true,
			cellMinWidth: 200,
			text: '对不起，加载出现异常！'
		});

		form.on('submit(LAY-user-userlist-search-btn)', function(data) {
			table.reload('LAY-user-userlist-table', {
				where: data.field,
				page: {
					curr: 1
				}
			});
		});

		form.on('submit(LAY-user-userlist-export-btn)', function(data) {
			var url = setter.baseWorkerUrl + 'user/exportuser?';
			for (var item in data.field) {
				url = url + '&' + item + '=' + data.field[item];
			}
			window.location.href = url;
		});

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

		table.on('tool(LAY-user-userlist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'auths') {
				var index = layer.open({
					type: 2,
					title: '用户【' + data.nickname + '】授权列表',
					content: 'authslist.html?userid=' + data.id,
					maxmin: true,
					area: ['750px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'follow') {
				var index = layer.open({
					type: 2,
					title: '用户【' + data.nickname + ' - ' + data.mobile + '】跟进记录',
					content: 'follow.html?userid=' + data.id,
					maxmin: true,
					area: ['750px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'edit') {
				var index = layer.open({
					type: 2,
					title: '编辑用户',
					content: 'userform.html?id=' + data.id,
					maxmin: true,
					area: ['550px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'pic') {
				layer.photos({
					photos: '#pic' + data.id,
					anim: 5,
				});
			}
		});

	});
</script>
