<template>
	<view class="mealsBox rad8 flex column m-r-24 m-l-24 m-b-24 p-24 bg-w boxshadow">
		<view class="flex">
			<view class="flex3 ofh">
				<view class="fz24 fcb m-t-12">订单号：{{order.order_sn}}</view>
				<view class="fz24 fcb">{{order.order_time | parseTime}}</view>
				<view class="fz32 l-h-18 fb">取餐点：{{order.receive_site_name}}</view>
				<view class="m-t-12">
					<scroll-view class="scroll-view_H" scroll-x="true">
						<view class="scroll-view-item_H m-r-12" v-for="(ps,index) in order.product_snapshot"
							:key="index">
							<u--image :src="ps.spu_photo[0]" width="147rpx" height="110rpx" radius="4rpx" />
						</view>
					</scroll-view>
				</view>
			</view>
			<view class="flex1 flex column md">
				<view class="fz28 fb fcm l-h-18">{{ orderStatusDisplay }}</view>
				<view class="priceBox fz36 fb">
					¥{{fullPrice}}
				</view>
				<view class="fz24 fcb">共{{order.total_num}}件</view>
			</view>
		</view>
		<view class="m-t-24 flex mr">
			<navigator v-if="showPayButton" :url="`/pages/shop/pay?order_sn=${order.order_sn}&fullPrice=${fullPrice}`"
				hover-class="navigator-hover">
				<view class="nullBut bg-m fci rad8 fz28 flex mac mc">
					立即支付
				</view>
			</navigator>
			<view v-if="showPickupNumber" class="numberBut bg-m fci rad8 fz28 fb flex mac mc m-l-24">
				{{ foodSn }}
			</view>
			<navigator v-if="showAfterSaleButton" class="m-l-24" :url="`/pages/order/refund?order_sn=${order.order_sn}`"
				hover-class="navigator-hover">
				<view class="nullBut border fcb rad8 fz28 flex mac mc">
					申请售后
				</view>
			</navigator>
			<navigator v-if="showRefundDetailsButton" class="m-l-24"
				:url="`/pages/order/refProcess?order_sn=${order.order_sn}`" hover-class="navigator-hover">
				<view class="nullBut border fcb rad8 fz28 flex mac mc">
					退款详情
				</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	import {
		parseTime,
		formatPrice
	} from '@/utils/index.js'
	export default {
		name: 'orderItem',
		data() {
			return {
				foodSn: '取餐号', //取餐号 文字轮播
				intervalId: null, // 存储定时器ID，用于清除定时器
			}
		},
		created() {
			this.intervalId = setInterval(() => {
				this.foodSn = this.foodSn === '取餐号' ? this.order.food_sn : '取餐号'
			}, 1200)
		},
		destroyed() {
			// 清除定时器，避免内存泄漏
			clearInterval(this.intervalId);
		},
		computed: {
			//订单总价格
			fullPrice() {
				return this.formatPrice(Number(this.order.transport_price) + Number(this.order.pack_price) + Number(this
					.order.total_price))
			},
			// 是否显示 "立即支付" 按钮
			showPayButton() {
				return this.order.main_status === '未付款';
			},
			// 是否显示 "取餐号"
			showPickupNumber() {
				return this.order.main_status === '已付款' && this.order.refund_status === '未申请';
			},
			// 是否显示 "申请售后" 按钮
			showAfterSaleButton() {
				return this.order.main_status === '已付款' && this.order.refund_status === '未申请';
			},
			// 是否显示 "退款详情" 按钮
			showRefundDetailsButton() {
				return this.order.refund_status !== '未申请' && this.order.main_status !== '已完成';
			},
			// 显示订单状态
			orderStatusDisplay() {
				if (this.order.main_status === '未付款') {
					return '待支付';
				}
				if (this.order.main_status === '已付款' && this.order.refund_status === '未申请') {
					return this.order.shipment_status;
				}
				// 如果订单的主状态是已退款，直接返回已退款
				if (this.order.main_status === '已退款') {
					return '已退款';
				}

				// 如果订单的主状态是已付款，并且退款状态为已同意，返回商家同意退款
				if (this.order.main_status === '已付款') {
					if (this.order.refund_status === '已同意') {
						return '商家同意退款';
					}

					// 如果退款状态为主动退，返回商家退单
					if (this.order.refund_status === '主动退') {
						return '商家退单';
					}
				}

				// 根据退款状态显示其他状态
				if (this.order.refund_status === '申请中') {
					return '申请中';
				}

				if (this.order.refund_status === '拒绝退') {
					return '商家拒绝退款';
				}

				// 默认返回空
				return '';
			},
		},
		methods: {
			formatPrice //价格格式化
		},
		props: {
			order: {
				type: Object,
				deep: true
			}
		},
		filters: {
			parseTime, //日期 格式化
			formatPrice //价格 格式化
		},
	}
</script>

<style lang="scss" scoped>
	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.scroll-view-item_H {
		display: inline-block;
		height: 110rpx;
		line-height: 110rpx;
	}

	.priceBox {
		margin-top: 85rpx;
	}
</style>