<template>
	<view class="page-container">
		<view class="condition-box">
			<view class="condition-input-box">
				<view class="condition-input-container">
					<icon class="ml-20" type="search" size="20"></icon>
					<input class="condition-input-text" :value="keywords" @input="inputKeywords"
						@confirm="doSearchKeyWord" placeholder-style="font-size:28rpx;" type="text"
						confirm-type="search" placeholder="搜索" />
					<view class="icon-clear size-32 mr-10" v-if="keywords != ''" @click="clearSearch"></view>
				</view>
			</view>
		</view>
		<!-- #ifdef MP-TOUTIAO || MP-WEIXIN -->
		<ContainerLoading :loadingVisible="loadingState" style="height: 85vh;margin-top: 120rpx;"
			@doRefresh="doRefreshList">
		<!-- #endif -->
			<!-- #ifndef MP-TOUTIAO || MP-WEIXIN -->
			<ContainerLoading :loadingVisible="loadingState" style="height: 85vh;margin-top: 80rpx;"
				@doRefresh="doRefreshList">
			<!-- #endif -->
				<scroll-view scroll-y style="height: 85vh;" :lower-threshold="100" refresher-background="#FFFFFF00"
					@scrolltolower="doLoadMore">
					<view class="order-box">
						<block v-for="(item,index) in orderList" :key="index">
							<view class="order-item">
								<view class="order-title">
									<view class="order-no">订单号:{{item.orderNo}}</view>
									<view class="order-status">{{orderStatus(item.orderStatus)}}</view>
								</view>
								<block v-for="(detail,dIndex) in item.orderDetails" :key="dIndex">
									<view class="order-content">
										<view class="order-caption">{{detail.productName}}</view>
										<view class="order-types">
											<view class="or-type">{{proType(detail.productType)}}</view>
											<view class="or-count">数量:{{detail.quantity}}</view>
											<view class="or-price">单价:{{detail.unitPrice/100}}</view>
											<view class="or-total">总金额:{{item.totalAmount/100}}</view>
										</view>
										<view class="order-remark-box">
											<view class="order-remark-title">订单备注</view>
											<view class="order-remark-content">{{detail.notes}}</view>
										</view>
										<view class="order-remark-box">
											<view class="order-remark-title">商品描述</view>
											<view class="order-remark-content">{{detail.productDescription}}</view>
										</view>
									</view>
								</block>
							</view>
						</block>
						<uni-load-more :status="hasMore"></uni-load-more>
					</view>
				</scroll-view>
			</ContainerLoading>
	</view>
</template>

<script>
	import ContainerLoading from '@/components/container-loading.vue'
	import UserApi from '@/common/js/net/UserApi.js'
	import {
		orderStatus,
		proType
	} from '@/common/js/conver.js'
	export default {
		components: {
			ContainerLoading
		},
		data() {
			return {
				orderList: [], //订单列表
				pageData: {
					page: 1,
					rows: 10,
					keywords: ''
				}, //检索参数
				loadingState: 'loading', //加载状态
				listRefreshTrig: false, //list刷新状态
				isLoadMore: false, //加载更多的状态
				hasMore: true, //是否有更多数据
				keywords: '', //搜索关键字
				loadMore: 'more', //加载更多 more loading noMore
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: '我的订单'
			})
			uni.setNavigationBarColor({
				frontColor: '#000000', // 必写项，字体颜色仅支持#ffffff和#000000
				backgroundColor: '#F0F0F0', // 传递的颜色值，仅支持十六进制颜色
				animation: { // 可选项
					duration: 500,
					timingFunc: 'linear'
				}
			})
			this.doRefreshList()
		},
		onPullDownRefresh() {
			this.doRefreshList()
			uni.stopPullDownRefresh()
		},
		methods: {
			orderStatus,
			proType,
			//监听搜索输入框
			inputKeywords(e) {
				this.keywords = e.detail.value
			},
			//清除搜索内容
			clearSearch() {
				const _self = this
				_self.keywords = ''
				_self.doRefreshList()
			},
			//发起搜索
			doSearchKeyWord() {
				const _self = this
				_self.doRefreshList()
			},
			//获取我的订单列表 isRefresh false 加载更多  true 刷新
			doGetMineOrderList(isRefresh) {
				const _self = this
				_self.orderList = isRefresh ? [] : _self.orderList
				_self.loadingState = isRefresh ? 'loading' : ''
				UserApi.doGetMineOrder(_self.pageData)
					.then(res => {
						console.log(res);
						var status = 'success'
						status = res.rows && res.rows.length > 0 ? 'success' : 'empty'
						_self.loadingState = isRefresh ? status : ''
						_self.orderList = _self.orderList.concat(res.rows)
						_self.listRefreshTrig = false
						_self.isLoadMore = false
						_self.hasMore = _self.orderList.length < res.total ? 'more' : 'noMore'
						_self.loadMore = _self.hasMore ? 'more' : 'noMore'
					})
					.catch(err => {
						_self.loadingState = 'error'
						_self.listRefreshTrig = false
						_self.isLoadMore = false
						_self.hasMore = 'more'
						_self.loadMore = 'more'
					})
			},
			//刷新列表
			doRefreshList() {
				console.log('正在刷新...')
				const _self = this
				_self.listRefreshTrig = true
				_self.loadingState = 'loading'
				_self.hasMore = true
				_self.loadMore = 'more'
				_self.pageData.page = 1
				_self.pageData.keywords = _self.keywords
				_self.isLoadMore = false
				_self.doGetMineOrderList(true)
			},
			//加载更多
			doLoadMore() {
				//判断是否正在加载中 与是否存在更多数据
				const _self = this
				if (_self.isLoadMore || _self.hasMore == 'noMore') {
					return
				}
				_self.isLoadMore = true
				_self.hasMore = 'loading'
				_self.pageData.page = ++_self.pageData.page
				_self.pageData.keywords = _self.keywords
				_self.doGetMineOrderList(false)
			},
		}
	}
</script>

<style lang="scss">
	.search-box {
		display: flex;
		flex-direction: row;
	}

	.search-container {
		position: relative;
		align-self: center;
		border-radius: 5rpx;
		background-color: rgba(255, 255, 255, 1);
		font-family: -regular;
		padding: 5px;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		width: 100vw;
	}

	.search-input {
		width: 100%;
		box-sizing: border-box;
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: center;
	}

	.search-input::after {
		content: '';
		position: absolute;
		left: 10px;
		top: 50%;
		transform: translateY(-50%);
		width: 20px;
		height: 20px;
		margin-top: -1px;
		background-size: cover;
		background-image: url('data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSI2NCA2NCA4OTYgODk2IiB3aWR0aD0iMTQiIGhlaWdodD0iMTkiIHN0eWxlPSIiIGZpbHRlcj0ibm9uZSI+CiAgICAKICAgIDxnPgogICAgPHBhdGggZD0iTTkwOS42IDg1NC41TDY0OS45IDU5NC44QzY5MC4yIDU0Mi43IDcxMiA0NzkgNzEyIDQxMmMwLTgwLjItMzEuMy0xNTUuNC04Ny45LTIxMi4xLTU2LjYtNTYuNy0xMzItODcuOS0yMTIuMS04Ny45cy0xNTUuNSAzMS4zLTIxMi4xIDg3LjlDMTQzLjIgMjU2LjUgMTEyIDMzMS44IDExMiA0MTJjMCA4MC4xIDMxLjMgMTU1LjUgODcuOSAyMTIuMUMyNTYuNSA2ODAuOCAzMzEuOCA3MTIgNDEyIDcxMmM2NyAwIDEzMC42LTIxLjggMTgyLjctNjJsMjU5LjcgMjU5LjZhOC4yIDguMiAwIDAgMCAxMS42IDBsNDMuNi00My41YTguMiA4LjIgMCAwIDAgMC0xMS42ek01NzAuNCA1NzAuNEM1MjggNjEyLjcgNDcxLjggNjM2IDQxMiA2MzZzLTExNi0yMy4zLTE1OC40LTY1LjZDMjExLjMgNTI4IDE4OCA0NzEuOCAxODggNDEyczIzLjMtMTE2LjEgNjUuNi0xNTguNEMyOTYgMjExLjMgMzUyLjIgMTg4IDQxMiAxODhzMTE2LjEgMjMuMiAxNTguNCA2NS42UzYzNiAzNTIuMiA2MzYgNDEycy0yMy4zIDExNi4xLTY1LjYgMTU4LjR6IiBmaWxsPSJyZ2JhKDIwNCwyMDQsMjA0LDEpIj48L3BhdGg+CiAgICA8L2c+CiAgPC9zdmc+');
	}


	.content-container {
		height: 86vh;
		margin-top: 50px;
		width: 100vw;
	}


	.order-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding-bottom: 30px;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		flex-grow: 1;
	}

	.order-item:nth-of-type(n+2) {
		margin-top: 15px;
	}

	.order-title {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 8px 17px;
		background-color: #7AC483;
		border-top-left-radius: 5rpx;
		border-top-right-radius: 5rpx;
	}

	.order-no {
		color: rgba(39, 28, 0, 1);
		font-size: 12px;
		text-align: left;
	}

	.order-status {
		color: rgba(39, 28, 0, 1);
		font-size: 12px;
		text-align: left;
	}

	.order-content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		background-color: white;
		padding: 17px;
		border-bottom-left-radius: 5rpx;
		border-bottom-right-radius: 5rpx;
	}

	.order-caption {
		line-height: 25px;
		color: rgba(16, 16, 16, 1);
		font-size: 18px;
		text-align: left;
		font-family: SourceHanSansSC-medium;
	}

	.order-types {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin-top: 15px;
	}

	.or-type {
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: PingFangSC-regular;
	}

	.or-count {
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: PingFangSC-regular;
	}

	.or-price {
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: PingFangSC-regular;
	}

	.or-total {
		color: rgba(16, 16, 16, 1);
		font-size: 16px;
		text-align: left;
		font-family: PingFangSC-medium;
	}

	.order-remark-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 10px;
	}

	.order-remark-title {
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: left;
		font-family: PingFangSC-regular;
	}

	.order-remark-content {
		color: rgba(16, 16, 16, 1);
		font-size: 20rpx;
		text-align: left;
		font-family: PingFangSC-light;
		border-radius: 5px;
		background-color: rgba(239, 239, 239, 1);
		flex: 1;
		padding: 7px 10px;
		margin-left: 10px;
	}

	.icon-position {
		width: 42rpx;
		height: 42rpx;
		margin-right: 10rpx;
	}
</style>