<template>
	<view class="main-page">
		<cc-couponList colors="#ff7131" :couponList="couponList" @itemClick="jumpNext"></cc-couponList>
	</view>
	<view v-if="couponList.length == 0">
		<up-empty mode="coupon">
		</up-empty>
	</view>
</template>

<script setup>
	import sheep from '@/sheep';
	import config from '@/common/config/config.js';
	import {
		reactive,
		ref,
	} from "vue";
	import {
		onLoad,
		onShow,
		onReachBottom,
		onUnload,
		onHide,
		onTabItemTap,
		onPullDownRefresh
	} from "@dcloudio/uni-app";

	// 
	const orderData = reactive({ //请求的订单列表
		orderList: [],
		orderAskIng: []
	})

	const couponList = ref([
		// {
		// 	name: '满105减5',
		// 	dates: '2023-07-09 2023-08-02',
		// 	frequency: 3
		// 	status: 0,
		// 	money: 105,
		// 	sub: 5
		// },
	])
	const page = ref(1) //触底加载增加的页数
	const totalPages = ref() //总页数，用于判断是否加载完


	const jumpNext = async (item) => {

		let data = {
			// payPrice: item.payPrice,
			setMealId: item.id,
			bindType: uni.getStorageSync("version") == '车队版' ? 2 : 1
		}
		await uni.$request({
			url: config.baseUrl + 'app-api/cloud/set-meal-records/create/set-meal-records',
			method: 'POST',
			data: data
		}).then(res => {
			// console.log(res.data.data.payOrderId, '==========>');
			if (res.data.code == 0) {
				handle_pay_money(res.data.data)
			} else {
				uni.showToast({
					title: res.data.msg,
					duration: 2000,
					icon: 'error'
				});
			}

		}).catch((err) => {
			uni.showToast({
				title: '加载失败',
				duration: 2000,
				icon: 'error'
			});
		})
	}

	const handle_pay_money = async (e) => {
		sheep.$router.go('/pagesCenter/pay/index', {
			id: e.payOrderId,
			orderType: 'recharge',
		});
	}
	// 
	onShow(() => {
		page.value = 1
		getOrder(0)
	})
	// onReachBottom(async () => {
	// 	if (page.value < totalPages.value) {
	// 		page.value += 1
	// 		await getOrder(1)
	// 		orderData.orderList = orderData.orderList.concat(orderData.orderAskIng)
	// 	} else {
	// 		uni.$u.toast('没有更多啦');
	// 	}
	// })


	// 
	const getOrder = async (type) => {
		let types = ''
		// if (uni.getStorageSync("version") == '个人版') {
		// 	types = '?type=1'
		// }
		console.log(config.baseUrl, '接口地址');
		console.log(types, '接口参数');
		await uni.$request({
			url: config.baseUrl + 'app-api/cloud/set-meal/page' + types
		}).then(res => {
			console.log(res, '接口返回信息');
			let str_array = []
			for (let item of res.data.data.list) {
				let str = {
					id: item.id,
					name: item.name,
					dates: item.effective,
					frequency: item.frequency,
					status: 0,
					money: item.payPrice / 100,
					sub: 5
				}
				str_array.push(str)
			}
			couponList.value = str_array
			// if (type == 1) {
			// 	orderData.orderAskIng = res.data.data.list //触底加载的列表
			// } else {
			// 	orderData.orderList = res.data.data.list //渲染的列表
			// }
			// totalPages.value = res.data.data.totalPage //总页数	
		}).catch((err) => {
			uni.showToast({
				title: '加载失败',
				duration: 2000,
				icon: 'error'
			});
		})
	}
</script>

<style lang='less' scoped>
	.main-page {
		padding: 30rpx;
	}
</style>