<template>
	<view class="default-box-padding bg-fff border-r-24 u-m-t-32" v-if="data.length">
		<view class=" font-bold u-p-b-32 border-bottom u-m-b-24" v-if="orderInfo.tableName">
			<text class="">桌号：</text>
			<text class="">{{orderInfo.tableName||""}}</text>
		</view>
		<view class="  color-999 border-bottom u-p-b-24">
			<text>共</text>
			<text class="color-333 "> {{goodsNumber}}</text>
			<text>份菜品</text>
		</view>
		<view class="u-m-b-20 " v-for="(order,orderIndex) in data" :key="orderIndex">
			<view class="u-p-t-24"> 第{{ order.placeNum }}次下单
			</view>
			<view class="u-m-t-24 list">
				<view class="item  u-m-b-32" v-for="(item,index) in order.info" :key="index">
					<view class="u-flex u-col-top">
						<view class="u-flex">
							<image class="img" :src="item.coverImg||item.productImg" mode=""></image>
						</view>
						<view class="u-p-l-32 u-flex-1">
							<view class="u-flex u-row-between u-col-top">

								<view class="">
									<view class="u-flex">
										<view class="tui" v-if="item.status=='return'||item.status=='refund'">
											已退
										</view>
										<view :class="{'line-th':item.status=='return'||item.status=='refund'}">{{item.name||item.productName}}
										</view>
									</view>
									<view class="u-flex u-m-t-8">
										<view class="u-m-r-20 u-flex " v-if="item.gift">
											<uni-tag text="赠送"
												custom-style="background-color: #FFF0DF; border-color: #FFF0DF; color: #FF9F2E;">
											</uni-tag>
										</view>
										<view class="u-m-r-20 u-flex" v-if="item.pack">
											<uni-tag
												custom-style="background-color: #E6F0FF; border-color: #E6F0FF; color: #318AFE;"
												size="small" text="打包" inverted type="success" />
										</view>
									</view>
									<view class="color-999 u-font-24 u-m-t-8">{{item.productSkuName||''}}</view>


									<view class="u-m-t-12 color-666 u-font-24" v-if="item.note">
										备注：{{item.note}}
									</view>
								</view>
								<view class="u-text-right u-m-t-28">
									<template v-if="item.status=='return'||item.gift||item.status=='refund'">
										<view>￥0.00</view>
										<view class="line-th color-666 ">￥{{returnTotalMoney(item)}}
										</view>
									</template>
									<template v-else>
										<view>￥{{returnTotalMoney(item)}}</view>
									</template>
									<view class="u-m-t-22 color-999 u-font-24">X{{item.number||item.num}}</view>
								</view>
							</view>
						</view>
					</view>
					<template v-if="orderInfo.status=='unpaid'&&orderInfo.useType!='dine-in-before'">
						<view class="u-flex u-row-right gap-20 u-m-t-24" v-if="item.status!='return'">
							<!-- <my-button :height="60" color="#333" plain type="cancel" shape="circle">更多操作</my-button> -->
							<my-button :width="128" :height="48" plain shape="circle" @tap="tuicai(item,index)"><text
									class="no-wrap">退菜</text></my-button>
						</view>
					</template>
					<template v-if="item.status!='refund'&&!item.gift">
						<template v-if="orderInfo.status=='closed'||orderInfo.status=='refund'">
							<view class="u-flex u-row-right gap-20 u-m-t-20" v-if="item.status!='return'">
								<my-button :width="128" :height="48" plain shape="circle"
									@tap="tuikuan(item,index)"><text class="no-wrap">退款</text> </my-button>
							</view>
						</template>
					</template>

				</view>
			</view>
			<!-- <view class="bg-gray u-p-20 u-m-t-20" v-if="orderInfo.remark">
				<view>备注</view>
				<view class="u-m-t-10">{{orderInfo.remark}}</view>
			</view> -->
		</view>

		<view class="u-m-t-32">
			<view class="u-flex u-row-between border-top  u-p-t-32">
				<view>
					<template v-if="orderInfo.status=='unpaid'">
						<view class="tag no-pay">
							未支付
						</view>
					</template>
					<template v-if="orderInfo.status=='refund'">
						<view class="tag refund">
							退款成功
						</view>
					</template>
				</view>

				<view>
					<text>小计￥</text>
					<text class="font-bold u-font-32">{{allPrice}}</text>
				</view>
			</view>

			<template v-if="orderInfo.status=='refund'">
				<view class="u-flex u-row-between u-m-t-32">
					<view>退款金额</view>
					<view class="color-999">
						<text class="">{{orderInfo.refundAmount}}</text>
					</view>
				</view>
				<view class="u-flex u-row-between u-m-t-32">
					<view>退款原因</view>
					<view class="color-999">
						<text class="">{{orderInfo.refundRemark||''}}</text>
					</view>
				</view>
				<view class="u-flex u-row-between u-m-t-32 u-p-b-24 border-bottom">
					<view></view>
					<view class="">
						<text class="">退款成功</text>
					</view>
				</view>
			</template>
			<view class="u-flex u-row-between u-m-t-20">
				<view></view>
				<view>
					<text>总计￥</text>
					<text class="font-bold u-font-32">{{to2(allPrice*1+seatFeePrice*1+packFee*1) }}</text>
				</view>
			</view>
			<view class="u-m-t-24">
				<my-button @tap="printOrder" type="cancel" :color="color.ColorMain">重新打印</my-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		computed
	} from 'vue';
	import color from '@/commons/color.js'
	import {
		hasPermission
	} from '@/commons/utils/hasPermission.js'
	const emits = defineEmits(['tuicai', 'tuikuan', 'printOrder'])

	function returnTotalMoney(item) {
		return (item.price * item.num).toFixed(2)
	}

	function to2(n) {
		if (!n) {
			return 0
		}
		return n.toFixed(2)
	}

	function tuicai(item, index) {
		emits('tuicai', item, index)
	}

	function tuikuan(item, index) {
		hasPermission('允许退款').then(res => {
			if (res) {
				emits('tuikuan', item, index)
			}
		})
	}

	function printOrder() {
		emits('printOrder')
	}
	const props = defineProps({
		orderInfo: {
			type: Object,
			default: () => {}
		},
		data: {
			type: Array,
			default: () => []
		},
		seatFee: {
			type: Object,
			default: () => {}
		}
	})

	const seatFeePrice = computed(() => {
		if (!props.seatFee.totalAmount) {
			return 0
		}
		const n = props.seatFee.totalAmount * (props.seatFee.status == 'return' ? 0 : 1)
		return n.toFixed(2)
	})

	const allPrice = computed(() => {
		const goodsPrice = props.data.reduce((prve, cur) => {
			const curTotal = cur.info.filter(v => v.gift != true && v.status !== "return").reduce((a,
				b) => {
				// return a +  b.priceAmount*1||(b.num*b.price+b.packAmount*1)
				// return a +  (b.num*b.price+b.packAmount*1)
				return a + (b.num * b.price)
			}, 0)
			return prve + curTotal
		}, 0)
		return goodsPrice.toFixed(2)

	})

	const packFee = computed(() => {
		const goodsPrice = props.data.reduce((prve, cur) => {
			const curTotal = cur.info.filter(v => v.gift != true && v.status !== "return").reduce((a,
				b) => {
				return a + b.packAmount
			}, 0)
			return prve + curTotal
		}, 0)
		return goodsPrice.toFixed(2)

	})


	const goodsNumber = computed(() => {
		let result = 0
		result = props.data.reduce((a, b) => {
			const bTotal = b.info.reduce((prve, cur) => {
				return prve + (cur.number || cur.num) * 1;
			}, 0);
			return a + bTotal
		}, 0)
		return result
	})
</script>

<style lang="scss" scoped>
	.img {
		width: 152rpx;
		height: 152rpx;
		border-radius: 6px;
	}

	.border-top {
		border-color: #F6F6F6;
	}

	.border-r-24 {
		border-radius: 24rpx;
	}

	.border-bottom {
		// border-color: rgb(240, 240, 240);
		border-color: #F6F6F6;
	}

	.line-th {
		text-decoration: line-through;
	}

	.tag {
		padding: 4rpx 8rpx 2rpx 10rpx;
		border-radius: 8rpx;
		font-size: 24rpx;

		&.no-pay {
			background-color: rgb(170, 170, 170);
			color: #fff;
		}

		&.refund {
			background-color: #FCE7E7;
			padding: 8rpx 20rpx 6rpx 22rpx;
			color: #EB4F4F;
		}
	}

	.tui {
		background-color: rgb(239, 239, 239);
		border-radius: 4rpx;
		margin-right: 6rpx;
		color: #666;
		padding: 0 4rpx;
		font-size: 20rpx;
	}
</style>