<template>
	<view class="flex-col" :style="{height: winHeight + 'px'}">
		<!-- #ifdef MP -->
		<view class="cart_nav">
			<nav-bar iconColor='#fff' ref="navBarRef" navTitle="代取快递" backgroundColor="#FF6702" :isBackgroundColor="false">
			</nav-bar>
		</view>
		<!-- #endif -->
		<!-- <view class='newsList tui-skeleton flex-col' style="background-color: #fff;flex: 1;"> -->
		<!-- <view class='nav'>
				<scroll-view class="scroll-view_x" scroll-x scroll-with-animation :scroll-left="scrollLeft" style="width:auto;overflow:hidden;">
					<block v-for="(item,index) in navList" :key="index">
						<view class='item borRadius14 tui-skeleton-rect' :class='active==item.id?"on":""' @click='tabSelect(item.id, index)'>
							<view class="line1">{{item.name}}</view>
							<view class='line bg_color' v-if="active==item.id"></view>
						</view>
					</block>
				</scroll-view>
			</view> -->
		<!-- banner轮播图 -->
		<view class="mb-10" v-if="bannerList.length">
			<swiper :indicator-dots="true" :autoplay="true" :interval="2500" :duration="500" circular="true">
				<block v-for="(item,index) in bannerList" :key='item.id'>
					<swiper-item>
						<view class="swiper-item">
							<image class="banner-img" :src="item.imageUrl" mode="widthFix" />
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>
		<navigator url="/pages/address/user_address_list/index">
			<view class="fast-top flex-between-center">
				<view>
					<view>送至：{{addressItem.province || ''}}{{addressItem.city || ''}}{{addressItem.district || ''}}{{addressItem.street || ''}}{{addressItem.detail || ''}}</view>
					<view style="margin-top: 10rpx;">
						<text>{{addressItem.realName || ''}}</text> 收 {{addressItem.phone || ''}}
					</view>
				</view>
				<view>
					<text class="iconfont icon-jiantou"></text>
				</view>
			</view>
		</navigator>
		<!-- <navigator url="/pages/substitute_fast_mail/take_fast_mail/index"> -->
		<view class="flex-center fast-top" @tap="onAddFastMail">
			<view class="fast-jia">
				＋
			</view>
			<view>添加代取快递信息</view>
		</view>
		<!-- </navigator> -->
		<scroll-view scroll-y scroll-with-animation style="flex: 1;overflow: hidden;">
			<view class="fast-box" :class="index !== fastMailData.length - 1 ? 'qie-ge' : ''" v-for="(item, index) in fastMailData"
			 :key="index">
				<view class="flex-between-center mb-20">
					<view>
						<view class="fast-yz">{{item.postName}}</view>
						<view class="fast-xx">{{item.addressDetail}}</view>
					</view>
					<view class="flex-y-center"  @tap="makePhone(item.phone)">
						<image src="/static/img/phone.png" class="image2"/><text>联系驿站</text>
					</view>
				</view>
				<view class="fast-item" v-for="(value, key) in item.orderInfoList" :key="key">
					<view class="flex-between-center mb-20 ml-14" @click="onValRadio(value)">
						<radio class="radios" :checked="value.radio" color="#FF6702" />
						<view class="flex-y-center">
							<image style="width: 135rpx; height: 135rpx;border-radius: 30rpx;margin-right: 23rpx;" src="/static/img/ic-qu-chengse.png"></image>
							<view>
								<view class="fast-yz" style="font-weight: bold;">取件码 <text>{{value.pickUpCode}}</text></view>
								<view class="fast-xx">{{value.expressCompany}} {{value.expressNo}}</view>
								<view class="fast-yz">规格：{{value.spec}}</view>
							</view>
						</view>
						<view>
							<!-- <view class="fast-yz">134m</view> -->
							<view class="bg-color-cash">￥<text class="fast-cash">{{value.price}}</text></view>
						</view>
					</view>
					<view class="fast-remark">
						<view v-if="value.remark">备注：{{value.remark}}</view>
						<view class="flex-y-center" v-if="value.image" @click="lookImg(value.image)">
							<text>附图：</text>
							<image style="width: 38rpx; height: 38rpx;" src="/static/img/ic-figure.png"></image>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- <view class='list' style="flex: 1;">
				<template v-if="active == 1">
				</template>
				<template v-if="active == 2">
					<send-fast-mail></send-fast-mail>
				</template>
			</view> -->
		<view class="fast-tip flex-y-center">
			<!-- <radio :checked="radioArr" color="#FF6702" @tap="onRadioChang()" />全选 -->
			<view @tap="onRadioDel()"><text class="iconfont icon-shanchu mr-14"></text>删除</view>
		</view>
		<!--view class="flex-between-center fast-tip" @tap="isTip = true">
			<view class="remark-name">小费</view>
			<view class="flex-y-center">
				<text style="font-weight: 400;font-size: 27rpx;color: #646464;">{{giveTips}}</text>
				<view class='iconfont icon-jiantou' style="font-size: 30rpx;margin: 0 19rpx;"></view>
			</view>
		</view-->
		<!-- 购物车栏 begin -->
		<view class="cart-box">
			<view class="mark-box">
				<view class="mark">应付 ￥ <text style="font-size: 31rpx;">{{orderPrice}}</text></view>
				<view class="tips">普通用户 | 查看明细 》</view>
			</view>
			<button type="primary" class="pay-btn" @tap="toPay">
				提交订单
			</button>
		</view>
		<!-- </view> -->
		<fast-mail-order v-if="isOrder" @onCancel="onCanCel"></fast-mail-order>
		<take-fast-mail v-if="isTake" :orderNo="preOrderNo" :addressObj="addressItem" @onTakeFastOk="onTakeChang" @onCancel="onCanCel"></take-fast-mail>
		<fast-mail-tip v-if="isTip" :value="giveTips" @tipValue="onTipValue" @onCancel="onCanCel"></fast-mail-tip>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	import navBar from '@/components/navBar';
	import animationType from '@/utils/animationType.js'
	import tuiSkeleton from '@/components/base/tui-skeleton.vue';
	//import sendFastMail from '../components/send_fast_mail/index.vue'
	import takeFastMail from '../take_fast_mail/index.vue'
	import fastMailOrder from '../components/fast_mail_order/index.vue'
	import fastMailTip from '../components/fast_mail_tip/index.vue'
	import {
		preExpressApi,
		loadPreExpressApi,
		loadPreExpressDelApi,
		expressCreate,
		getExpressInfo
	} from '@/api/fastMail.js';
	import {
		getAddressDefault,
		getAddressExpressDefault
	} from '@/api/user.js';
	import orderPay from "@/mixins/OrderPay.js";
	import {
		Debounce
	} from '@/utils/validate.js'
	let app = getApp();
	export default {
		mixins: [orderPay],
		components: {
			tuiSkeleton,
			navBar,
			//sendFastMail,
			fastMailOrder,
			takeFastMail,
			fastMailTip
		},
		data() {
			return {
				isOrder: false, // 提交快递订单弹窗
				isTake: false, // 新增快递弹窗
				isTip: false, // 小费弹窗
				giveTips: 0,
				winHeight: 0,
				isNodes: 0, //控制什么时候开始抓取元素节点,只要数值改变就重新抓取
				autoplay: true,
				radioArr: false,
				radioDel: false,
				preOrderNo: '',
				navList: [{
					id: 1,
					name: '取快递',
					preOrderType: 'take'
				}, {
					id: 2,
					name: '寄快递',
					preOrderType: 'send'
				}],
				fastMailData: [],
				navListIndex: 0,
				active: 0,
				scrollLeft: 0,
				addressItem: {},
				orderPrice: 0,
				bannerList: []
			};
		},
		onLoad() {
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					that.winHeight = res.windowHeight
				},
			});
			getAddressDefault().then(res => {
				console.log('地址', res)
				this.addressItem = res.data || {}
			})
			getExpressInfo().then(res => {
				this.bannerList = res.data.banner
			})
			// this.getPreExpressApi()
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let that = this
			uni.$on('addressItem', function(res) {
				that.$set(that, 'addressItem', res);
				that.preOrderNo = ''
				that.fastMailData = []
				that.orderPrice = 0
			})
		},
		methods: {
			lookImg(url) {
				uni.previewImage({
					current: url, //  传 Number H5端出现不兼容
					urls: [url]
				});
			},
			getPreExpressApi(orderNo) {
				loadPreExpressApi(orderNo).then(res => {
					this.fastMailData = res.data.postOrderList.map(item => {
						item.orderInfoList.forEach(val => {
							val.radio = false
						})
						return { ...item
						}
					}) || []
					console.log(this.fastMailData,'fastMailData')
					this.orderPrice = +res.data.payFee
				})
			},
			onAddFastMail() {
				if (Object.keys(this.addressItem).length == 0) return this.$util.Tips({
					title: '请选择地址'
				});
				this.isTake = true
			},
			tabSelect(active, e) {
				this.active = active;
				this.navListIndex = e;
				this.scrollLeft = e * 60;
			},
			toPay: Debounce(function(item) {
				uni.showLoading({
					title: '加载中...'
				});
				expressCreate({
					preOrderNo: this.preOrderNo,
					giveTips: this.giveTips
				}).then(res => {
					uni.hideLoading()
					console.log(res)
					let data = {
						payPrice: +res.data.payPrice,
						orderNo: res.data.orderNo
					}
					this.getToPayment('', data, 'fast')
				}).catch(err => {
					uni.hideLoading()
					console.log('提交快递')
				})
			}),
			onTipValue(value) {
				this.giveTips = +value
				this.isTip = false;
				console.log(value)
			},
			onTakeChang(data) {
				this.isTake = false
				this.preOrderNo = data.orderNo
				this.getPreExpressApi(data.orderNo)
			},
			onCanCel(val) {
				this.isOrder = val
				this.isTake = val
				this.isTip = val
			},
			onValRadio(val) {
				val.radio = !val.radio
			},
			onRadioChang() {
				// let price = 0;
				this.radioArr = !this.radioArr
				if (this.fastMailData.length === 0) return
				this.fastMailData = this.fastMailData.map((item, index) => {
					item.orderInfoList.forEach(val => {
						val.radio = this.radioArr
						// val.radio ? price += +val.price : ''
					})
					return item
				})
				// this.orderPrice = price
			},
			/**
			 * 拨打电话
			 */
			makePhone: function(e) {
				uni.makePhoneCall({
					phoneNumber: e
				})
			},
			onRadioDel() {
				if (this.fastMailData.length === 0) return
				const arr = []
				this.fastMailData.map((item, index) => {
					item.orderInfoList.forEach(val => {
						val.radio ? arr.push(val.expressId) : ''
					})
					return item
				})
				console.log(arr)
				let that = this
				uni.showModal({
					content: '确定删除？',
					cancelText: "取消",
					confirmText: "确定",
					showCancel: true,
					confirmColor: '#f55850',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在删除中'
							});
							loadPreExpressDelApi(that.preOrderNo, arr).then(res => {
								if (res.data) {
									uni.hideLoading();
									that.getPreExpressApi(that.preOrderNo)
								}
							}).catch(err => {
								console.log(err)
								uni.hideLoading();
							})
							console.log(that.fastMailData)
						}
					},
				})
			}
		}
	}
</script>

<style lang="scss">
	.courie {
		padding: 14rpx 24rpx 0 24rpx;
		font-weight: 500;
		font-size: 23rpx;
		color: $bg-color-primary;
		background-color: #fff;

		image {
			width: 46rpx;
			height: 46rpx;
			margin-right: 15rpx;
		}
	}

	.noCheck {
		width: 32rpx;
		height: 32rpx;
		border-radius: 50%;
		overflow: hidden;
		border: 1px solid #999999;
		background-color: #eee;
	}

	.fast-top {
		width: 100%;
		padding: 28rpx 30rpx;
		font-weight: 600;
		font-size: 27rpx;
		color: #141414;
		background-color: #fff;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
	}

	.remark-name {
		font-weight: 600;
		font-size: 27rpx;
		color: #141414;
	}

	.fast-tip {
		background-color: #fff;
		margin-top: 20rpx;
		padding: 20rpx 30rpx;
	}

	.image2 {
		width: 42rpx;
		height: 42rpx;
		margin-right: 20rpx;
	}

	.fast-jia {
		width: 44rpx;
		height: 44rpx;
		line-height: 44rpx;
		text-align: center;
		color: #fff;
		font-size: 40rpx;
		border-radius: 50%;
		background-color: $bg-color-primary;
		margin-right: 20rpx;
	}

	.qie-ge {
		border-bottom: 2rpx solid #D6D7DC;
	}

	.fast-box {
		border-radius: 23rpx;
		padding: 30rpx;
		background-color: #fff;

		.fast-item {
			position: relative;
			padding: 20rpx 20rpx 20rpx 86rpx;
			background-color: #F8F9FB;
			margin-bottom: 10rpx;
		}

		.radios {
			position: absolute;
			left: 38rpx;
		}

		.fast-remark {
			font-weight: 400;
			font-size: 21rpx;
			color: #999999;

			view {
				margin-bottom: 10rpx;
			}
		}

		.fast-yz {
			font-weight: 500;
			font-size: 21rpx;
			color: #141414;

			text {
				color: #FF0202;
				margin-left: 20rpx;
			}
		}

		.fast-xx {
			font-weight: 400;
			font-size: 21rpx;
			color: #646464;
			margin: 10rpx 0;
		}

		.bg-color-cash {
			margin-top: 30rpx;
			color: $bg-color-primary;

			.fast-cash {
				font-weight: 600;
				font-size: 27rpx;
				color: #FF6702;
			}
		}
	}

	.newsList .nav {
		padding: 0 24rpx;
		width: 100%;
		white-space: nowrap;
		box-sizing: border-box;
		padding-top: 43rpx;
		border-top-right-radius: 23rpx;
	}

	.newsList .nav .item {
		// max-width: 156rpx;
		display: inline-block;
		font-size: 32rpx;
		color: #999;
	}

	.newsList .nav .item.on {
		color: #282828;
	}

	.newsList .nav .item~.item {
		margin-left: 46rpx;
	}

	.newsList .nav .item .line {
		// width: 24rpx;
		height: 10rpx;
		border-radius: 6rpx;
		margin: 10rpx auto 0 auto;
		background-color: $bg-color-primary;
	}

	.newsList .nav .item .line1 {
		color: $bg-color-primary;
	}

	.cart-box {
		height: 96rpx;
		border-radius: 48rpx;
		box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.2);
		background-color: #000;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 9999;

		.pay-btn {
			height: 100%;
			padding: 0 30rpx;
			color: #FFFFFF;
			border-radius: 0 50rpx 50rpx 0;
			display: flex;
			align-items: center;
			font-size: $font-size-base;
			background-color: $bg-color-primary;
		}

		.mark-box {
			padding-left: 46rpx;
			margin-right: 30rpx;
			position: relative;

			.mark {
				font-weight: 500;
				font-size: 23rpx;
				color: #FFFFFF;
			}

			.tips {
				font-weight: 500;
				font-size: 27rpx;
				color: #999999;
			}
		}
	}

	.banner-img {
		width: 100%;
		height: 100%;
	}
</style>
