<!-- 订单状态详情 -->
<template>
	<view class="order-detail-page">
		<HeaderCom></HeaderCom>
		<scroll-view scroll-y="true" class="scroll-view">
			<view class="main">
				<view class="shop">
					<view class="contain">
						<image v-if="detailData.coverResource " :src="detailData.coverResource.url" mode="aspectFit" class="shop-icon"></image>
					</view>
					<view class="detail">
						<view class="name">{{detailData.name}}</view>
						<!-- <view class="remark"></view> -->
						<view class="price">
							<view class="value">¥{{detailData.totalAmount}}</view>
							<view class="num">x{{typeObj[detailData.type]}}</view>
						</view>
					</view>
				</view>
				<!-- <view class="item">
					<view class="label">运费</view>
					<view class="value">¥0</view>
				</view> -->
				<view class="item">
					<view class="label">优惠</view>
					<view class="value">¥{{Number(detailData.discountAmount)}} </view>
				</view>
				<view class="item">
					<view class="label">实付金额</view>
					<view class="value">¥{{Number(detailData.totalAmount)}} </view>
				</view>
				<view class="item-big">
					<view class="label">实际支付</view>
					<view class="value">¥{{Number(detailData.totalAmount)}} </view>
				</view>
			</view>
			<view class="main">
				<view class="item">
					<view class="label">订单编号</view>
					<view class="value" @click="copy(detailData.no)">{{detailData.no}}</view>
				</view>
				<view class="item">
					<view class="label">下单时间</view>
					<view class="value">{{detailData.paidAt ? formatDateTime(detailData.paidAt) : '-'}}</view>
				</view>
				<view class="item">
					<view class="label">订单状态</view>
					<view class="value">{{statusObj[detailData.status]}}</view>
				</view>
				<view class="item">
					<view class="label">支付方式</view>
					<view class="value">{{detailData.method ? paidWith[detailData.method] : '-'}}</view>
				</view>
				<!-- <view class="item">
					<view class="label">配送服务</view>
					<view class="value">¥ {{detailData.deliveryFee || 0}}</view>
				</view> -->
			</view>
		</scroll-view>
		<view class="footer" v-if="detailData.status === 'WAITING_FOR_RECEIPT' || detailData.status === 'SHIPPED' || detailData.status === 'RECEIVED' || detailData.status === 'COMPLETED'">
			<view class="btn" @click="handleCustomer">联系客服</view>
				<view class="btn" @click="logistics"
					v-if="detailData.status === 'WAITING_FOR_RECEIPT' || detailData.status === 'SHIPPED' || detailData.status === 'RECEIVED'">
					物流信息</view>
				<!-- <view class="btn " v-if="detailData.status === 'COMPLETED'" @click.stop="againProduct">再次购买</view> -->
			<view class="btn btn-active" @click="orderReceipt"
				v-if="detailData.status === 'WAITING_FOR_RECEIPT' || detailData.status === 'RECEIVED'">
				确认收货</view>
		</view>
		<popup-view v-model="openShow" mode="bottom" border-radius="15" zIndex="1">
			<view class="dlg-main" @click.stop="">
				<view class="header">
					<view class="title">物流明细</view>
					<image src="" mode="" class="close" @click="openShow = false"></image>
				</view>
				<view class="popup-scroll" v-if="detailData.productCoverResource">
					<logistics :wlInfo="detailData.logistics" :img="detailData.productCoverResource.url"
						:name="detailData.productName" :address="address" :logisticsStatus="detailData.logisticsStatus">
					</logistics>
				</view>
				<!-- <image src="@/static/order/close.png" mode="" class="popup-close" @click.stop="close"></image> -->
			</view>
		</popup-view>
		<!-- 所有页面的弹框 -->
		<page-popup page="/pages/mine/orderBoxDetail"></page-popup>
	</view>
</template>

<script>
	import { myMixins } from "@/mixins/mixins.js";
	import {
		orderDetail,
		delOrder,
		orderReceive,
		orderCancel,
		orderReceiptApi,
		orderIdDetail,
	} from '@/API/order.js'
	
	import {
		customerApi
	} from '@/API/user.js'
	import {
		mathAdd
	} from '@/utils/mathUtils.js'
	import {
		uniCopy
	} from '@/utils/uniCopyUtils.js'
	import logistics from '@/components/xinyu-logistics/xinyu-logistics.vue'
	import HeaderCom from '@/pages/components/header.vue'
	export default {
	mixins: [myMixins],
		data() {
			return {
				no: '',
				detailData: {
					logistics: {}
				},
				totalPrice: "",
				statusList: [{
						label: "待付款",
						value: 'WAITING_FOR_PAYMENT'
					},
					{
						label: "待发货",
						value: 'WAITING_FOR_SHIPMENT'
					},
					{
						label: "已发货",
						value: 'SHIPPED'
					},
					{
						label: "待收货",
						value: 'WAITING_FOR_RECEIPT'
					},
					{
						label: "已收货",
						value: 'RECEIVED'
					},
					{
						label: "已完成",
						value: 'COMPLETED'
					},
					{
						label: "已取消",
						value: 'CANCELED'
					},
					{
						label: "已退款",
						value: 'REFUNDED'
					},
					{
						label: "已回收",
						value: 'RECOVERED'
					},
				],
				statusObj: {
					"WAITING_FOR_PAYMENT": "待付款",
					"WAITING_FOR_SHIPMENT": "待发货",
					"SHIPPED": "已发货",
					"WAITING_FOR_RECEIPT": "待收货",
					"RECEIVED": "已收货",
					"COMPLETED": "已完成",
					"CANCELED": "已取消",
					"REFUNDED": "已退款",
					"RECOVERED": "已回收"
				},
				paidWith: {
					'POINTS': '积分支付',
					'ALIPAY': '支付宝支付',
					'WECHAT': '微信支付',
					'COIN':'代币支付',
					'NONE': "无需支付"
				},
				openShow: false,
				address: '',
				customer:'',
				id:'',//为盲盒详情
				
				typeObj: {
					'SINGLE':1,
					'TRIPLE':3,
					'PENTA':5
				},
			}
		},
		components: {
			logistics,HeaderCom
		},
		onLoad(opation) {
			this.no = opation.no
			if(opation.id){
				this.id = opation.id
				this.getDetail()
			}else{
				this.no = opation.no
				this.getDetail()
			}
			this.getCustomer()
		},
		methods: {
			getCustomer() {
				customerApi().then(res => {
					if (res.code === 200) {
						this.customer = res.data
					} else {
						this.$api.msg(res.message)
					}
				})
			},
			handleCustomer() {
				window.open(this.customer)
			},
			orderReceipt() {
				uni.showLoading()
				orderReceiptApi({
					id: this.detailData.id
				}).then(res => {
					uni.hideLoading(),
						this.$api.msg(res.message)
					if (res.code === 200) {
						this.getDetail()
					}
				})
			},
			formatDateTime(value) {
				if (!value) return '';
				const date = new Date(value);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				const seconds = String(date.getSeconds()).padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			goBack() {
				uni.navigateBack()
			},
			againProduct() {
				if(this.id){
					uni.navigateTo({
						url: `/pages/index/boxDetail?id=${this.detailData.id}`
					})
				}else{
					uni.navigateTo({
						url: `/pages/shop/detail?id=${this.detailData.productId}`
					})
				}
				
			},
			makePhoneCall() {
				uni.makePhoneCall({
					phoneNumber: '18620545056'
				});
			},
			copy(no) {
				uni.setClipboardData({
					data: no,
				})
			},
			// 获取详情
			getDetail() {
				uni.showLoading()
				if(this.id){
					orderIdDetail(this.id).then(res => {
						uni.hideLoading()
						if (res.code === 200) {
							this.detailData = res.data
							this.address = this.detailData.receiver.address
					
						} else {
							this.$api.msg(res.message)
						}
					}).catch(err => {
						uni.hideLoading()
					})
				}else{
					orderDetail(this.no, {
						no: this.no
					}).then(res => {
						uni.hideLoading()
						if (res.code === 200) {
							this.detailData = res.data
							this.address = this.detailData.receiver.address
					
						} else {
							this.$api.msg(res.message)
						}
					}).catch(err => {
						uni.hideLoading()
					})
				}
				
			},

			// 删除订单
			del() {
				uni.showLoading()
				orderCancel({
					no: this.detailData.no
				}).then(res => {
					uni.hideLoading()
					this.$api.msg(res.message)
					if (res.code === 200) {
						this.getDetail()
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
			// 确认收货
			receive() {
				uni.showLoading()
				orderReceive({
					no: this.detailData.no
				}).then(res => {
					uni.hideLoading()
					this.$api.msg(res.message)
					if (res.code === 200) {
						this.getDetail()
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
			buy() {
				uni.navigateTo({
					url: `/pages/order/confirm?id=${this.detailData.productId}&orderId=${this.detailData.id}&quantity=${this.detailData.quantity}&skuId=${this.detailData.skuId}&no=${this.detailData.no}&paidWith=${this.detailData.paidWith}`
				})
			},
			logistics() {
				this.detailData.logistics['logisticsStatus'] = this.detailData.logisticsStatus
				this.openShow = true
			},
			close() {
				this.openShow = false
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.order-detail-page {
		padding: 112rpx 32rpx 32rpx 32rpx;
		.scroll-view{
			height: calc(100vh - 320rpx);
			.main{
				width: 686rpx;
				background: #FFFFFF;
				border-radius: 24rpx;
				margin-bottom: 24rpx;
				padding: 0 24rpx;
				.shop{
					width: 100%;
					display: flex;
					align-items: center;
					padding: 24rpx 0;
					border-bottom: 1px solid #F2F2F2;
					
					.contain{
						width: 160rpx;
						height: 160rpx;
						border-radius: 0 24rpx 24rpx 0;
						background-image: url('@/static/new/shop-bg.png');
						background-size: contain;
						display: flex;
						align-items: center;
						justify-content: center;
						.shop-icon{
							width: 142rpx;
							height: 132rpx;
						}
					}
					.detail{
						margin-left: 24rpx;
						height: 160rpx;
						.name{
							width: 444rpx;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
							font-weight: 500;
							font-size: 28rpx;
							color: #000000;
						}
						.remark{
							width: 444rpx;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
							font-weight: 500;
							font-size: 24rpx;
							color: #999999;
							margin-top: 8rpx;
						}
						.price{
							// margin-top: 34rpx;
							margin-top: 76rpx;
							display: flex;
							align-items: center;
							justify-content: space-between;
							width: 444rpx;
							.value{
								font-weight: bold;
								font-size: 32rpx;
								color: #000000;
							}
							.num{
								font-weight: 500;
								font-size: 28rpx;
								color: #999999;
							}
						}
					}
				}
				.item{
					// width: 686rpx;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 24rpx;
					.label{
						font-weight: 500;
						font-size: 28rpx;
						color: #666666;
					}
					.value{
						font-weight: 500;
						font-size: 28rpx;
						color: #000000;
					}
				}
				.item-big{
					// width: 686rpx;
					height: 96rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 24rpx;
					border-top: 1px solid #F2F2F2;
					.label{
						font-weight: 500;
						font-size: 28rpx;
						color: #666666;
					}
					.value{
						font-weight: 800;
						font-size: 36rpx;
						color: #000000;
					}
				}
			}
		}
		.footer{
			display: flex;
			align-items: center;
			justify-content: end;
			width: 100%;
			height: 144rpx;
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			padding: 0 32rpx;
			.btn{
				width: 212rpx;
				height: 96rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				border: 2px solid #EBEBEB;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 500;
				font-size: 32rpx;
				color: #666666;
				margin-left: 24rpx;
			}
			.active-btn{
				background: linear-gradient( 90deg, #39B2FF 0%, #3354FF 100%), #D9D9D9;
				border: none;
				color: #FFFFFF;
			}
		}
		.dlg-main {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			.header{
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 750rpx;
				height: 96rpx;
				padding: 0 32rpx;
				.title{
					font-weight: bold;
					font-size: 32rpx;
					color: #000000;
				}
				.close{
					width: 48rpx;
					height: 48rpx;
				}
			}
			.popup-scroll {
				width: 706rpx;
				height: 900rpx;
				background-color: #fff;
				border-radius: 20rpx;
				overflow-y: scroll;
			}
		
			.popup-close {
				margin-top: 36rpx;
				width: 54rpx;
				height: 54rpx;
			}
		}
	}
</style>