<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('会员码列表')" />
	<th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
<div class="container-div">
	<div class="row">
		<div class="col-sm-12 search-collapse">
			<form id="data-form">
				<div class="select-list">
					<ul>
						<input type="text" name="memberId" hidden="hidden" th:value="${memberId}" readonly/>
						<li>
							状态：<select name="state" th:with="type=${@dict.getType('zhengzai_coupon_code_state')}">
							<option value="">所有</option>
							<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
						</select>
						</li>
						<li>
							<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="resetPre()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</form>
		</div>

		<div class="btn-group-sm" id="toolbar" role="group">
			<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="adam:member:export:code">
				<i class="fa fa-download"></i> 导出
			</a>
			<a class="btn btn-primary multiple disabled" onclick="invalidHandler()" shiro:hasPermission="adam:member:invalid:code">
				<i class="fa fa-edit"></i> 批量失效
			</a>
<!--			<a class="btn btn-danger multiple disabled" onclick="recoverHandler()" shiro:hasPermission="adam:member:recover:code">-->
<!--				<i class="fa fa-remove"></i> 批量退回-->
<!--			</a>-->
		</div>

		<div class="col-sm-12 select-table table-striped">
			<table id="bootstrap-table"></table>
		</div>
	</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<script th:inline="javascript">
	var invalidFlag = [[${@permission.hasPermi('adam:member:invalid:code')}]];
	var recoverFlag = [[${@permission.hasPermi('adam:member:recover:code')}]];

	var datas = [[${@dict.getType('zhengzai_coupon_code_state')}]];// 样例：[{"dictLabel":"未支付","dictValue":"0"},{"dictLabel":"已支付","dictValue":"1"},{"dictLabel":"已关闭","dictValue":"2"}]
	var memberId = [[${memberId}]];
	var prefix = ctx + "adam/member";

	$(function() {
		var options = {
			url: prefix + "/list_code",
			exportUrl: prefix + "/export_code",
			detailUrl: ctx + "adam/order/orderList/detail/{id}",
			sortName: "code",
			sortOrder: "asc",
			modalName: "会员码",
			columns: [{
				checkbox: true
			},
				{
					field: 'code',
					title: '会员码'
				},
				{
					field: 'expireAt',
					title: '到期时间'
				},
				{
					field: 'useAt',
					title: '使用时间'
				},
				{
					field: 'useOrderNo',
					title: '兑换订单号',
					formatter: function(value, row, index) {
						var actions = [];
						if ($.common.isEmpty(value)) {
							actions.push('<a href="javascript:void(0)">-</a>');
						} else {
							actions.push('<a href="javascript:void(0)" onclick="$.operate.detailTab(\'' + value + '\')">' + value + '</a>');
						}
						return actions.join('');
					}
				},
				{
					field: 'state',
					title: '状态',
					align: 'center',
					formatter: function(value, row, index) {
						return $.table.selectDictLabel(datas, value);
					}
				},
				{
					title: '操作',
					align: 'center',
					formatter: function(value, row, index) {
						var actions = [];
						if (row.state === 0) {
							actions.push('<a class="btn btn-primary btn-xs ' + (invalidFlag && row.state===0) + '" href="javascript:void(0)" onclick="invalidHandler(\'' + row.code + '\')"><i class="fa fa-edit"></i>失效</a> ');
						} else if (row.state === 1) {
							// actions.push('<a class="btn btn-danger btn-xs ' + (recoverFlag && row.state==0) + '" href="javascript:void(0)" onclick="recoverHandler(\'' + row.code + '\')"><i class="fa fa-remove"></i>退回</a>');
						}
						return actions.join('');
					}
				}]
		};
		$.table.init(options);
	});

	function resetPre() {
		$.form.reset();
	}

	function invalidHandler(code) {
		let invalidCodeUrl = prefix + '/invalid_code';
		if (code !== null && code !== undefined) {
			var data = {"memberId": memberId,"codes": code};
			$.operate.submit(invalidCodeUrl, "post", "json", data);
		} else {
			var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
			var data = {"memberId": memberId, "codes": rows.join()};
			$.operate.batchProcessing(invalidCodeUrl, '失效', data);
		}
	}

	function recoverHandler(code) {
		let recoverCodeUrl = prefix + '/recover_code';
		if (code !== null && code !== undefined) {
			var data = {"memberId": memberId,"codes": code};
			$.operate.submit(recoverCodeUrl, "post", "json", data);
		} else {
			var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
			var data = {"memberId": memberId, "codes": rows.join()};
			$.operate.batchProcessing(recoverCodeUrl, '失效', data);
		}
	}
</script>
</body>
</html>