<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">
			<input type="hidden" name="ispublic" value="0">
			<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" id="agentbroker">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">状态</label>
					<div class="layui-input-block">
						<select name="status">
							<option value="">全部状态</option>
							<option value="1">待审核</option>
							<option value="2">已通过</option>
							<option value="3">未通过</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">实名认证</label>
					<div class="layui-input-block">
						<select name="authstatus">
							<option value="">全部认证状态</option>
							<option value="1">待认证</option>
							<option value="2">待审核</option>
							<option value="3">已认证</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">跟进状态</label>
					<div class="layui-input-block">
						<select name="followstatus">
							<option value="">全部跟进状态</option>
							<option value="1">未跟进</option>
							<option value="2">未面试</option>
							<option value="3">面试通过</option>
							<option value="4">面试未通过</option>
							<option value="5">用户放弃</option>
							<option value="6">已入职</option>
							<option value="7">已离职</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">
			<div class="layui-form" lay-filter="LAY-user-userlist-setbroker" style="padding-bottom: 10px;">
				<div class="layui-form-item">
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn" data-type="batchdel">删除</button>
					</div>
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn" data-type="add">添加新用户</button>
					</div>
					<div class="layui-inline">
						<div class="layui-input-block" style="margin-left: 20px;" id="setagentbroker">
						</div>
					</div>
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn" lay-submit lay-filter="LAY-user-userlist-setbroker-submit">批量设置顾问</button> 
					</div>
				</div>
			</div>
			<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="workeridTpl">
				{{d.brokerid == 0 ? '' : d.broker.worker.title}}
			</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="integral"><i class="layui-icon layui-icon-edit"></i>聘豆</a>
				<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.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' }
		});
		
		var setagentbroker = selectN({
			elem: '#setagentbroker',
			search: [true, true],
			tips: ["请选择代理门店（可搜索）", "请选择职业顾问（可搜索）"],
			data: {$agentlist|raw},
			selected: [0, 0],
			field: { idName: 'id', titleName: 'title', childName: 'broker' }
		});
		
		form.on('submit(LAY-user-userlist-setbroker-submit)', function(obj) {
			var checkStatus = table.checkStatus('LAY-user-userlist-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);
			}
			obj.field.idarr = idarr;
			admin.req({
				url: setter.baseAdminUrl + 'user/setbroker',
				type: 'get',
				data: obj.field,
				done: function(res) {
					layer.msg("批量设置职业顾问成功。", {
						icon: 1
					});
					table.reload('LAY-user-userlist-table');
				}
			});
		});

		table.render({
			elem: '#LAY-user-userlist-table',
			url: setter.baseAdminUrl + 'user/listuser',
			where: { "ispublic": 0 },
			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: 'integral', title: '可用聘豆', width: 100, align: 'right', totalRow: true },
					{ field: 'inttotal', title: '累计聘豆', width: 100, align: 'right', totalRow: true },
					{ field: 'status_text', title: '状态', minWidth: 80, align: 'center' },
					{ field: 'isvip_text', title: '是否VIP', minWidth: 100, align: 'center' },
					{ field: 'authstatus_text', title: '实名认证', minWidth: 100, align: 'center' },
					{ field: 'groupsid', title: '用户组', templet: '#groupsidTpl' },
					{ field: 'workerid', title: '劳务公司', templet: '#workeridTpl' },
					{ field: 'agentid', title: '代理门店', templet: '#agentidTpl' },
					{ field: 'brokerid', title: '职业顾问', templet: '#brokeridTpl' },

                    { field: 'education_text', title: '学历', minWidth: 100, align: 'center' },
                    { field: 'worker_text', title: '工作经验', minWidth: 100, align: 'center' },
                    { field: 'jobintention_text', title: '求职意向', minWidth: 100, align: 'center' },

                    { field: 'address', title: '居住地', minWidth: 100, align: 'center' },
                    { field: 'eduexperience', title: '个人描述', minWidth: 100, align: 'center' },


					{ field: 'user_part_count', title: '推广人数', minWidth: 100, align: 'center' },
					{ field: 'createtime', title: '注册时间', width: 170, sort: true },
					{ field: 'followstatus_text', title: '跟进状态', minWidth: 150, align: 'center' },
					{ title: '操作', minWidth: 350, align: 'center', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			toolbar: true,
			totalRow: 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.baseAdminUrl + '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.baseAdminUrl + 'user/fielduser',
				data: { id: id, field: field, value: value },
				done: function(res) {
					layer.msg('修改成功');
					// layui.table.reload('LAY-user-userlist-table');
				}
			});
		});

		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-user-userlist-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 + 'user/deluser',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-user-userlist-table');
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			},
			add: function() {
				var index = layer.open({
					type: 2,
					title: '添加用户',
					content: 'userform.html?id=0',
					maxmin: true,
					area: ['550px', '550px']
				});
				layer.full(index);
			}
		};

		table.on('tool(LAY-user-userlist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('确定删除此用户吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'user/deluser',
						data: {
							idarr: [data.id]
						},
						done: function(res) {
							obj.del();
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			} else 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 === 'integral') {
				var index = layer.open({
					type: 2,
					title: '设置用户【' + data.nickname + '】聘豆',
					content: 'integralform.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,
				});
			}
		});

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

	});
</script>
