<template>
	<view class="container">
		<view class="header-box">
			<view class="search-box">
				<u-search v-model="searchForm.keyword" :clearabled="true" placeholder="搜索您想要的内容"
					@search="onSearchConfirm" @custom="onSearchConfirm"></u-search>
			</view>
			<view class="tab-box" style="padding: 40rpx 0 35rpx;">
				<u-tabs name="label" :list="searchType" :is-scroll="true" v-model="searchForm.typeCurrent"
					@change="onSearchTypeChange" font-size="32" inactive-color="#000000" bg-color="'rgb(141, 204, 255)'"
					active-color="#ffffff" :bar-style="{'background-color': '#2979ff'}"
					:active-item-style="{'background-color': '#2979ff', 'border-radius':'30rpx'}" :gutter="40"
					height="55" :show-bar="false" duration="0"></u-tabs>
			</view>
		</view>
		<!-- <view class="header-content"></view> -->
		<u-empty mode="data" v-if="list.length === 0" iconSize="120" textSize="58" text="暂无数据" margin-top="400">
		</u-empty>
		<view class="list-box">
			<view class="list-item-box" v-for="item in list" :key="item.id" @click="onItem(item)">
				<view class="title-box">
					{{item.title}}
				</view>
				<view class="main-box">
					<view class="info-box">
						<view class="info-item-box">
							<view class="label">
								订单编号:
							</view>
							<view class="text">
								{{item.orderNo}}
							</view>
						</view>
						<view class="info-item-box">
							<view class="label">
								订单时间:
							</view>
							<view class="text">
								{{item.date}}
							</view>
						</view>
					</view>
					<view class="price-box">
						<view class="text" v-if="item.status === '1'">
							实付款:&nbsp;￥{{item.amount}}元
						</view>
						<view class="button error" v-if="item.status === '0'" @click.stop="onPay(item)">
							待支付
						</view>
						<view class="button" v-if="item.status === '2'">
							订单关闭
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more v-if="list.length !== 0" v-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
		<view class="buttom-block" v-if="false"></view>
		<view class="bottom-box" v-if="false">
			<view class="button" @click="onToInvoiceApplication">
				发票申请
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import {
		query,
		byId
	} from '@/api/order.js'
	import {
		msgError,
		msgSuccess
	} from '@/utils/common'

	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const searchType = [{
			label: '全部',
			value: null
		},
		{
			label: '研究报告',
			value: 8
		},
		{
			label: '培训课程',
			value: 9
		},
		{
			label: '个人会费',
			value: 1
		},
		{
			label: '机构会费',
			value: 2
		}
	]
	const pageNum = ref(1)
	const pageSize = ref(10)
	const visualLoadMore = ref(false)
	const loadMoreStatus = ref('more')
	const searchForm = ref({
		keyword: '',
		type: null,
		typeCurrent: 0,
		pageNumber: 1,
		pageSize: 10
	})

	const list = ref([
		// {
		// 	id: '01',
		// 	title: '【市场研究】2024年11月广州市中介促成二手住宅市场交易简报',
		// 	type: 8,
		// 	orderNo: '123456789987654321',
		// 	date: '2023年8月8日',
		// 	amount: 9.9,
		// 	invoice: true,
		// 	status: 1
		// },
	])

	function onItem(order) {
		if (order.status === '2') {
			uni.showToast({
				title: '订单已关闭',
				icon: 'none'
			})
			return
		}
		uni.showLoading({
			title: '加载中...'
		})
		byId(order.id).then(res => {
			if (res && res.code === 0) {
				uni.hideLoading()
				const objId = res.data.objId
				const title = res.data.title
				if (res.data.objType === '8') {
					uni.navigateTo({
						url: `/pages/reportDetail/reportDetail?id=${objId}&title=${title}`
					})
				}
				if (res.data.objType === '9') {
					uni.navigateTo({
						url: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${objId}&title=${title}`
					})
				}
			} else {
				uni.hideLoading()
			}
		}).catch(e => {
			uni.hideLoading()
		})
	}

	function onPay(order) {
		wx.requestPayment({
			timeStamp: order.prepay.timeStamp,
			nonceStr: order.prepay.nonceStr,
			package: order.prepay.package,
			signType: order.prepay.signType,
			paySign: order.prepay.paySign,
			success(res) {
				msgSuccess('支付成功')
				onSearch()
			},
			fail(res) {
				// console.log('支付失败', res)
				const errMsg = res.errMsg
				if (errMsg.indexOf('fail cancel')) {
					msgError('已取消支付')
				}
			}
		})
	}

	function onSearchConfirm() {
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}

	function onSearchClear() {
		searchForm.value.keyword = null
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}

	function onSearch() {
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
			}
		})
	}

	function onSearchTypeChange(val) {
		searchForm.value.type = searchType[val].value
		onSearchConfirm()
	}

	// 前往发票申请
	function onToInvoiceApplication() {
		uni.navigateTo({
			url: '/pages/InvoiceApplication/InvoiceApplication'
		})
	}
	onPullDownRefresh((e) => {
		searchForm.value.pageNumber = 1
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
				uni.showToast({
					title: '刷新成功',
					icon: 'success',
					duration: 2000
				})
			}
			uni.stopPullDownRefresh()
		})
	})

	onReachBottom(async () => {
		if (loadMoreStatus.value === 'noMore') {
			uni.showToast({
				title: '没有更多啦＞﹏＜',
				icon: 'none'
			})
			return
		}
		visualLoadMore.value = true
		loadMoreStatus.value = 'loading'
		searchForm.value.pageNumber++
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				if (res.data) {
					if (res.count >= list.value.length) {
						if (list.value.length === res.count) {
							loadMoreStatus.value = 'noMore'
							visualLoadMore.value = true
						} else {
							list.value.push(...res.data)
							loadMoreStatus.value = 'more'
							visualLoadMore.value = false
						}
					} else {
						loadMoreStatus.value = 'noMore'
						visualLoadMore.value = true
					}
				} else {
					loadMoreStatus.value = 'noMore'
					visualLoadMore.value = true
				}
			}
		})
	})

	onLoad(() => {
		onSearch()
	})
</script>
<style>
	::v-deep(.u-tab-item) {
		margin-right: 20rpx;
	}

	::v-deep(.u-empty) {
		height: auto !important;
	}
</style>
<style lang="scss" scoped>
	$certificate-width: 220rpx;
	$certificate-height: 300rpx;

	.container {
		height: 100vh;
		width: 100vw;
		background: rgb(141, 204, 255);
		background: -moz-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -webkit-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -o-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -ms-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: linear-gradient(180deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);

		$header-bg-color: #8dccff;
		// $transition-duration: 0.4s;
		.header-box {
			padding: 0 20px;
			// 应用顶部磁吸效果
			@include topMagnet();
			// 初始状态：透明背景
			background-color: $header-bg-color;
		}
		.search-box {
			padding-top: 10rpx;

			// margin-bottom: 20rpx;
			::v-deep(.u-search) {
				// background-color: #e5e5e5;
				border-radius: 50rpx;

				.u-action {
					width: 18%;
					background-color: $uni-color-primary;
					border-radius: 50rpx;
					color: $uni-text-color-inverse;
					margin-right: 8rpx;
					font-size: 28rpx;
					line-height: 50rpx;
					letter-spacing: 3rpx;
					text-align: center;
				}
			}
		}

		.list-box {
			padding: 0 20rpx;

			.list-item-box {
				padding: 40rpx 20rpx;
				margin: 35rpx 0;
				/* border-bottom: 5rpx solid #E6E6E6; */
				box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
				background-color: #fff;
				border-radius: 20rpx;

				&:first-child {
					// padding: 0 20rpx 40rpx;
					margin-top: 0;
				}

				&:active {
					background-color: $uni-bg-color-hover;
				}

				.title-box {
					font-size: 28rpx;
					font-weight: bold;
					margin-bottom: 20rpx;
					@include text-overflow()
				}

				.main-box {
					display: flex;
					justify-content: space-between;
					// padding-left: 20rpx;
					position: relative;

					.info-box {
						// width: 50%;
						width: 100%;
						flex: 1 1 auto;

						.info-item-box {
							display: flex;
							font-size: 22rpx;
							color: $uni-text-color-grey;
							line-height: 40rpx;

							.label {
								flex: 0 0 auto;
								// width: 80rpx;
								margin-right: 10rpx;
							}
						}
					}

					.price-box {
						// flex: 0 0 auto;
						position: absolute;
						right: 0;
						bottom: 0;
						display: flex;
						align-items: flex-end;
						text-align: right;
						color: $uni-color-error;
						font-size: $uni-title-font-size-3;
						font-weight: bold;

						.button {
							background-color: #CCCCCC;
							color: $uni-text-color-inverse;
							padding: 6rpx 25rpx;
							border-radius: $uni-card-border-radius;
						}

						.error {
							background-color: $uni-color-error;
						}
					}
				}
			}
		}

		.buttom-block {
			height: 100rpx;
			padding-bottom: env(5rpx + safe-area-inset-bottom, 0);
		}

		.bottom-box {
			text-align: center;
			font-size: $uni-title-font-size-2;
			background-color: $uni-bg-color-grey;
			position: fixed;
			bottom: 0;
			width: 100%;

			.button {
				width: 100%;
				height: 100rpx;
				line-height: 100rpx;
				color: $uni-color-primary;
				letter-spacing: 2rpx;
				border: 1rpx solid #E9E9E9;

				&:active {
					background-color: $uni-bg-color-hover;
				}
			}
		}
	}
</style>