<template>
	<view class="shop-order-detail" v-if="detail.id">
		<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" color="#fff"
			:backgroundColor="scrollTop < 20 ?``:primaryColor" :leftIcon="options.notice?'iconshouye':'icon-left'"
			title="订单详情">
		</uni-nav-bar>
		<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
		<image mode="aspectFill" lazy-load class="common-bg abs" src="https://lbqny.migugu.com/admin/farm/bg-cash.png">
		</image>
		<view class="pl-lg pr-lg c-base" style="height: 120rpx;">
			<view class="f-sm-title text-bold pt-md">{{statusType[detail.pay_type]}}</view>
			<view class="f-caption" v-if="detail.pay_type == 1">请在<min-countdown :targetTime="over_time_text"
					color="#fff" @callback="countEnd">
				</min-countdown>内完成支付，逾期未支付将自动取消</view>
		</view>
		<view class="order-text-info ml-md mr-md pd-lg fill-base f-paragraph c-title box-shadow radius-24"
			v-if="detail.send_type == 2">
			<view @tap.stop="toMap" class="flex-warp">
				<i class="iconfont icon-dingwei mr-sm" style="font-size: 28rpx;margin-top: 6rpx;"></i>
				<view class="flex-1">{{`${detail.address_info.address}${detail.address_info.address_info}`}}</view>
			</view>
			<view @tap.stop="$util.goUrl({url:detail.address_info.mobile,openType:'call'})"
				class="flex-y-baseline c-caption" style="margin:5rpx 0 0 38rpx;">{{detail.address_info.user_name}}
				<view class="ml-lg">
					{{detail.address_info.mobile}}
				</view>
			</view>
		</view>
		<view class="order-text-info mt-md ml-md mr-md pl-lg pr-lg fill-base f-paragraph c-title box-shadow radius-24">
			<view class="flex-between pt-lg pb-lg">
				<view class="text c-desc">{{detail.send_type == 1 ? '自提时间':'配送时间'}}</view>
				<view>{{detail.user_send_time}}</view>
			</view>
			<view class="pt-lg pb-lg b-1px-t" v-if="detail.text">
				<view class="text c-desc">备注</view>
				<view class="mt-sm">
					<text decode="emsp" style="word-break:break-all;">{{detail.text}}</text>
				</view>
			</view>
		</view>

		<view class="mt-md ml-md mr-md pd-lg fill-base f-paragraph c-title box-shadow radius-24">
			<view @tap="$util.goUrl({url:`/shop/pages/store?id=${detail.store_info.id}`})"
				class="flex-y-center pb-lg b-1px-b">
				<i class="iconfont icon-dianpu mr-sm"></i>
				<view class="f-title c-title text-bold ellipsis">{{detail.store_info.title}}</view>
				<i class="iconfont icon-right"></i>
			</view>
			<view class="order-item flex-center" :class="[{'mt-lg':index==0},{'mt-md':index!=0}]"
				v-for="(item,index) in detail.order_goods" :key="index">
				<image mode="aspectFill" lazy-load class="avatar box-shadow-mini radius-24" :src="item.goods_cover">
				</image>
				<view class="flex-1 ml-lg">
					<view class="f-paragraph c-title text-bold max-490 ellipsis"> {{item.goods_name}}
					</view>
					<view class="f-caption c-title" :class="[{'flex-center':item.refund_num*1>0}]">
						<view class="ellipsis" :style="{maxWidth:item.refund_num*1>0?'300rpx':'490rpx'}">
							{{item.spe_name}}
						</view>
						<view class="flex-1 c-warning text-right" v-if="item.refund_num*1>0">已退x{{item.refund_num}}
						</view>
					</view>
					<view class="flex-y-baseline mt-sm">
						<view class="flex-1 f-paragraph c-warning">¥{{item.goods_price}} </view>
						<view class="f-caption c-caption">x {{item.goods_num}} </view>
					</view>
				</view>
			</view>
			<view class="mt-lg pt-lg pb-lg f-paragraph c-title b-1px-tb">
				<view class="flex-between">
					<view>商品金额</view>
					<view>¥{{detail.init_goods_price}}</view>
				</view>
				<block v-if="detail.send_type == 2">
					<view class="flex-between mt-sm">
						<view>配送费</view>
						<view>¥{{detail.freight}}</view>
					</view>
				</block>
				<view class="flex-between mt-sm" v-if="detail.kill_discount_price*1>0">
					<view>秒杀优惠</view>
					<view class="c-warning">-¥{{detail.kill_discount_price}}</view>
				</view>
				<view class="flex-between mt-sm" v-if="detail.integral_discount_price*1>0">
					<view>积分抵扣</view>
					<view class="c-warning">-¥{{detail.integral_discount_price}}</view>
				</view>
				<view class="flex-between mt-sm" v-if="detail.discount*1>0">
					<view>卡券优惠</view>
					<view class="c-warning">-¥{{detail.discount}}</view>
				</view>
			</view>
			<view class="mt-lg f-paragraph c-title flex-between mt-sm">
				<view>合计</view>
				<view class="f-title c-warning text-bold">¥{{detail.pay_price}}</view>
			</view>
		</view>
		<video class="item-video mt-md ml-md mr-md radius-24" style="overflow: hidden;" :loop="false" enable-play-gesture enable-progress-gesture
			:src="detail.video" @waiting="onWaiting" @progress="onProgress" @loadedmetadata="onLoadedMetaData"
			v-if="detail.video">
		</video>
		<view class="order-text-info mt-md ml-md mr-md pd-lg fill-base f-caption c-title box-shadow radius-24">
			<view class="flex-warp pt-sm">
				<view class="text c-caption">订单编号</view>
				<view class="flex-1 flex-y-center">{{detail.order_code}}
					<view @tap="$util.goUrl( {url:`${detail.order_code}`,openType:'copy'})"
						class="copy-btn sm flex-center fill-body" :style="{color:primaryColor}">复制</view>
				</view>
			</view>
			<block v-if="detail.send_type == 2">
				<view class="flex-warp pt-md" v-if="detail.transaction_id">
					<view class="text c-caption">付款单号</view>
					<view class="flex-1 flex-y-center">{{detail.transaction_id}}
						<view @tap="$util.goUrl( {url:`${detail.transaction_id}`,openType:'copy'})"
							class="copy-btn sm flex-center fill-body" :style="{color:primaryColor}">复制</view>
					</view>
				</view>
				<view class="flex-warp pt-md" v-if="detail.pay_type == -1 && detail.refund_code">
					<view class="text c-caption">退款单号</view>
					<view class="flex-1 flex-y-center">{{detail.refund_code}}
						<view @tap="$util.goUrl( {url:`${detail.refund_code}`,openType:'copy'})"
							class="copy-btn sm flex-center fill-body" :style="{color:primaryColor}">复制</view>
					</view>
				</view>
			</block>
			<view class="flex-warp pt-md">
				<view class="text c-caption">下单时间</view>
				<view class="flex-1">{{detail.create_time}}</view>
			</view>
			<block v-if="detail.send_type == 2">
				<view class="flex-warp pt-md" v-if="detail.pay_time">
					<view class="text c-caption">支付时间</view>
					<view class="flex-1">{{detail.pay_time}}</view>
				</view>
				<view class="flex-warp pt-md">
					<view class="text c-caption">支付方式</view>
					<view class="flex-1">{{payType[detail.pay_model]}}</view>
				</view>
			</block>
		</view>

		<view class="order-text-info mt-md ml-md mr-md pd-lg fill-base f-caption c-title box-shadow radius-24"
			v-if="detail.pay_type == 7">
			<view class="flex-warp pt-sm" v-if="detail.hx_time">
				<view class="text c-caption">收货时间</view>
				<view class="flex-1">{{detail.hx_time}}</view>
			</view>
			<view class="flex-warp pt-md">
				<view class="text c-caption">收货人</view>
				<view class="flex-1">{{detail.hx_user_name}}</view>
			</view>
		</view>

		<view class="space-max-footer"></view>

		<view class="footer-btn fix fill-base pl-lg pr-lg"
			v-if="detail.pay_type==1 || (detail.pay_type == 2 && detail.can_refund) || detail.pay_type == 3 || (detail.pay_type == 7 && (detail.can_refund || !detail.have_eva))">

			<view class="flex-between">
				<view></view>
				<view class="flex-y-center" v-if="detail.pay_type == 1">
					<view @tap.stop="toChangeOrder('cancel_item')"
						class="common-btn disabled flex-center f-caption c-title radius">取消订单
					</view>
					<view @tap.stop="toPay" class="common-btn flex-center f-caption c-base radius ml-lg"
						:style="{background:primaryColor}">去支付
					</view>
				</view>
				<view class="flex-y-center" v-if="detail.pay_type == 2">
					<view @tap.stop="toRefund" class="common-btn disabled flex-center f-caption c-title radius"
						v-if="detail.can_refund">申请退款
					</view>
				</view>
				<view class="flex-y-center" v-if="detail.pay_type == 3">
					<view @tap.stop="toRefund" class="common-btn disabled flex-center f-caption c-title radius"
						v-if="detail.can_refund">申请退款
					</view>
					<view @tap.stop="toChangeOrder('end_item')"
						class="common-btn flex-center f-caption c-base radius ml-lg" :style="{background:primaryColor}">
						确认收货
					</view>
				</view>
				<view class="flex-y-center" v-if="detail.pay_type == 7 && (detail.can_refund || !detail.have_eva)">
					<view @tap.stop="toRefund(index)" class="common-btn disabled flex-center f-caption c-title radius"
						v-if="detail.can_refund">申请退款
					</view>
					<view @tap.stop="toEvaluate" class="common-btn flex-center f-caption c-base radius ml-lg"
						:style="{background:primaryColor}" v-if="!detail.have_eva">去评价
					</view>
				</view>

			</view>
			<view class="space-safe"></view>
		</view>

		<common-popup @confirm="confirmUpdate" ref="cancel_item" type="CANCEL_ORDER" :info="popupInfo"></common-popup>
		<common-popup @confirm="confirmUpdate" ref="end_item" type="END_ORDER" :info="popupInfo"></common-popup>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				scrollTop: 0,
				options: {},
				detail: {},
				payType: {
					1: '微信支付',
					2: '余额支付',
					3: '支付宝支付'
				},
				statusType: {
					'-1': '已取消',
					1: '待支付',
					2: '待发货',
					3: '待收货',
					7: '已完成',
				},
				sendType: {
					1: '自提',
					2: '快递'
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				popupInfo: {},
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			haveOperItem: state => state.order.haveOperItem,
			over_time_text() {
				return new Date().getTime() + this.detail.over_time * 1000
			}
		}),
		onLoad(options) {
			let {
				notice = 0
			} = options
			options.notice = notice
			this.options = options
			this.$util.showLoading()
			this.updateOrderItem({
				key: 'haveOperItem',
				val: false
			})
			this.initIndex()
		},
		onUnload() {
			if (!this.haveOperItem) return
			this.$util.back()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			...mapActions(['getConfigInfo']),
			...mapMutations(['updateOrderItem']),
			async initIndex() {
				let {
					id,
				} = this.options
				let data = await this.$api.shop.orderInfo({
					id
				})
				this.detail = data
				this.$util.hideAll()
			},
			initRefresh() {
				this.initIndex()
			},
			countEnd() {
				this.$util.log("倒计时完了")
				setTimeout(() => {
					this.initRefresh()
					this.$util.back()
				}, 1000)
			},
			onWaiting(e) {},
			onProgress(e) {},
			onLoadedMetaData(e) {},
			async toMap() {
				let {
					lat,
					lng,
					address
				} = this.detail
				await this.$util.checkAuth({
					type: 'userLocation'
				})
				await uni.getLocation({
					type: 'gcj02',
				})
				await uni.openLocation({
					latitude: lat * 1,
					longitude: lng * 1,
					name: address,
					scale: 28
				})
			},
			// 取消订单/确认收货
			async toChangeOrder(key) {
				let {
					id,
					order_code,
					order_goods,
				} = this.detail
				let {
					goods_cover: image,
				} = order_goods[0]
				this.popupInfo = {
					id,
					name: `订单编号：${order_code}`,
					image,
					refs_key: key,
				}
				this.$refs[key].open()
			},
			async confirmUpdate() {
				let {
					id,
					refs_key,
				} = this.popupInfo
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				let methodModel = {
					'cancel_item': {
						method: 'cancelOrder',
						pay_type: -1,
						msg: '取消成功'
					},
					'end_item': {
						method: 'endOrder',
						pay_type: 7,
						msg: '收货成功'
					},
				}
				try {
					let {
						method,
						pay_type,
						msg
					} = methodModel[refs_key]
					await this.$api.shop[method]({
						id
					})
					this.$util.hideAll()
					this.$util.showToast({
						title: msg
					})
					this.lockTap = false
					this.initRefresh()
					this.$refs[refs_key].close()
					this.updateOrderItem({
						key: 'haveOperItem',
						val: true
					})
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 去支付
			async toPay() {
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				let {
					id,
					pay_model
				} = this.detail

				try {
					let {
						pay_list
					} = await this.$api.shop.rePayOrder({
						id
					})
					this.$util.hideAll()
					if (pay_list) {
						if (pay_model == 3) {
							pay_list = {
								orderInfo: pay_list,
								provider: 'alipay'
							}
						}
						try {
							await this.$util.pay(pay_list)
							this.lockTap = false;
							this.detail.pay_type = 2
							this.updateOrderItem({
								key: 'haveOperItem',
								val: true
							})
						} catch (e) {
							this.lockTap = false;
							return;
						}
					}
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 申请退款
			toRefund() {
				let {
					id
				} = this.detail
				let url = `/shop/pages/order/refund?id=${id}`
				this.$util.goUrl({
					url
				})
			},
			// 去评价
			toEvaluate() {
				let {
					id
				} = this.detail
				let url = `/mine/pages/evaluate/edit?id=${id}&type=shop`
				this.$util.goUrl({
					url
				})
			},
		}
	}
</script>


<style lang="scss">
	.shop-order-detail {
		.order-seed-item {
			.seed-img {
				width: 80rpx;
				height: 80rpx;
				box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
			}
		}

		.item-video {
			width: 710rpx;
			height: 380rpx;
			display: block;
		}

		.footer-btn {
			width: 100%;
			bottom: 0;

			.flex-between {
				height: 98rpx;
			}
		}
	}
</style>
