<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-partlist-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="status">
							<option value="">全部状态</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-partlist-search-btn">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
					<button class="layui-btn" lay-submit lay-filter="LAY-user-partlist-export-btn">
						导出
					</button>
				</div>
			</div>
		</div>

		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
				<button class="layui-btn layuiadmin-btn" data-type="batchdel">删除</button>
			</div>
			<table id="LAY-user-partlist-table" lay-filter="LAY-user-partlist-table"></table>
			<script type="text/html" id="puseravatarTpl">
				{{#  if(d.puser.avatar != ''){ }}
				<div id="picpuser{{d.id}}" lay-event="picpuser">
					<img style="width: 100%; height: 100%;" src="{{d.puser.avatar}}" alt="{{d.puser.nickname}}">
				</div>
				{{#  } }}
			</script>
			<script type="text/html" id="pusernicknameTpl">
				{{d.puser.nickname}}
					</script>
			<script type="text/html" id="puserrealnameTpl">
				{{d.puser.realname}}
					</script>
			<script type="text/html" id="pusermobileTpl">
				{{d.puser.mobile}}
					</script>
			<script type="text/html" id="useropenbankTpl">
				{{d.puser.bankcard ? d.puser.bankcard.openbank : ""}}
					</script>
			<script type="text/html" id="useraccountTpl">
				{{d.puser.bankcard ? d.puser.bankcard.account : ""}}
					</script>
			<script type="text/html" id="usernumberTpl">
				{{d.puser.bankcard ? d.puser.bankcard.number : ""}}
					</script>
			<script type="text/html" id="useravatarTpl">
				{{#  if(d.user.avatar != ''){ }}
				<div id="picuser{{d.id}}" lay-event="picuser">
					<img style="width: 100%; height: 100%;" src="{{d.user.avatar}}">
				</div>
				{{#  } }}
			</script>
			<script type="text/html" id="usernicknameTpl">
				{{d.user.nickname}}
					</script>
			<script type="text/html" id="userrealnameTpl">
				{{d.user.realname}}
					</script>
			<script type="text/html" id="usermobileTpl">
				{{d.user.mobile}}
					</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-user-partlist-table',
			url: setter.baseAdminUrl + 'user/listpart',
			height:"full-20",
			cols: [
				[
					{ type: 'checkbox', fixed: 'left', rowspan: 2 },
					{ field: 'id', width: 80, title: '表ID', sort: true, rowspan: 2 },
					{ field: 'puser', align: 'center', title: '邀请人信息', colspan: 4 },
					{ field: 'user', align: 'center', title: '被邀请人信息', colspan: 4 },
					{ field: 'status_text', width: 150, title: '状态', align: 'center', rowspan: 2 },
					{ field: 'redmoney', title: '奖励金额', edit: 'text', width: 120, sort: true, rowspan: 2, totalRow: true },
					{ field: 'createtime', title: '邀请时间', width: 170, sort: true, rowspan: 2 },
					{ title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#setTpl', rowspan: 2 }
				],
				[
					{ field: 'avatar', width: 60, title: '头像', templet: '#puseravatarTpl' },
					{ field: 'nickname', title: '昵称', templet: '#pusernicknameTpl' },
					{ field: 'realname', title: '姓名', templet: '#puserrealnameTpl' },
					{ field: 'mobile', title: '手机号', templet: '#pusermobileTpl' },
				// 	{ field: 'bankcardopenbank', title: '开户行', templet: '#useropenbankTpl'},
				// 	{ field: 'bankcardaccount', title: '帐户名', templet: '#useraccountTpl'},
				// 	{ field: 'bankcardnumber', title: '账户号', templet: '#usernumberTpl' },

					{ field: 'avatar', width: 60, title: '头像', templet: '#useravatarTpl' },
					{ field: 'nickname', title: '昵称', templet: '#usernicknameTpl' },
					{ field: 'realname', title: '姓名', templet: '#userrealnameTpl' },
					{ field: 'mobile', title: '手机号', templet: '#usermobileTpl' },
				]
			],
			page: true,
			limit: 50,
			toolbar: true,
			totalRow: true,
			cellMinWidth: 150,
			text: '对不起，加载出现异常！'
		});

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

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

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

		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-user-partlist-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/delpart',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-user-partlist-table');
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			}
		};

		table.on('tool(LAY-user-partlist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('确定删除此邀请关系吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'user/delpart',
						data: {
							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: 'partform.html?id=' + data.id,
					maxmin: true,
					area: ['550px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'picpuser') {
				layer.photos({
					photos: '#picpuser' + data.id,
					anim: 5,
				});
			} else if (obj.event === 'picuser') {
				layer.photos({
					photos: '#picuser' + data.id,
					anim: 5,
				});
			}
		});

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

	});
</script>
