<template>
	<view class="container">
		<my-header-card
			:options="{ name: '限时折扣', intro: '批量设置商品折扣', icon: 'https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/2/bc360b13e5734821a2de2f0494b72a8b.png' }"
		></my-header-card>
		<view class="list">
			<view class="item" v-for="item in tableData.list" :key="item.id">
				<view class="head">
					<view class="left">
						<text class="t">优先级：{{ item.sort }}</text>
					</view>
					<view class="status-wrap">
						<u-tag :type="statusFilter(item.status, 'type')" plain :text="statusFilter(item.status, 'label')"></u-tag>
					</view>
				</view>
				<!-- <view class="row">
					<text class="b">活动名称：</text>
					<text class="t">{{ item.title }}</text>
				</view> -->
				<view class="row">
					<text class="b">活动时间：</text>
					<text class="t">{{ item.validStartTime }}至{{ item.validEndTime }}</text>
				</view>
				<view class="row">
					<text class="t">{{ item.useDays }}</text>
				</view>
				<view class="row">
					<text class="t" v-if="item.useTimeType == 'custom'" style="margin-right: 20px">
						{{ convertTimeFormat(item.useStartTime) }} - {{ convertTimeFormat(item.useEndTime) }}
					</text>
					<text class="t">折扣：{{ item.discountRate }}%</text>
				</view>
				<view class="footer" v-if="isMainShop()">
					<view class="btn">
						<u-button shape="circle" @click="delHandle(item)">删除</u-button>
					</view>
					<view class="btn">
						<u-button shape="circle" type="primary" @click="editorHandle(item)">编辑</u-button>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="tableData.status"></u-loadmore>
		<my-footer-btn confirmText="添加" @confirm="go.to('PAGES_LIMIT_DISCOUNT_ADD')"></my-footer-btn>
	</view>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { onLoad, onShow, onReachBottom } from '@dcloudio/uni-app';
import { limitTimeDiscountPage, limitTimeDiscountDel } from '@/http/api/market/index.js';
import go from '@/commons/utils/go.js';
import { convertTimeFormat } from '@/utils/index.js';
import { isMainShop, isMarketShow } from '@/store/account.js';
// 去编辑
function editorHandle(item) {
	uni.setStorageSync('limitDiscountObj', item);
	go.to('PAGES_LIMIT_DISCOUNT_ADD');
}

// 删除限时折扣
function delHandle(item) {
	uni.showModal({
		title: '注意',
		content: '确定要删除吗？',
		success: async (res) => {
			if (res.confirm) {
				try {
					uni.showLoading({
						title: '删除中...',
						mask: true
					});
					await limitTimeDiscountDel(item.id);
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					});

					let index = tableData.list.findIndex((val) => val.id === item.id);
					tableData.list.splice(index, 1);
				} catch (error) {
					console.log(error);
				}
				uni.hideLoading();
			}
		}
	});
}

const statusList = ref([
	{
		value: 1,
		label: '未开始',
		type: 'info'
	},
	{
		value: 2,
		label: '进行中',
		type: 'success'
	},
	{
		value: 3,
		label: '已结束',
		type: 'info'
	},
	{
		value: -1,
		label: '当前时间不可用',
		type: 'primary'
	}
]);

function statusFilter(status, key = 'label') {
	return statusList.value.find((val) => val.value == status)[key];
}

const tableData = reactive({
	loading: false,
	page: 1,
	size: 10,
	list: [],
	status: 'loading'
});

// 滚动到底部
onReachBottom(() => {
	if (tableData.status != 'nomore') {
		tableData.page++;
		limitTimeDiscountPageAjax();
	}
});

// 获取限时折扣分页
async function limitTimeDiscountPageAjax() {
	try {
		uni.showLoading({
			title: '加载中...',
			mask: true
		});
		const res = await limitTimeDiscountPage({
			page: tableData.page,
			size: 10,
			startTime: '',
			endTime: ''
		});
		if (tableData.page == 1) {
			tableData.list = res.records;
		} else {
			tableData.list.push(...res.records);
		}
		if (res.pageNumber == res.totalPage) {
			tableData.status = 'nomore';
		}
	} catch (error) {
		console.log(error);
	}
	setTimeout(() => {
		uni.hideLoading();
	}, 500);
}

onShow(() => {
	tableData.page = 1;
	limitTimeDiscountPageAjax();
});

onLoad(() => {});
</script>

<style>
page {
	background-color: #f7f7f7;
}
</style>
<style scoped lang="scss">
.container {
	padding: 0 28upx 28upx;
}
.list {
	padding: 28upx 0;
	.item {
		background-color: #fff;
		padding: 20upx;
		border-radius: 20upx;
		&:not(:first-child) {
			margin-top: 28upx;
		}
		.head {
			display: flex;
			justify-content: space-between;
			.left {
				.t {
					color: #999;
					font-size: 28upx;
				}
			}
		}
		.row {
			margin-bottom: 12upx;
			.b {
				font-size: 28upx;
				font-weight: bold;
			}
			.t {
				font-size: 28upx;
			}
		}
		.footer {
			display: flex;
			gap: 28upx;
			justify-content: flex-end;
			.btn {
				width: 200upx;
			}
		}
	}
}
</style>
