<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-integrallist-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="itype">
							<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>
							<option value="8">其他</option>
						</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-user-integrallist-search-btn">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
					<button class="layui-btn" lay-submit lay-filter="LAY-user-integrallist-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>
				<!--<button class="layui-btn layuiadmin-btn" data-type="batchitype">批量通过兑现记录</button>-->
			</div>
			<table id="LAY-user-integrallist-table" lay-filter="LAY-user-integrallist-table"></table>
			<script type="text/html" id="useravatarTpl">
				<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 ? d.user.nickname : ""}}
					</script>
			<script type="text/html" id="userrealnameTpl">
				{{d.user.realname ? d.user.realname : ""}}
					</script>
			<script type="text/html" id="usermobileTpl">
				{{d.user.mobile ? d.user.mobile : ""}}
					</script>
			<script type="text/html" id="useropenbankTpl">
				{{d.user.bankcard ? d.user.bankcard.openbank : ""}}
					</script>
			<script type="text/html" id="useraccountTpl">
				{{d.user.bankcard ? d.user.bankcard.account : ""}}
					</script>
			<script type="text/html" id="usernumberTpl">
				{{d.user.bankcard ? d.user.bankcard.number : ""}}
					</script>
			<script type="text/html" id="setTpl">
				{{#  if(d.itype == 3 && d.status == 1){ }}
					<a class="layui-btn layui-btn-xs" lay-event="setstatus"><i class="layui-icon layui-icon-ok"></i>已处理</a>
				{{#  } else if (d.itype == 3 && d.status == 2) { }}
					<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="setstatus"><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'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			form = layui.form,
			table = layui.table;
		form.render();

		table.render({
			elem: '#LAY-user-integrallist-table',
			url: setter.baseAdminUrl + 'user/listintegral',
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: 'ID', sort: true },
					{ field: 'avatar', width: 60, title: '头像', templet: '#useravatarTpl' },
					{ field: 'nickname', title: '昵称', templet: '#usernicknameTpl' },
					{ field: 'realname', title: '姓名', templet: '#userrealnameTpl' },
					{ field: 'mobile', title: '手机号', templet: '#usermobileTpl' },
					{ field: 'bankcardopenbank', title: '开户行', templet: '#useropenbankTpl', hide: true },
					{ field: 'bankcardaccount', title: '帐户名', templet: '#useraccountTpl', hide: true },
					{ field: 'bankcardnumber', title: '账户号', templet: '#usernumberTpl', hide: true },
					{ field: 'title', title: '积分标题', edit: 'text', width: 200 },
					{ field: 'itype_text', width: 150, title: '类型', align: 'center' },
					{ field: 'status_text', width: 150, title: '状态', align: 'center' },
					{ field: 'intvalue', title: '积分变更值', edit: 'text', width: 120, sort: true },
					{ field: 'intmoney', title: '积分金额', edit: 'text', width: 120, sort: true },
					{ field: 'remark', title: '备注', edit: 'text', width: 200 },
					{ field: 'createtime', title: '记录时间', width: 170, sort: true },
					{ title: '操作', minWidth: 230, align: 'right', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			toolbar: true,
			totalRow: true,
			cellMinWidth: 150,
			text: '对不起，加载出现异常！'
		});

		form.on('submit(LAY-user-integrallist-search-btn)', function(data) {
			table.reload('LAY-user-integrallist-table', {
				where: data.field,
				page: {
					curr: 1
				}
			});
		});
		form.on('submit(LAY-user-integrallist-export-btn)', function(data) {
			var url = setter.baseAdminUrl + 'user/exportintegral?';
			for (var item in data.field) {
				url = url + '&' + item + '=' + data.field[item];
			}
			window.location.href = url;
		});

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

		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-user-integrallist-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/delintegral',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-user-integrallist-table');
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			},
			batchstatus: function() {  
				var checkStatus = table.checkStatus('LAY-user-integrallist-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/statusintegralall',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-user-integrallist-table');
							layer.msg('已批量通过');
						}
					});
					layer.close(index);
				});
			}
		};

		table.on('tool(LAY-user-integrallist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('确定删除此用户积分记录吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'user/delintegral',
						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: 'integralupdate.html?id=' + data.id,
					maxmin: true,
					area: ['550px', '550px']
				});
				layer.full(index);
			} else if (obj.event === 'setstatus') {
				admin.req({
					url: setter.baseAdminUrl + 'user/statusintegral',
					data: { id: data.id },
					done: function(res) {
						layer.msg('兑现类型状态修改成功');
						layui.table.reload('LAY-user-integrallist-table');
					}
				});
			} 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>
