<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-broker-brokerlist-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="workerid" lay-search>
							<option value="">全部公司（可搜索）</option>
							{volist name="workerlist" 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="">全部状态</option>
							<option value="1">正常</option>
							<option value="2">禁用</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<button class="layui-btn" lay-submit lay-filter="LAY-broker-brokerlist-btn">
						<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" data-type="batchdel">删除</button>
				<button class="layui-btn layuiadmin-btn" data-type="add">添加</button>
				<button class="layui-btn layuiadmin-btn" data-type="default">默认经纪人</button>
			</div>

			<table id="LAY-broker-brokerlist-table" lay-filter="LAY-broker-brokerlist-table"></table>
			<script type="text/html" id="address">
				{{d.town}} - {{d.village}}
			</script>
			<script type="text/html" id="muserTpl">
				{{#  if(d.userid != 0){ }}
					{{d.muser.realname}} - {{d.muser.mobile}} 
				{{#  } }}
			</script>
			<script type="text/html" id="workertitleTpl">
				{{d.worker.title}}
			</script>
			<script type="text/html" id="agenttitleTpl">
				{{d.agent.title}}
			</script>
			<script type="text/html" id="usercountTpl">
				{{d.user_count}}
			</script>
			<script type="text/html" id="setTpl">
				<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'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			form = layui.form,
			table = layui.table;
		form.render();

		table.render({
			elem: '#LAY-broker-brokerlist-table',
			url: setter.baseAdminUrl + 'broker/listbroker',
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: '表ID', sort: true },
					{ field: 'title', title: '姓名', edit: 'text' },
					{ field: 'mobile', title: '手机号' },
					{ field: 'muser', title: '关联的用户（姓名-手机号）', templet: '#muserTpl', minWidth: 250, align: 'left' },
					{ field: 'address', title: '镇街', templet: '#address' },
					{ field: 'region', title: '区域' },
					{ field: 'workertitle', title: '劳务公司', templet: '#workertitleTpl' },
					{ field: 'agenttitle', title: '代理门店', templet: '#agenttitleTpl' },
					{ field: 'usercount', title: '用户邀请数', templet: '#usercountTpl', width: 100, align: 'center' },
					{ field: 'status_text', title: '状态', width: 100, align: 'center' },
					{ field: 'powerreport_text', title: '报备权限', width: 100, align: 'center' },
					{ field: 'createtime', title: '创建时间', width: 170 },
					{ title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			cellMinWidth: 150,
			text: '对不起，加载出现异常！'
		});

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

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

		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-broker-brokerlist-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.prompt({
					formType: 1,
					title: '敏感操作，请验证密码'
				}, function(value, index) {
					layer.close(index);
					layer.confirm('确定删除此经纪人吗？', function(index) {
						admin.req({
							url: setter.baseAdminUrl + 'broker/delbroker',
							data: {
								password: value,
								idarr: idarr
							},
							done: function(res) {
								table.reload('LAY-broker-brokerlist-table');
								layer.msg('已删除');
							}
						});
						layer.close(index);
					});
				});
			},
			add: function() {
				var index = layer.open({
					type: 2,
					title: '添加经纪人',
					content: 'brokerform.html?id=0',
					maxmin: true,
					area: ['750px', '480px']
				});
				layer.full(index);
			},
			default: function () {
				var index = layer.open({
					type: 2,
					title: '默认经纪人',
					content: 'brokerdefault.html',
					maxmin: true,
					area: ['750px', '480px']
				});
				layer.full(index);
			}
		};

		table.on('tool(LAY-broker-brokerlist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.prompt({
					formType: 1,
					title: '敏感操作，请验证密码'
				}, function(value, index) {
					layer.close(index);
					layer.confirm('确定删除此经纪人吗？', function(index) {
						admin.req({
							url: setter.baseAdminUrl + 'broker/delbroker',
							data: {
								password: value,
								idarr: [data.id]
							},
							done: function(res) {
								obj.del();
								layer.msg('已删除');
							}
						});
						layer.close(index);
					});
				});
			} else if (obj.event === 'edit') {
				var index = layer.open({
					type: 2,
					title: '编辑经纪人',
					content: 'brokerform.html?id=' + data.id,
					maxmin: true,
					area: ['750px', '480px']
				});
				layer.full(index);
			}
		});

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

	});
</script>
