<!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="couponId" hidden="hidden" th:value="${couponId}" readonly/>
						<li>
							状态：<select name="state" th:with="type=${@dict.getType('zhengzai_coupon_code_state')}" onchange="selectChange()" class="selectChange">
							<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 multiple disabled" onclick="$.table.exportExcel()" shiro:hasPermission="candy:coupon:code:export">
				<i class="fa fa-download"></i> 导出
			</a>
			<a class="btn btn-primary multiple disabled selectChangeShowInvalid" style="display: none" onclick="invalidHandler()" shiro:hasPermission="candy:coupon:code:invalid">
				<i class="fa fa-edit"></i> 批量失效
			</a>
			<a class="btn btn-danger multiple disabled selectChangeShowRecover" style="display: none" onclick="recoverHandler()" shiro:hasPermission="candy:coupon:code:recover">
				<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('candy:coupon:code:invalid')}]];
	var recoverFlag = [[${@permission.hasPermi('candy:coupon:code:recover')}]];
	var datas = [[${@dict.getType('zhengzai_coupon_code_state')}]];
	var couponId = [[${couponId}]];
	var prefix = ctx + "candy/coupon/code";

	$(function() {
		var options = {
			url: prefix + "/list",
			exportUrl: prefix + "/export",
			queryParams: queryParams,
			sortName: "ccode",
			sortOrder: "asc",
			modalName: "数据",
			columns: [{
				checkbox: true
			},
				{
					field: 'ccode',
					title: '券码'
				},
				{
					field: 'redeemMobile',
					title: '用户手机号',
					formatter: function(value, row, index) {
						var listClass = $.common.equals("default", row.listClass) || $.common.isEmpty(row.listClass) ? "" : "badge badge-" + row.listClass;
						return $.common.sprintf("<span class='%s'>%s</span>", listClass, value);
					}
				},
				{
					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 + '" href="javascript:void(0)" onclick="invalidHandler(\'' + row.ccode + '\')"><i class="fa fa-edit"></i>失效</a> ');
						} else if (row.state === 1) {
							actions.push('<a class="btn btn-danger btn-xs ' + recoverFlag + '" href="javascript:void(0)" onclick="recoverHandler(\'' + row.ccode + '\')"><i class="fa fa-remove"></i>退回</a>');
						}
						return actions.join('');
					}
				}]
		};
		$.table.init(options);
	});

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

	function queryParams(params) {
		var search = $.table.queryParams(params);
		search.dictType = $("#dictType").val();
		return search;
	}
	
	function invalidHandler(ccode) {
		if (ccode !== null && ccode !== undefined) {
			var data = {"couponId":couponId,"ccodes": ccode};
			$.operate.submit('code/invalid', "post", "json", data);
		} else {
			$.operate.batchProcessingForCouponCode(couponId, 1);
		}
	}

	function recoverHandler(ccode) {
		if (ccode !== null && ccode !== undefined) {
			var data = {"couponId":couponId,"ccodes": ccode};
			$.operate.submit('code/recover', "post", "json", data);
		} else {
			$.operate.batchProcessingForCouponCode(couponId, 2);
		}
	}

	function selectChange() {
		let val = $(".selectChange").val();
		console.log(typeof val);
		if (val === '0') {
			$(".selectChangeShowInvalid").show();
		} else if (val === '1') {
			// $(".selectChangeShowRecover").show();
		} else {
			$(".selectChangeShowInvalid").hide();
			$(".selectChangeShowRecover").hide();
		}
	}
</script>
</body>
</html>