<template>
	<view class="whole">
		
		<view v-if="routeType == 1">
			<web-view :src="mapUrl"></web-view>
		</view>
		
		<view class="express" v-if="routeType == 2">
			<view class="express-code">
				<view class="left">
					<image v-if="order.items && order.items[0] && order.items[0].pic_url" :src="order.items && order.items[0] && order.items[0].pic_url" mode="aspectFill"></image>
					<text class="text">{{ order.company && order.company.name }}&nbsp;&nbsp;</text>
					<text>{{ order && order.ship_sn }}</text>
				</view>
				<view class="btn" @click="copyBtn(order.ship_sn)">复制</view>
			</view>
			<view class="express-order">
				<view class="icon"><span class="iconfont icon-yunshuzhong Color"></span></view>
				<view style="width: calc(100% - 26px);">
					<view>订单编号：{{ order.order && order.order.order_sn }}</view>
					<view class="address">收货地址：{{ order.order && order.order.address }}</view>
				</view>
			</view>
			<view class="express-steps">
				<view class="time-axis" :class="{ 'is-done': index === 0 }" v-for="(item, index) in order.logs"
					:key="index">
					<text v-for="(list, index) in item.context" :key="index" @click="clickMakePhoneCall"
						:data-phone="list.type=='phone'?list.val:''" :class="list.type == 'phone' ? 'phoneColor' : ''">
						{{ list.val }}
					</text>
					<view>{{ item.time }}</view>
				</view>
			</view>
		</view>
		
		<!-- 隐私协议弹窗 -->
		<privacy-popup :show-dialog="showPrivacy" @close="showPrivacy = false" @agree="showPrivacy = false" @refuse="showPrivacy = false" />
	</view>
	
</template>

<script>
	import { ref, reactive, toRefs, onMounted } from 'vue'
	import { get } from '@/Api/request.js'
	import { Style } from '@/utils/list.js'
	import { judgePrivacy } from '@/components/common.js'
	import privacyPopup from '@/components/privacyPopup/index.vue'
	
	export default {
		components: {
			privacyPopup
		},
		setup(props) {
			const data = reactive({
				mapUrl: '',
				shipId: '',
				order: {},
				Color: uni.getStorageSync('theme_color'),
				priceColor: Style[uni.getStorageSync('theme_index') * 1].priceColor,
				showPrivacy: false,
				routeType: 0
			})

			const clickMakePhoneCall = (e) => {
				uni.makePhoneCall({
					phoneNumber: e.currentTarget.dataset.phone,
					success: (res) => {
					},
					fail: (res) => {
					}
				})
			}

			const mobilePhoneArray = (strContent) => {
				if (strContent == '') {
					return
				}
				let list = []
				const mobileReg = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|0[\d]{2,3}[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g
				const phoneList = strContent.context.match(mobileReg)
				const textList = strContent.context.split(mobileReg)
				if (phoneList == null) {
					list.push({
						type: 'text',
						val: textList[0]
					})
					return list
				}

				textList.forEach((item) => {
					if (phoneList.indexOf(item) > -1) {
						list.push({
							type: 'phone',
							val: item
						})
					} else {
						list.push({
							type: 'text',
							val: item
						})
					}
				})
				return list
			}

			async function getShip() {
				await get(`/api/v1/order/ship/${data.shipId}`).then((res) => {
					if(res.data.status === 0 && res.data.logs.length === 0) {
						res.data.logs.push({
							context: '待揽件'
						})
					}
					res.data.logs.forEach((item) => {
						const list = mobilePhoneArray(item)
						item.context = list
					})
					data.order = res.data
					data.routeType = res.data.has_route_map ? 1 : 2
					data.mapUrl = res.data.route_map_url + '&nocopy=true&title=物流信息&height=40vh'
				})
			}

			// 复制
			async function copyBtn(val) {
				if(await judgePrivacy()) {
					data.showPrivacy = true
					return false
				}
				uni.setClipboardData({
					data: val
				})
			}

			return {
				...toRefs(data),
				getShip,
				copyBtn,
				clickMakePhoneCall
			}
		},
		async onLoad(options) {
			// await this.$onLaunched
			this.shipId = options.id
			this.getShip()
			
		}
	}
</script>

<style lang="scss" scoped>
	.express {
		min-height: 100%;
		background-color: #fff;
		padding: 0 30rpx;
		&-code {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 120rpx;
			border-bottom: 2rpx solid #e5e5e5;

			.left {
				display: flex;
				align-items: center;
				font-size: 28rpx;

				image {
					width: 69rpx;
					height: 69rpx;
					background: rgba(203, 202, 203, 0.39);
					opacity: 1;
					border-radius: 10rpx;
					margin-right: 15rpx;
				}

				.text {
					color: v-bind('Color');
				}
			}

			.btn {
				font-size: 22rpx;
				border: 1rpx solid #2c2c2c;
				border-radius: 20rpx;
				padding: 4rpx 20rpx;
			}
		}

		&-order {
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			font-size: 28rpx;
			padding: 30rpx 0;
			border-bottom: 2rpx solid #e5e5e5;
			.icon{
				transform: rotateY(180deg);
			}
			.address {
				margin-top: 15rpx;
			}
		}

		&-steps {
			padding-bottom: 20rpx;
		}
	}

	/* 时间轴 */
	.time-axis {
		color: #262626;
		position: relative;
		padding-left: 50rpx;
		list-style: none;
		font-size: 28rpx;
		margin: 10rpx 0;

		&:before {
			position: absolute;
			margin-top: 16rpx;
			left: 9rpx;
			content: ' ';
			height: 20rpx;
			width: 20rpx;
			border-radius: 50%;
			background-color: #e8e8e8;
		}

		&:after {
			position: absolute;
			top: 32rpx;
			left: 16rpx;
			bottom: -38rpx;
			content: ' ';
			width: 2rpx;
			border-right: 2rpx solid #e8e8e8;
		}

		&:not(:first-child) {
			padding-top: 16rpx;
		}

		&:last-child {
			&:after {
				display: none;
			}
		}

		&.is-done {
			&:after {
				border-color: v-bind('Color');
			}

			&:before {
				background-color: v-bind('Color');
				box-shadow: v-bind('Color') 0 0 10px;
			}
		}
	}

	.phoneColor {
		color: #61b8fd;
	}
	.Color{
		color: v-bind('Color');
		font-size: 20px;
	}
</style>
