<template>
	<view class="bg-fff item" @click="toDetail">
		<view class="u-flex u-p-b-22 border-bottom  u-row-between u-col-center">
			<view class="u-flex   u-col-bottom">
				<template v-if="data.tableName">
					<view class="u-flex u-col-center">
						<view class="u-font-40 color-333">{{data.tableName}}</view>
						<view class="line" style="height: 16px;"></view>
					</view>
				</template>
				<view class="">{{data.masterId}}</view>
			</view>
			<view class="u-flex">
				<view>
					<text :class="[data.status]">{{returnStatus(data.status)}}</text>
				</view>
				<view class="line"></view>
				<view class="  color-main">
					<text>
						{{sendTypeFilter(data.dineMode)}}
					</text>
				</view>
			</view>
		</view>
		<view class="u-m-t-26">
			<view class="u-flex u-col-bottom u-font-24 color-999">
				<up-avatar :size="33"></up-avatar>
				<view class="u-m-l-16">{{formatTime(data.createTime)}}</view>
			</view>
			<view class="u-m-t-32">
				<view class="u-font-32">{{goosZhonglei}}种商品，共{{goodsNumber}}件</view>
				<view class="border-bottom u-p-b-32">
					<view class="" v-for="(item,index) in data.goods" :key="index">
						<view class="u-flex u-row-between u-col-top u-m-t-32" v-if="item.productId!=-999">
							<view>
								<view class=""> {{item.productName}}</view>
								<view class="color-999 u-font-24 u-m-t-8">
									{{item.skuName}}
								</view>
							</view>
							<view class="u-flex u-flex-1 u-row-right">
								<view>×{{item.num}}</view>
								<template v-if="item.userCouponId">
									<view class="u-text-right u-relative" :style="computedPriceStyle()">
										<text class="line-th">￥{{item.payAmount}}</text>
										<view class="u-absolute" style="bottom: 100%;right: 0;">
											￥0
										</view>
									</view>
								</template>
								
								<template v-else>
									<view class="u-text-right u-relative" :style="computedPriceStyle()">
										<text>￥{{item.payAmount}}</text>
									</view>
								</template>
							</view>
						</view>

					</view>
				</view>

			</view>
			<view class="border-bottom u-p-t-32 u-p-b-32"
				v-if="data.packFee>0||(data.seatInfo&&data.seatInfo.priceAmount>0)">
				<view class="u-flex u-row-between    u-col-top" v-if="data.packFee>0">
					<view class="no-wrap u-m-r-32">打包费</view>
					<view>￥{{data.packFee||0}}</view>
				</view>
				<view style="height: 32rpx;" ></view>
				<view class="u-flex u-row-between u-col-top" v-if="data.seatInfo&&data.seatInfo.priceAmount>0">
					<view class="no-wrap u-m-r-32">{{data.seatInfo.productName}}</view>
					<view>￥{{data.seatInfo.priceAmount}}</view>
				</view>
			</view>
			<view class="border-bottom u-p-b-32 u-m-t-32"
				v-if="data.fullCouponDiscountAmount>0||data.productCouponDiscountAmount>0||data.pointsDiscountAmount>0">
				<view class="u-flex u-row-between    u-col-top" v-if="data.fullCouponDiscountAmount>0">
					<view class="no-wrap u-m-r-32">满减券抵扣</view>
					<view class="color-red">-￥{{data.fullCouponDiscountAmount||0}}</view>
				</view>
				<view class="u-flex u-row-between u-m-t-32  u-col-top" v-if="data.productCouponDiscountAmount>0">
					<view class="no-wrap u-m-r-32">商品券抵扣</view>
					<view class="color-red">-￥{{data.productCouponDiscountAmount||0}}</view>
				</view>
				<view class="u-flex u-row-between u-m-t-32  u-col-top" v-if="data.pointsDiscountAmount>0">
					<view class="no-wrap u-m-r-32">积分抵扣</view>
					<view class="color-red">-￥{{data.pointsDiscountAmount||0}}</view>
				</view>
			</view>



			<view class="u-flex u-row-between border-bottom u-m-t-32 u-p-b-32 u-col-top">
				<view class="no-wrap u-m-r-32">订单备注</view>
				<view>{{data.remark||'无'}}</view>
			</view>
			<view class="u-m-t-32">
				<view class="u-flex u-row-right">
					<text>总计￥</text>
					<text class="font-bold u-font-32">{{data.originAmount}}</text>
				</view>
				<view class="u-flex u-row-right u-m-t-24">
					<view class="print" @click.stop="print(item)">重新打印</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import dayjs from 'dayjs';
	import orderEnum from '@/commons/orderEnum.js'
	import go from '@/commons/utils/go.js'
	import { computed, reactive, ref, watch } from 'vue';
	const emits = defineEmits(['printOrder'])
	const props = defineProps({
		data: {
			type: Object,
			default: () => {
				return {
					packFee: 0,
					seatInfo: {
						productName: '客座费',
						priceAmount: 0
					},
					goods: []
				}
			}
		},
		index: {
			type: [String, Number],
			default: 0
		}
	})
	let $goodsMap = {}
	let goosZhonglei = ref(0)
	let goodsNumber = ref(0)

	const priceSize = 9
	let minWidth=ref(36)
	
	/**
	 * 计算菜品价格
	 * @param {Object} item
	 */
	function goodsPriceAmount(item) {
		const total=(item.payAmount * item.num).toFixed(2)
		const minW=total.length * priceSize + 15
		minWidth.value=minW<minWidth.value?minWidth.value:minW
		return total
	}

	function computedPriceStyle() {
		return {
			'min-width':minWidth.value + 'px'
		}
	}
	
	function goodsMapInit() {
		for (let i in props.data.goods) {
			const goods = props.data.goods[i]
			if ($goodsMap.hasOwnProperty(goods.productId)) {
				$goodsMap[goods.productId] += goods.num * 1
				goodsNumber.value += goods.num * 1
			} else {
				$goodsMap[goods.productId] = goods.num * 1
				goosZhonglei.value += 1
				goodsNumber.value += goods.num * 1
			}
		}
	}
	goodsMapInit()
	watch(() => props.data.goods.length, (newval) => {
		goodsNumber.value = 0
		goodsMapInit()
	})


	function formatTime(time) {
		return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
	}

	function returnStatus(status) {
		const item = orderEnum.status.find(v => v.key == status)
		return item ? item.label : ''
	}

	function sendTypeFilter(t) {
		if (t) {
			const item = orderEnum.dineMode.find(item => item.key == t)
			return item ? item.label : '';
		} else {
			return t;
		}
	}

	function toDetail() {
		go.to('PAGES_ORDER_DETAIL', {
			id: props.data.id
		})
	}

	function print(item) {
		emits('printOrder', props.data)
	}
</script>

<style lang="scss" scoped>
	.border-bottom {
		border-bottom: 1rpx solid #E5E5E5;
	}

	.u-font-40 {
		font-size: 40rpx;
	}

	.item {
		padding: 16rpx 24rpx 32rpx 24rpx;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		margin-bottom: 32rpx;
	}

	.unpaid {
		color: #FD7B49;
	}

	.print {
		padding: 6rpx 14rpx 8rpx 18rpx;
		border: 1px solid $my-main-color;
		color: $my-main-color;
		font-size: 24rpx;
		border-radius: 100rpx;
	}

	.line {
		height: 14px;
		width: 1px;
		background-color: #E5E5E5;
		margin: 0 12rpx;
	}

	.row {
		.top {
			display: flex;

			.name {
				width: 50%;
			}

			.num {
				width: 20%;
				display: flex;
				justify-content: flex-end;
			}

			.price {
				width: 30%;
				display: flex;
				justify-content: flex-end;
			}
		}
	}
</style>