<template>
	<view class="order-detail" :class="{ hasBottom: hasControlNav }">
		<tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
		
		<view class="detail-top">
			<!-- 订单状态 -->
			<view class="order-status">{{ stateExpFormat(orderInfo.status) }}</view>
			<!-- 收货地址 -->
			<cm-choose-address :addressData="userInfo" disabled></cm-choose-address>
		</view>

		<!-- 订单商品列表 -->
		<template v-for="(shopOrder, shopOrderIndex) in shopOrderList">
			<view class="grid" :key="shopOrderIndex"></view>
			<view class="shop-section" :key="shopOrderIndex">
				<!-- 供应商 -->
				<view class="origin">
					<image class="cover" :src="shopOrder.shopLogo"></image>
					<view class="name">{{ shopOrder.shopName }}</view>
				</view>
				<!-- 商品列表 -->
				<view class="goods-list">
					<!-- 商品信息 -->
					<view class="order-goods" v-for="goods in shopOrder.orderProductList" :key="goods.productId">
						<cm-order-prodcut :goods-data="goods"></cm-order-prodcut>
					</view>
				</view>
				<view class="line"></view>
				<!-- 统计 -->
				<view class="total">
					<view class="count">共{{ shopOrder.itemCount }}件商品</view>
					<view class="status">
						<text class="label">商品总额：</text>
						<text>￥{{ shopOrder.totalAmount | formatPrice }}</text>
					</view>
				</view>
			</view>
		</template>

		<view class="grid"></view>
		<!-- 订单详细信息 -->
		<cm-order-information :orderInfo="orderInfo"></cm-order-information>

		<!-- 操作 -->
		<view class="order-control" v-if="hasControlNav">
			<cm-order-control-nav
				:orderInfo="orderInfo"
				@confirm="handleConfirmClick"
				@change="getCount"
			></cm-order-control-nav>
		</view>

		<!-- 操作弹窗 -->
		<tui-modal
			:show="modal"
			:content="modalText"
			:size="32"
			:maskClosable="false"
			color="#333"
			shape="circle"
			@click="handleModalConfirm"
		></tui-modal>

		<!-- 加载框 -->
		<cm-loading :visible="isSubLoading" :text="loadingText"></cm-loading>

		<!-- 失效商品列表 -->
		<cm-order-invalid-modal
			:goodsList="invalidList"
			:visible="buyAgainModal"
			@confirm="buyAgainModalClick"
			@cancel="buyAgainModalHide"
		></cm-order-invalid-modal>

		<!-- 底部占位 -->
		<view class="reserved" v-if="isIphoneX"></view>
	</view>
</template>

<script>
import CmChooseAddress from '@/components/cm-module/cm-choose-address/cm-choose-address.vue' //地址信息
import CmOrderProdcut from '@/components/cm-module/cm-order-prodcut/cm-order-prodcut.vue'
import CmOrderInformation from '@/components/cm-module/cm-order-information/cm-order-information.vue'
import CmOrderControlNav from '@/components/cm-module/cm-order-control-nav/cm-order-control-nav.vue'
import CmOrderInvalidModal from '@/components/cm-module/cm-order-invalid-modal/cm-order-invalid-modal.vue'
import CmLoading from '@/components/cm-module/cm-loading/cm-loading.vue'
import orderList from './mixins/orderList.js'
import wechatPay from './mixins/wechatPay.js'

import { mapGetters } from 'vuex'
export default {
	mixins: [wechatPay, orderList],
	components: {
		CmOrderProdcut,
		CmOrderInformation,
		CmOrderControlNav,
		CmOrderInvalidModal,
		CmLoading,
		CmChooseAddress
	},
	data() {
		return {
			isRequest: false,
			orderId: '',
			discernReceiptList: [],
			shopOrderList: [],
			orderInfo: {},
			userInfo: {},
			buttonCount: 0
		}
	},
	computed: {
		...mapGetters(['userId', 'userIdentity', 'isIphoneX']),
		// 当前用户是否为协销
		isDealer() {
			return this.userIdentity === 2
		},
		hasControlNav() {
			return this.orderInfo.buyUserId === this.userId && this.buttonCount > 0
		}
	},
	onLoad(option) {
		this.orderId = option.orderId
		this.getOrderDetail()
	},
	methods: {
		getOrderDetail() {
			this.OrderService.QueryOrderDetails({ orderId: this.orderId })
				.then(res => {
					this.discernReceiptList = res.data.discernReceiptList
					this.shopOrderList = res.data.shopOrderList
					this.orderInfo = res.data.order
					this.userInfo = res.data.userInfo
				})
				.catch(err => {
					this.$util.modal('提示', '订单查询失败，请稍后重试~', '确定', '', false, () => {
					    this.$api.switchTabTo('/pages/tabBar/index/index')
					})
				})
				.finally(() => {
					this.isRequest = false
				})
		},
		getCount(e) {
			this.buttonCount = e.count
		}
	}
}
</script>

<style lang="scss" scoped>
.order-detail {
	min-height: 100vh;
	background: #f7f7f7;

	padding-top: 138rpx + 60rpx;
	box-sizing: border-box;

	&.has-bottom {
		padding-bottom: 100rpx;
	}
	
	.order-status{
		height: 60rpx;
		font-size: 26rpx;
		line-height: 26rpx;
		padding: 34rpx 24rpx 0;
		box-sizing: border-box;
		color: #FF457B;
		background: #fff;
	}
	
	.detail-top {
		height: 134rpx + 60rpx;
		position: fixed;
		top: 0;
		z-index: 999;
	}
	
	.grid {
		height: 20rpx;
		background: #f7f7f7;
	}

	.shop-section {
		background: #fff;
		overflow: hidden;

		.line {
			width: 702rpx;
			height: 1px;
			background: #f7f7f7;
			margin: 0 auto;
		}

		.origin {
			padding: 0 24rpx;
			margin-top: 24rpx;
			margin-bottom: 16rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.cover {
				width: 56rpx;
				height: 56rpx;
				border-radius: 4rpx;
				border: 1px solid #f7f7f7;
				box-sizing: border-box;
			}

			.name {
				margin-left: 16rpx;
				font-size: 30rpx;
				color: #333333;
				font-weight: bold;
			}
		}

		.goods-list {
			margin-bottom: 32rpx;
			.order-goods {
				padding-top: 32rpx;
				&:first-child {
					padding-top: 0;
				}
			}
		}
	}

	.total {
		display: flex;
		justify-content: space-between;
		align-items: center;

		padding: 0 24rpx;
		margin: 32rpx 0;

		font-size: 26rpx;
		color: #333333;

		.count {
			font-weight: bold;
		}

		.status {
			color: #ff457b;

			.label {
				color: #333333;
			}
		}
	}

	.order-control {
		position: fixed;
		width: 100%;
		box-sizing: border-box;
		height: 100rpx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		bottom: 0;
		background: #fff;
	}
}
</style>
