<template>
	<view class="order" :style="appThemeStyle">
		<view class="search">
			<view class="box">
				<u-icon name="search" size="30" color="#8D8D8D"></u-icon>
				<input type="search" @confirm="confirm" v-model='keyWords' placeholder="输入订单编号" />
			</view>
		</view>
		<!-- tab栏 -->
		<view class="container">
			<view class="classify-list">
				<view class="list" v-for="(item,index) in tabs" :class="{'action':current==index}"
					@click="onChangeTab(item,index)" :key="index">
					<text>{{item.name}}</text>
					<text class="line" v-show="current==index"></text>
				</view>
			</view>
		</view>
	<!-- 	<u-tabs :list="tabs" bg-color="#fafafa" :is-scroll="true" height="120" font-size="32" :current="current"
			bar-width="56" bar-height="6" active-color="#3B3B3B" :show-bar='true' :duration="0.2"
			@change="onChangeTab" /> -->
		<!-- 订单列表 -->
		<view class="order-list">
			<view class="item" v-for="(item,index) in list" v-if="list.length>0">
				<view class="hd">
					<view class="a">
						<image class="icon" :src="item.storeInfo.image_url"></image>
						{{item.storeInfo.store_name}}
					</view>
					<view class="b"
						:style="{'color': (item.order_status == OrderStatusEnum.COMPLETED.value)?'#3E801D':'#F21A1C'}">
						{{item.state_text}}
					</view>
				</view>
				<view class="bd" @click="goOrderDetail(item.order_id)" v-for="(goods, idx) in item.goods" :key="idx">
					<view class="pic">
						<image :src="goods.goods_image"></image>
					</view>
					<view class="info">
						<view class="a">{{ goods.goods_name }}</view>
						<view class="b">
							￥<text>{{ goods.is_user_grade ? goods.grade_goods_price : goods.goods_price }}</text>
						</view>
					</view>
					<view class="num">
						x{{ goods.total_num }}
					</view>
				</view>
				<view class="fd">
					共{{ item.total_num }}件商品，实付款￥<text>{{ item.pay_price }}</text>
				</view>
				<view v-if="item.menushow" class="menuarea">
					<view class="mask" @click="changeMenu(index)">
					</view>
					<view class="menulist">
						<view class="" v-if="item.order_status == OrderStatusEnum.COMPLETED.value"
							@click="onInvoicing(item.order_id,item)">
							申请开票
						</view>
						<view class="" @click="onDelete(item.order_id)"
							v-if="item.order_status == OrderStatusEnum.COMPLETED.value">
							删除订单
						</view>
						<view class="" v-if="item.pay_status == PayStatusEnum.PENDING.value"
							@click="onCancel(item.order_id)">
							取消订单
						</view>
					</view>
					<view class="trangle"></view>
				</view>
				<view class="btn">
					<view class="a" @click="changeMenu(index)"
						v-if="item.order_status == OrderStatusEnum.COMPLETED.value || (item.pay_status == PayStatusEnum.PENDING.value&&item.order_status == OrderStatusEnum.NORMAL.value)">
						更多</view>
					<view class="b">
						<view class="n" v-if="item.pay_status == PayStatusEnum.SUCCESS.value && item.delivery_type == DeliveryTypeEnum.EXTRACT.value"
							@click="onExtractQRCode(item.order_id)">核销码
						</view>
						<view class="n"
							v-if="item.pay_status == PayStatusEnum.PENDING.value && item.order_status == OrderStatusEnum.NORMAL.value"
							@click="onPay(item.order_id)">立即支付</view>
						<view class="n" v-if="item.delivery_type == DeliveryTypeEnum.EXPRESS.value&&item.delivery_status == DeliveryStatusEnum.DELIVERED.value"
							@click="onLogistics(item.order_id)">查看物流
						</view>
						<view class="n n-1"
							v-if="item.delivery_type == DeliveryTypeEnum.EXPRESS.value&&item.delivery_status == DeliveryStatusEnum.DELIVERED.value && item.receipt_status == ReceiptStatusEnum.NOT_RECEIVED.value"
							@click="onReceipt(item.order_id)">确认收货</view>
						<view class="n"
							v-if="(item.pay_status == PayStatusEnum.SUCCESS.value && item.delivery_status == ReceiptStatusEnum.NOT_RECEIVED.value)||item.order_status == OrderStatusEnum.CANCELLED.value"
							@click="handleTargetGoods(item.goods[0].goods_id)">再次购买</view>
						<view class="n"
							v-if="item.delivery_type == DeliveryTypeEnum.EXPRESS.value&&item.pay_status == PayStatusEnum.SUCCESS.value && item.delivery_status == DeliveryStatusEnum.NOT_DELIVERED.value"
							@click="onCdDia(item.order_id)">催发货</view>
						<!-- 		<view class="n"
										v-if="item.pay_status == PayStatusEnum.SUCCESS.value && item.delivery_status == DeliveryStatusEnum.NOT_DELIVERED.value"
										@click="onAdress(order.goods_id)">修改地址</view> -->
						<!-- <view class="n n-1"
								v-if="item.order_status == OrderStatusEnum.COMPLETED.value && item.is_comment == 0"
								@click="handleTargetComment(item.order_id)">评价</view> -->
					</view>
				</view>
			</view>
			<u-empty text="暂无数据显示哦～" v-else mode="list"></u-empty>
		</view>
		<u-modal v-model="deleteShow" :show-cancel-button="true" :closable='false' @cancel="deleteShow=false"
			@confirm="handleOk" title="确认删除订单？" confirm-color="#F55349">
			<view class="slot-content order-modal">
				<view class="content">删除之后订单无法恢复，无法处理您的售后问题 ，请慎重考虑</view>
				<view class="desc" @click="toggleImage()">
					<image v-if="isToggle" src="/static/invoice/select.png"></image>
					<image v-else src="/static/invoice/select-on.png"></image>删除后将拼单信息设置为匿名
				</view>
			</view>
		</u-modal>
		<!-- 核销二维码弹窗 -->
		<view class="bigCode" v-if="showQRCodePopup" @click="showQRCodePopup=false">
			<view class="cm">
				<view class="cm_text">
					自提核销二维码
				</view>
				<view class="txm">
					<image :src="qrcodeImage" @click="clickImg" mode="" class="codeImg"></image>
				</view>
				<view class="cline">
					<u-line border-style="dashed" color="#D0D0D0" />
				</view>
				<view class="sm">
					可前往门店扫码完成操作
				</view>
			</view>
		</view>
		<!-- 催单弹框 -->
		<view class="dia" v-if="cdDia">
			<view class="diaMain">
				<image src="/static/order/ld.png" mode="" class="ld"></image>
				<view class="cdTitle">
					催单成功
				</view>
				<view class="cdInfo">
					商家已收到您的催单提醒，您精心挑选的商品正在配送中，属于正常配送时效内，请您耐心等待，谢谢！
				</view>
				<view class="iKnow" @click="meKone()">
					我知道了
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		DeliveryStatusEnum,
		DeliveryTypeEnum,
		OrderStatusEnum,
		PayStatusEnum,
		ReceiptStatusEnum
	} from '@/common/enum/order'
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
	import {
		getEmptyPaginateObj,
		getMoreListData
	} from '@/core/app'
	import * as OrderApi from '@/api/order'

	// 每页记录数量
	const pageSize = 15

	// tab栏数据
	const tabs = [{
		name: `全部`,
		value: 'all'
	}, {
		name: `待付款`,
		value: 'pay'
	}, {
		name: `待发货`,
		value: 'delivery'
	}, {
		name: `待收货`,
		value: 'receipt'
	}, {
		name: `已完成`,
		value: 'complete'
	}, {
		name: `已取消`,
		value: 'cancel'
	}, {
		name: `退货/售后`,
		value: 'refund'
	}, ]

	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				cdDia: false,
				isToggle: false,
				deleteShow: false,
				orderId: '',
				// 枚举类
				DeliveryStatusEnum,
				DeliveryTypeEnum,
				OrderStatusEnum,
				PayStatusEnum,
				ReceiptStatusEnum,

				// 当前页面参数
				options: {
					dataType: 'all'
				},
				// tab栏数据
				tabs,
				// 当前标签索引
				current: 0,
				// 订单列表数据
				list: [],
				keyWords: '',
				total: 1,
				pageNum: 1,
				// 控制onShow事件是否刷新订单列表
				// 核销二维码弹窗
				showQRCodePopup: false,
				// 核销二维码图片url (通过后端获取)
				qrcodeImage: '',
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// 初始化当前选中的标签
			this.initCurTab(options)
			this.getOrderList();
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			// 注册全局事件订阅: 是否刷新订单列表
			uni.$on('syncRefresh', canReset => {
				this.list = []
				this.getOrderList()
			})
		},

		/**
		 * 生命周期函数--监听页面的卸载
		 */
		onUnload() {
			// 卸载全局事件订阅
			uni.$off('syncRefresh')
		},
		onReachBottom() {
			if (this.list.length <= this.total) {
				this.pageNum++;
				this.getOrderList();
			}
		},
		methods: {
			confirm() {
				// 刷新订单列表
				this.list = []
				app.getOrderList()
			},
			changeMenu(index) {
				let idList = []
				this.list.forEach(item => {
					item.menushow = false;
					idList.push(item.response.data.fileInfo.file_id)
				});
				this.list[index].menushow = !this.list[index].menushow
			},
			toggleImage() {
				this.isToggle = !this.isToggle
			},
			// 初始化当前选中的标签
			initCurTab(options) {
				const app = this
				if (options.dataType) {
					const index = app.tabs.findIndex(item => item.value == options.dataType)
					app.current = index > -1 ? index : 0
					console.log(app.current, 'KKKKKKK')
				}
			},

			/**
			 * 获取列表
			 */
			async getOrderList() {
				uni.showLoading({
					title: "加载中"
				})
				let {
					status,
					message,
					data
				} = await OrderApi.list({
					dataType: this.getTabValue(),
					page: this.pageNum,
					keyword: this.keyword
				});
				if (status == 200) {
					uni.hideLoading();
					this.list = this.list.concat(data.list.data)
					this.total = data.list.total
				}
			},

			// 获取当前标签项的值
			getTabValue() {
				return this.tabs[this.current].value
			},

			// 切换标签项
			onChangeTab(item,index) {
				const app = this
				app.list = []
				// 设置当前选中的标签
				app.current = index
				if (index == 6) {
					uni.navigateTo({
						url: '/pages/refund/index'
					})
				} else {
					// 刷新订单列表
					app.getOrderList()
				}

			},
			// 取消订单
			onCancel(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '确认要取消该订单吗？',
					success(o) {
						if (o.confirm) {
							OrderApi.cancel(orderId)
								.then(result => {
									// 显示成功信息
									app.$toast(result.message)
									// 刷新订单列表
									app.onRefreshList()
								})
						}
					}
				});
			},
			handleCancel() {
				app.deleteShow = false
			},
			onDelete(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '删除之后订单无法恢复，无法处理您的售后问题 ，请慎重考虑，确认要删除该订单吗？',
					success(o) {
						if (o.confirm) {
							OrderApi.del(orderId)
								.then(result => {
									// 显示成功信息
									app.$toast(result.message)
									// 刷新订单列表
									app.onRefreshList()
								})
						}
					}
				});
			},
			// 删除订单
			onDelete(orderId) {
				const app = this
				app.deleteShow = true
				app.orderId = orderId
			},

			// 确认收货
			onReceipt(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '确认收到商品了吗？',
					success(o) {
						if (o.confirm) {
							OrderApi.receipt(orderId)
								.then(result => {
									// 显示成功信息
									app.$success(result.message)
									// 刷新订单列表
									app.onRefreshList()
								})
						}
					}
				});
			},

			// 获取核销二维码
			onExtractQRCode(orderId) {
				const app = this
				OrderApi.extractQrcode(orderId, {
						channel: app.platform
					})
					.then(result => {
						app.qrcodeImage = result.data.qrcode
						app.showQRCodePopup = true
					})
			},
			// 跳转到订单评价页
			handleTargetComment(orderId) {
				this.$navTo('pages/order/comment/index', {
					orderId
				})
			},
			// 点击去开票
			onInvoicing(orderId, order) {
				uni.navigateTo({
					url: '/pages/invoice/edit?orderId=' + orderId + '&source=' + 0 + "&order=" + JSON.stringify(
						order)
				})
			},
			// 点击去退款
			onRefund(orderId) {
				this.$navTo('pages/order/refund/refund', {
					orderId
				})
			},
			// 申请售后
			onSales(orderId) {
				this.$navTo('pages/order/refund/index', {
					orderId
				})
			},
			// 点击去物流
			onLogistics(orderId) {
				this.$navTo('pages/order/express/index', {
					orderId
				})
			},
			// 点击去支付
			onPay(orderId) {
				this.$navTo('pages/checkout/cashier/index', {
					orderId
				})
			},
			// 跳转到订单详情页
			goOrderDetail(orderId) {
				this.$navTo('pages/order/detail', {
					orderId
				})
			},
			onCdDia(orderId) {
				const app = this
				OrderApi.orderfast(orderId)
					.then(result => {
						if (result.status == 200) {
							this.cdDia = true
						}

					})

			},
			// 跳转到商品详情页面
			handleTargetGoods(goodsId) {
				this.$navTo('pages/goods/detail', {
					goodsId
				})
			},
			// 跳转到订单评价页
			handleTargetComment(orderId) {
				this.$navTo('pages/order/comment/index', {
					orderId
				})
			}

		},

	}
</script>

<style lang="scss" scoped>
	::v-deep .u-empty {
		padding: 100rpx 0;
	}

	::v-deep .u-tab-bar {
		bottom: 12rpx !important;
		background-color: rgb(255, 98, 87) !important;
	}

	.container {
		background-color: #fff;
	}

	.classify-list {
		white-space: nowrap;
		width: 100%;
		height: 100rpx;
		overflow-x: auto;
		overflow-y: hidden;

		.list {
			position: relative;
			display: inline-block;
			width: 18%;
			height: 100%;
			line-height: 100rpx;
			text-align: center;

			text {
				font-size: 28rpx;
				color: #3B3B3B;

			}

			.line {
				position: absolute;
				left: 50%;
				bottom: 10rpx;
				// width: 60%;
				// height: 8rpx;
				background: linear-gradient(to right, #f8f893, #fe9d00);
				// border-radius: 10rpx;
				transform: translate(-50%, 0);

				width: 60upx;
				height: 8upx;
				background: #FF6257;
				border-radius: 29px 29px 29px 29px;
				opacity: 1;
			}
		}

		.action {
			text {
				font-size: 32rpx;
				opacity: 1;
			}
		}
	}

	.order {
		.order-list{
			margin-top: 20rpx;
		}
		.receive {
			width: 100%;
			background: #F8F8F8;
			margin-top: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #3B3B3B;
			line-height: 33rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
			padding: 20rpx 22rpx;
		}

		.search {
			width: 750rpx;
			height: 118rpx;
			background: #FFF;
			padding: 20rpx 26rpx 20rpx 48rpx;
			box-sizing: border-box;

			.box {
				padding: 0 25rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				background: #F3F3F3;
				border-radius: 60rpx;
				height: 78rpx;
				line-height: 78rpx;

				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 22rpx;
				}

				input {
					flex: 1;
					font-size: 28rpx;
					line-height: 30rpx;
					margin-left: 15rpx;
				}
			}
		}

		&-list {
			padding: 0 30rpx 30rpx;
			overflow: hidden;

			.item {
				background: #FFFFFF;
				padding: 0 25rpx;
				border-radius: 10rpx;
				margin-top: 30rpx;
				position: relative;

				&:first-child {
					margin-top: 0;
				}

				.menuarea {
					width: 100%;
					height: 100%;
				}

				.menulist {
					background-color: #FFF;
					border: 1rpx solid #F4F4F4;
					width: 150rpx;
					min-height: 50rpx;
					position: absolute;
					left: 0rpx;
					bottom: 80rpx;
					border-radius: 14rpx;
					text-align: center;
					padding: 10rpx 0;
				}

				.menulist view {
					width: 100%;
					height: 50rpx;
					color: #333;
					font-size: 24rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					line-height: 50rpx;

				}

				.trangle {
					width: 0px;
					height: 0px;
					position: absolute;
					border: 10rpx solid #FFF;
					border-top-color: #FFF;
					border-bottom-color: transparent;
					border-left-color: transparent;
					border-right-color: transparent;
					left: 16rpx;
					bottom: 60rpx;
				}

				.hd {
					padding: 24rpx 0;
					display: flex;
					align-items: center;
					justify-content: space-between;
					overflow: hidden;

					.a {
						display: flex;
						align-items: center;
						font-size: 32rpx;
						font-weight: 500;
						color: #333333;
						padding-right: 30rpx;
						position: relative;
						word-break: break-all;

						image {
							width: 40rpx;
							height: 40rpx;
							margin-right: 24rpx;
						}
					}

					.b {
						font-size: 28rpx;
						font-weight: 400;
						color: #F21A1C;
					}
				}

				.bd {
					display: flex;
					align-items: flex-start;
					overflow: hidden;
					margin-bottom: 30rpx;

					.pic {
						width: 162rpx;
						height: 148rpx;
						border-radius: 0px 0px 0px 0px;
						margin-right: 30rpx;

						image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}

					.info {
						flex: 1;
						overflow: hidden;

						.a {
							text-align: left;
							font-size: 28rpx;
							font-weight: 400;
							color: #1E1E1E;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							overflow: hidden;
						}

						.b {
							text {
								font-size: 36rpx;
								font-weight: 600;
							}

							margin-top: 12rpx;
							font-size: 24rpx;
							color: #F21A1C;
						}
					}

					.num {
						margin-left: 20rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: #1E1E1E;
					}
				}

				.fd {
					display: flex;
					align-items: baseline;
					justify-content: flex-end;
					font-size: 24rpx;
					font-weight: 500;
					color: #1E1E1E;

					text {
						font-size: 36rpx;
					}
				}

				.btn {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 30rpx 0;
					overflow: hidden;

					.a {
						font-size: 28rpx;
						font-weight: 400;
						color: #A59E9E;
					}

					.b {
						flex: 1;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						overflow: hidden;

						.n {
							width: 148rpx;
							line-height: 56rpx;
							background: #FFFFFF;
							border-radius: 56rpx;
							opacity: 1;
							border: 1px solid #DFDFDF;
							margin-left: 6rpx;
							text-align: center;
							font-size: 24rpx;
							font-weight: 400;
							color: #1E1E1E;

							&-1 {
								border-color: #F21A1C;
								color: #F21A1C;
							}
						}
					}
				}

			}
		}

		&-modal {
			padding: 50rpx;
			position: relative;

			.content {
				font-size: 28rpx;
				font-weight: 400;
				line-height: 50rpx;
				color: #4C4C4C;
			}

			.desc {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				font-weight: 400;
				color: #9F9F9F;
				justify-content: center;
				margin-top: 20rpx;

				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 20rpx;
				}
			}
		}
	}

	.dia {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.7);
		z-index: 99;
		display: flex;
		align-items: center;
		justify-content: center;

		.diaMain {
			width: 560rpx;
			height: 432rpx;
			background: linear-gradient(180deg, #FFE4E4 0%, #FFFFFF 100%);
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			opacity: 1;
			position: relative;

			.ld {
				width: 100rpx;
				height: 100rpx;
				position: absolute;
				top: -50rpx;
				left: 230rpx;
			}

			.cdTitle {
				width: 128rpx;
				height: 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #000000;
				line-height: 44rpx;
				margin: 80rpx 0 0 216rpx;
			}

			.cdInfo {
				width: 498rpx;
				height: 120rpx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #727272;
				line-height: 38rpx;
				margin-left: 48rpx;
				margin-top: 28rpx;
			}

			.iKnow {
				width: 412rpx;
				height: 72rpx;
				background: #F55349;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				opacity: 1;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 72rpx;
				margin: 36rpx 0 0 74rpx;
				text-align: center;
			}
		}
	}

	.bigCode {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(65, 65, 65, 0.46);
		display: flex;
		align-items: center;
		justify-content: center;

		.cm {
			width: 560rpx;
			height: 714rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			opacity: 1;
			padding-top: 44rpx;

			.cm_text {
				width: 100%;
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 500;
				color: #424242;
				line-height: 42rpx;
				text-align: center;
			}

			.txm {
				display: flex;
				justify-content: center;
				margin-top: 30rpx;

				.codeImg {
					width: 394rpx;
					height: 394rpx;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					opacity: 1;
				}

			}

			.cline {
				padding: 0 34rpx;
				margin: 46rpx 0 30rpx 0;
			}

			.sm {
				width: 100%;
				height: 34rpx;
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #979797;
				line-height: 28rpx;
				text-align: center;
			}

		}
	}
</style>
