<template>
	<view>
		<!-- <view class="top-head pb20">
			<view class="top-tabbar">
				<view class="tab-item" :class="{ active: type == 0 }" @click="changeType(0)">可申请订单</view>
				<view class="tab-item" :class="{ active: type == 1 }" @click="changeType(1)">已申请订单</view>
			</view>
			<view class="d-b-c search-data f26">
				<view class="flex-1 gray9">日期区间</view>
				<picker mode="date" :value="form.startDate" :start="startDate" :end="endDate" @change="bindDateChange1">
					<view class="picker-item-data">{{ form.startDate || '开始时间' }}</view>
				</picker>
				<view style="margin: 0 60rpx;">至</view>
				<picker mode="date" :value="form.endDate" :start="startDate" :end="endDate" @change="bindDateChange2">
					<view class="picker-item-data">{{ form.endDate || '结束时间' }}</view>
				</picker>
				<view class="search-radio" @click="searchSubmit"><text class="icon iconfont icon-sousuo"></text></view>
			</view>
		</view> -->
		<!--列表-->
		<scroll-view scroll-y="true" class="scroll-Y" :style="'height:' + scrollviewHigh + 'px;'" lower-threshold="50"
			@scrolltolower="scrolltolowerFunc">
			<view class="">
				<template v-if="type == 0">
					<view v-for="(item, index) in listData" :key="index" class="listdata-item">
						<view class="border-b">
							<view class="d-b-c mb16">
								<text class="type-btn">{{ item.orderTypeVal }}</text>
								<view class="f24 gray9 flex-1">{{$t('MY_ORD_1')}}:{{ item.orderCode }}</view>
								<view class="status-btn">{{ item.orderStatusVal }}</view>
							</view>
							<view class="d-b-c">
								<template v-if="item.waresOrderList.length > 0">
									<view class="product-list ww100">
										<view v-for="(pitem, pindex) in item.waresOrderList" :key="pindex"
											class="d-s-c border-b">
											<image class="product-img" :src="pitem.cover1" mode=""></image>
											<image class="product-img" :src="ppitem.cover"
												v-for="(ppitem, ppindex) in pitem.productList" :key="ppindex" mode="">
											</image>
										</view>
									</view>
								</template>
								<view class="d-c d-c-e">
									<view class="price-color f24">
										{{currencyIcon()}}
										<text class="f32 fb">{{ item.orderAmount }}</text>
									</view>
									<!-- <view class="gray6">x1</view> -->
								</view>
							</view>
							<view class="f24 gray9 mb20">{{$t('w_0423')}}:{{ item.creationTime }}</view>
						</view>
						<view class="d-e-c pt20">
							<view class="flex-1" v-if="item.approveStatus || item.approveStatus===0">
								{{$t('MY_CK_7')}}：{{item.approveStatusVal}}</view>
							<view class="order-btn" v-if="item.selfCode" @click="viewCode(item.selfCode)">
								{{$t('MY_CK_18')}}
							</view>
							<view class="order-btn" v-if="!item.approveStatus && item.approveStatus!==0"
								@click="openApply(item, item.orderCode, item.recPhone)">{{$t('MY_CK_19')}}</view>
						</view>
					</view>
				</template>
				<template v-if="type == 1">
					<view class="order-item" v-for="(item, index) in listData" :key="index">
						<view class="orer-item-top">
							<view class="f24 gray3">{{$t('MY_ORD_1')}}: {{ item.orderCode }}</view>
							<view class="green">{{ item.approveStatusVal }}</view>
						</view>
						<view class="f24 gray9 p-20-0">
							{{$t('MY_CK_26')}}:
							<text class="fb domation">{{ item.selfCode }}</text>
						</view>
						<view>
							<view>
								<view class="f24 gray9 line-h-15 mb10">
									<text>{{$t('MY_ORD_3')}}：</text>
									<text>{{ item.creationTime }}</text>
								</view>
								<view class="f24 gray9 line-h-15 mb10">
									<text>{{$t('CK_KS_51')}}：</text>
									<text>{{ item.creationTime }}</text>
								</view>
								<view class="f24 gray9 line-h-15 mb10">
									<text>{{$t('MY_WAL_22')}}：{{ item.remark }}</text>
									<text></text>
								</view>
							</view>
							<view class="d-e-c pt20 border-t">
								<view class="order-btn" @click="viewCode(item.selfCode)">{{$t('MY_CK_18')}}</view>
							</view>
						</view>
					</view>
				</template>
			</view>
			<view class="d-c-c p30" v-if="listData.length == 0 && !loading">
				<text class="iconfont icon-wushuju"></text>
				<text class="cont">{{$t('w_0405')}}</text>
			</view>
			<uni-load-more v-else :loadingType="loadingType"></uni-load-more>
		</scroll-view>

		<template v-if="isPopup">
			<view class="pop-bg"></view>
			<view class="pop-success" v-if="isPopup">
				<view class="d-c d-b-c hh100">
					<view class="d-e-c ww100" @click="closeCode()">
						<u-icon name="close" size="34rpx" color="#999"></u-icon>
					</view>
					<view class="f36 green tc fb">{{$t('MY_CK_26')}}:{{ selfCode }}</view>
					<view class="">
						<view class="f28 gray3 line-h-2">{{$t('MY_ORD_80')}}</view>
						<view class="f26 gray6 line-h-2">{{$t('MY_CK_23')}}</view>
						<view class="f26 gray6 line-h-2 mb40">{{$t('MY_ORD_81')}}：400-111-2818</view>
					</view>

					<button class="pop-btn" @click="copyMes(selfCode)">{{$t('MY_CK_27')}}</button>
				</view>
			</view>
		</template>
		<apply v-if="isShow" :module="module" :phone="recPhone" :orderCode="orderCode" @close="closeApply"></apply>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more.vue';
	import apply from './apply.vue';
	export default {
		components: {
			uniLoadMore,
			apply
		},
		data() {
			const currentDate = this.getDate({
				format: true
			});
			return {
				isPopup: false,
				type: 0,
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				/*最后一页码数*/
				last_page: 0,
				/*当前页面*/
				page: 1,
				/*每页条数*/
				list_rows: 10,
				/*有没有等多*/
				no_more: false,
				/*是否正在加载*/
				loading: true,
				/*数据*/
				listData: [],
				form: {
					pageNum: 1,
					pageSize: 10,
					startDate: '',
					endDate: ''
				},
				selfCode: '',
				module: {},
				orderCode: '',
				recPhone: '',
				isShow: false
			};
		},
		onShow() {
			/*获取数据*/
			this.listData = [];
			this.form.pageNum = 1;
			this.getData();
		},

		computed: {
			/*加载中状态*/
			loadingType() {
				if (this.loading) {
					return 1;
				} else {
					if (this.listData.length != 0 && this.no_more) {
						return 2;
					} else {
						return 0;
					}
				}
			},
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onShow() {
			/*获取订单列表*/
			this.page = 1;
			this.listData = [];
			this.getData();
		},
		mounted() {
			this.init();
		},
		methods: {
			closeApply(e) {
				if (e) {
					this.listData = [];
					this.form.pageNum = 1;
					this.getData();
				}
				this.isShow = false;
			},
			openApply(e, orderCode, recPhone) {
				this.module = e;
				this.recPhone = recPhone;
				this.orderCode = orderCode;
				this.isShow = true;
			},
			closeCode() {
				this.isPopup = false;
			},
			viewCode(e) {
				this.isPopup = true;
				this.selfCode = e;
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindDateChange1: function(e) {
				this.form.startDate = e.detail.value;
			},
			bindDateChange2: function(e) {
				this.form.endDate = e.detail.value;
			},
			copyMes(message) {
				uni.setClipboardData({
					data: message,
					showToast: true,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: self.$t('MY_CK_29')
						});
					}
				});
			},
			changeType(e) {
				let self = this;
				if (self.loading) {
					return;
				}
				self.listData = [];
				self.form.pageNum = 1;
				self.loading = true;
				self.type = e;
				self.getData();
			},
			getData() {
				let self = this;
				self.loading = true;
				if (self.type == 0) {
					var formdata = {};
					formdata.pageNum = self.form.pageNum;
					formdata.pageSize = self.form.pageSize;
					self._get(
						'sale/api/my-order/self-list',
						formdata,
						function(res) {
							self.listData = [...self.listData,...res.rows];
							self.total = res.total;
							if (self.total < self.form.pageNum * self.form.pageSize) {
								self.no_more = true;
							}
						}, {},
						() => {
							self.loading = false;
						}
					);
				} else {
					var formdata = self.form;
					self._post(
						'sale/api/order/self-order-list',
						formdata,
						function(res) {
							self.listData = res.rows;
							self.total = res.total;
							if (self.total < self.form.pageNum * self.form.pageSize) {
								self.no_more = true;
							}
						}, {},
						() => {
							self.loading = false;
						}
					);
				}
			},
			searchSubmit: function() {
				/*获取数据*/
				this.listData = [];
				this.form.pageNum = 1;
				this.getData();
			},
			/*可滚动视图区域到底触发*/
			scrolltolowerFunc() {
				let self = this;
				if (self.no_more) {
					return;
				}
				if (self.form.pageNum * self.form.pageSize < self.total) {
					self.form.pageNum++;
					self.getData();
				} else {
					self.no_more = true;
				}
			},
			/*初始化*/
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						self.scrollviewHigh = res.windowHeight;
						// 计算组件的高度
						// let view = uni.createSelectorQuery().select('.top-head');
						// view.boundingClientRect(data => {
						// 	let h = self.phoneHeight - data.height;
						// 	self.scrollviewHigh = h;
						// }).exec();
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.top-tabbar {
		border-bottom: 1rpx solid #eee;

		.tab-item {
			color: #666;
		}

		.tab-item.active {
			font-weight: 400;
			color: #333;
		}
	}

	.order-item {
		width: 750rpx;
		background: #ffffff;
		padding: 0 18rpx 20rpx 25rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.orer-item-top {
		height: 93rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid #eee;
	}

	.listdata-item {
		// height: 370rpx;
		padding: 27rpx 23rpx 22rpx 25rpx;
		background: #ffffff;
		margin-bottom: 22rpx;
	}

	.type-btn {
		line-height: 30rpx;
		height: 30rpx;
		background: #3d3d3d;
		border-radius: 15rpx;
		padding: 0 23rpx;
		font-size: 20rpx;
		color: #ffffff;
		margin-right: 6rpx;
	}

	.status-btn {
		font-size: 24rpx;
		color: #333333;
		margin-left: 20rpx;
	}

	.product-img {
		width: 124rpx;
		height: 124rpx;
		margin-right: 20rpx;
		flex-shrink: 0;
		display: block;
		border: 15rpx;
		margin-bottom: 12rpx;
		margin-top: 12rpx;
	}

	.product-list {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: nowrap;
		overflow-x: auto;
		margin-right: 20rpx;
	}

	.order-border-btn {
		box-sizing: border-box;
		padding: 0 26rpx;
		min-width: 146rpx;
		height: 52rpx;
		line-height: 52rpx;
		text-align: center;
		border-radius: 26rpx;
		font-size: 24rpx;
		color: #999;
		border: 1rpx solid #eee;
		margin-left: 18rpx;
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.order-btn {
		box-sizing: border-box;
		padding: 0 26rpx;
		min-width: 146rpx;
		height: 52rpx;
		line-height: 52rpx;
		text-align: center;
		border-radius: 26rpx;
		font-size: 24rpx;
		color: #fff;
		border: 1rpx solid #eee;
		margin-left: 18rpx;
		background: #fb3024;
		border: 1rpx solid #fb3024;
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.pop-success {
		height: 520rpx;
	}
</style>
