<template>
	<view class="container">
		<view class="header-box">
			<view class="search-box">
				<u-search
					v-model="searchForm.keyword"
					:clearabled="true"
					bg-color="#E5E5E5"
					:input-style="searchInputStyle"
					placeholder="搜索您想要的内容"
				></u-search>
			</view>
			<view class="tab-box">
				<u-tabs 
					name="label"
					:list="searchType"
					:is-scroll="true"
					v-model="searchForm.typeCurrent"
					@change="onSearchTypeChange"
					font-size="24"
					:bold="false"
					inactive-color="#000000"
					active-color="#000000"
					:bar-style="{'background-color': '#2979ff'}"
					:gutter="25"
					height="45"
				></u-tabs>
			</view>
		</view>
		<u-empty
			mode="data"
			v-if="list.length === 0"
			iconSize="120"
			textSize="58"
			text="暂无数据"
		>
		</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.buyDate}}
							</view>
						</view>
					</view>
					<view class="price-box">
						<view class="text" v-if="item.status === '1'">
							实付款:&nbsp;￥{{item.buyPrice}}元
						</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-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
		<view class="buttom-block"></view>
		<view class="bottom-box">
			<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 } 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
		},
	]
	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',
			buyDate: '2023年8月8日',
			buyPrice: 9.9,
			invoice: true,
			status: 1
		},
	])
	function onItem(order) {
		console.log('点击订单', order)
		if (order.type === '8') {
			// 研究报告
		}
		if (order.type === '9') {
			// 培训课程
		}
	}
	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.message === 'success') {
				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.message === 'success') {
				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.message === 'success') {
				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 lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: $uni-text-color-inverse;
		
		.header-box {
			padding: 0 20rpx;
			background-color: $uni-text-color-inverse;
			@include topMagnet();
		}
		
		.search-box {
			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: 20rpx;
				border-bottom: 5rpx solid #E6E6E6;
				&:active {
					background-color: $uni-bg-color-hover;
				}
				.title-box {
					font-size: $uni-title-font-size-3;
					font-weight: bold;
					margin-bottom: 20rpx;
					@include text-overflow()
				}
				.main-box {
					display: flex;
					justify-content: space-between;
					padding-left: 20rpx;
					.info-box {
						width: 50%;
						.info-item-box {
							display: flex;
							font-size: $uni-font-size-3;
							color: $uni-text-color-grey;
							line-height: 30rpx;
							.label {
								width: 80rpx;
							}
						}
					}
					.price-box {
						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>