<template name="freight">
	<!-- 运费信息 -->
	<view>
		<view class="goods-pros-m">
			<template v-if="supplier.postageFlag === 0">
				<view class="freight-main">
					<view class="freight-left">运费:</view>
					<view class="freight-right"><view class="freight-text">包邮</view></view>
				</view>
			</template>
			<template v-if="supplier.postageFlag === 1 && supplier.designatedFlag === 1">
				<view class="freight-main">
					<view class="freight-left">运费:</view>
					<view class="freight-right">
						<view class="freight-text">¥{{ handleTotalPostage | NumFormat }}</view>
						<view class="select" v-if="supplier.coldChain > 0" @click="handlePupShow">
							<text class="iconfont icon-xiayibu"></text>
						</view>
					</view>
				</view>
			</template>
			<template
				v-if="
					(supplier.postageFlag === 1 && supplier.designatedFlag === 2) ||
						(supplier.postageFlag === 1 && supplier.designatedFlag === 3)
				"
			>
				<view class="freight-main"> 
					<view class="freight-left">运费:</view>
					<view class="freight-right">
						<template v-if="handlePostageFlag === 1">
							<view class="freight-text">¥{{ handleTotalPostage | NumFormat }}</view>
						</template>
						<template v-if="handlePostageFlag === 2">
							<view v-if="handleSupplier.isColdChina" class="freight-text"
								>¥{{ handleTotalPostage | NumFormat }}</view
							>
							<view v-else class="freight-text">到付</view>
						</template>
						<view class="select" v-if="supplier.coldChain > 0" @click="handlePupShow">
							<text class="iconfont icon-xiayibu"></text>
						</view>
					</view>
				</view>
			</template>
			<template v-if="supplier.postageFlag === 2">
				<view class="freight-main">
					<view class="freight-left">运费:</view>
					<view class="freight-right"> <view class="freight-text">到付</view></view>
				</view>
			</template>
			<tui-bottom-popup :radius="true" :show="popupShow" @close="hidePopup">
				<view class="freight-title">运费设置</view>
				<view class="freight-content">
					<!-- <radio-group @change="radioChange" v-if="handleDesignatedFlag != 1">
						<label v-for="(item, index) in radioItems" :key="index">
							<radio
								style="transform:scale(0.7)"
								color="#FF5B00"
								:id="item.value"
								:value="item.value"
								:checked="item.checked"
							/>
							<label :for="item.value" class="label">
								<text>{{ item.name }}</text>
							</label>
						</label>
					</radio-group> -->
					<template v-if="handlePostageFlag === 1">
						<view class="freight-form">
							<view class="freight-form-item" v-if="handleSupplier.coldChain > 0">
								<view class="label">冷链运输费</view>
								<view class="texts">
									<text>¥{{ handleSupplier.coldChain | NumFormat }}</text>
									<text
										class="iconfont"
										:class="[isColdChina ? 'icon-yixuanze' : 'icon-weixuanze']"
										@click.stop="handleChangeColdChina"
									>
									</text>
								</view>
							</view>
							<view class="freight-form-item" v-if="handleDesignatedFlag != 1">
								<view class="label">其他运费 </view>
								<view class="texts red">¥{{ handleSupplier.postage | NumFormat }} </view>
							</view>
							<view class="freight-form-item">
								<view class="label">总运费 </view>
								<view class="texts red">¥{{ handleTotalPostage | NumFormat }} </view>
							</view>
						</view>
					</template>
					<template v-if="handlePostageFlag === 2">
						<view class="freight-form">
							<view class="freight-form-item" v-if="handleSupplier.coldChain > 0">
								<view class="label">冷链运输费</view>
								<view class="texts">
									<text>¥{{ handleSupplier.coldChain | NumFormat }}</text>
									<text
										class="iconfont"
										:class="[isColdChina ? 'icon-yixuanze' : 'icon-weixuanze']"
										@click.stop="handleChangeColdChina"
									>
									</text>
								</view>
							</view>
							<view class="freight-form-item">
								<view class="label">其他运费 </view><view class="texts red">到付</view>
							</view>
							<view class="freight-form-item">
								<view class="label">总运费</view>
								<view class="texts red" v-if="handleSupplier.isColdChina"
									>¥{{ handleSupplier.coldChain | NumFormat }}</view
								>
								<view class="texts red" v-else>到付</view>
							</view>
						</view>
					</template>
					<view class="freight-btn" @click="popupShow = false"> 确定 </view>
				</view>
			</tui-bottom-popup>
		</view>
	</view>
</template>

<script>
export default {
	name: 'freight',
	props: {
		supplier: {
			type: Object
		},
		index: {
			type: Number
		}
	},
	data() {
		return {
			popupShow: false,
			isColdChina: false,
			handlePostage: 0,
			handlePostageFlag: 1,
			handleDesignatedFlag: 0,
			radioItems: [
				{
					name: '不包邮',
					value: 1,
					checked: true
				}
				// 	{
				// 		name: '到付',
				// 		value: 2
				// 	}
			],
			handleSupplier: {}
		}
	},
	filters: {
		NumFormat(value) {
			//处理金额
			return Number(value).toFixed(2)
		}
	},
	created() {
		this.infoSupplier(this.supplier)
	},
	computed: {
		// 计算总运费
		handleTotalPostage() {
			if (this.handleSupplier.isColdChina) {
				return (this.handleSupplier.postage * 100 + this.handleSupplier.coldChain * 100) / 100
			} else {
				return this.handleSupplier.postage
			}
		}
	},
	watch: {
		supplier: {
			handler: function(el) {
				//监听对象的变换使用 function，箭头函数容易出现this指向不正确
				this.supplier = el
			},
			deep: true
		}
	},
	methods: {
		// 初始化
		infoSupplier(data) {
			this.handleSupplier = data
			this.isColdChina = data.isColdChina
			this.handlePostage = data.postage
			this.handlePostageFlag = data.handlePostageFlag
			this.handleDesignatedFlag = data.designatedFlag
		},
		// 勾选冷链费
		handleChangeColdChina() {
			this.isColdChina = !this.isColdChina
			this.handleSupplier.isColdChina = this.isColdChina
			if (this.isColdChina) {
				this.handleSupplier.totalPrice = Number(
					(this.handleSupplier.totalPrice * 100 + this.handleSupplier.coldChain * 100) / 100
				)
				this.$emit('changeColdChina', this.handleSupplier, this.index)
			} else {
				this.handleSupplier.totalPrice = Number(
					this.repiceNumSub(this.handleSupplier.totalPrice, this.handleSupplier.coldChain)
				)
				this.$emit('changeColdChina', this.handleSupplier, this.index)
			}
		},
		// 切换运费类型 1 不包邮 2 到付
		radioChange($event) {
			// const postage = this.isColdChina ? ( this.handleSupplier.postage + this.handleSupplier.coldChain ) : this.handleSupplier.postage
			this.handlePostageFlag = this.handleSupplier.handlePostageFlag = $event.detail.value * 1
			console.log('handlePostageFlag', this.handlePostageFlag)
			if (this.handlePostageFlag === 1) {
				this.handleSupplier.postage = this.handlePostage
				this.handleSupplier.totalPrice = Number(
					(this.handleSupplier.totalPrice * 100 + this.handleSupplier.postage * 100) / 100
				)
				this.$emit('changePostage', this.handleSupplier, this.index)
			} else {
				this.handleSupplier.totalPrice = Number(
					this.repiceNumSub(this.handleSupplier.totalPrice, this.handleSupplier.postage)
				)
				this.handleSupplier.postage = 0
				this.$emit('changePostage', this.handleSupplier, this.index)
			}
		},
		repiceNumSub(arg1, arg2) {
			//减法精确，arg2:被减数，arg1:减数
			var r1, r2, m, n
			try {
				r1 = arg1.toString().split('.')[1].length
			} catch (e) {
				r1 = 0
			}
			try {
				r2 = arg2.toString().split('.')[1].length
			} catch (e) {
				r2 = 0
			}
			m = Math.pow(10, Math.max(r1, r2))
			// last modify by deeka
			// 动态控制精度长度
			n = r1 >= r2 ? r1 : r2
			return ((arg1 * m - arg2 * m) / m).toFixed(n)
		},
		handlePupShow() {
			this.popupShow = true
		},
		hidePopup() {
			this.popupShow = false
		}
	}
}
</script>

<style lang="scss">
.goods-pros-m {
	width: 100%;
	box-sizing: border-box;
	padding: 10rpx 0;
	.freight-main {
		width: 100%;
		height: 78rpx;
		line-height: 78rpx;
		font-size: $font-size-28;
		color: $text-color;
		background: #ffffff;
		float: left;
		.freight-left {
			float: left;
			font-weight: bold;
		}
		.freight-right {
			float: right;
			color: #333333;
			position: relative;
			.freight-text {
				float: left;
			}
			.select {
				float: right;
				height: 78rpx;
				padding: 0 20rpx;
				line-height: 78rpx;
				color: #333333;
				font-weight: normal;
				position: relative;
				.select-text {
					display: inline-block;
					float: left;
					margin-right: 30rpx;
				}
				.iconfont {
					width: 60rpx;
					height: 78rpx;
					line-height: 78rpx;
					text-align: right;
					position: absolute;
					right: 0;
					top: 0;
					font-size: $font-size-30;
					color: #b2b2b2;
				}
			}
		}
	}
	.freight-bean {
		width: 100%;
		height: 58rpx;
		line-height: 58rpx;
		float: left;
		.bean-le {
			float: left;
			color: #666666;
			font-size: $font-size-26;
			font-weight: normal;
		}
		.bean-ri {
			float: right;
			display: flex;
			align-items: center;
			.checkbox-box {
				display: flex;
				width: 60rpx;
				float: left;
				height: 100%;
				font-size: $font-size-26;
				margin-top: 8rpx;
				.checkbox {
					width: 40rpx;
					text-align: right;
					box-sizing: border-box;
					text-align: center;
					text-decoration: none;
					border-radius: 0;
					-webkit-tap-highlight-color: transparent;
					overflow: hidden;
					color: $color-system;
				}
			}
		}
	}
}
.freight-title {
	color: #333333;
	font-size: 32rpx;
	text-align: center;
	margin: 40rpx 0;
}
.freight-content {
	box-sizing: border-box;
	padding: 0 24rpx;
	font-weight: normal;
	radio {
		margin-right: 8rpx;
	}
	.label {
		color: #666666;
		font-size: 28rpx;
		margin-right: 102rpx;
	}
	.freight-btn {
		height: 90rpx;
		background: $btn-confirm;
		border-radius: 45rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #ffffff;
		width: 100%;
		margin: 100rpx 0 0 0;
	}
}
.freight-form {
	margin-top: 50rpx;
	width: 100%;
	.freight-form-item {
		width: 100%;
		height: 48rpx;
		line-height: 48rpx;
		margin-bottom: 24rpx;
		color: #666666;
		font-size: 28rpx;
		.label {
			width: 50%;
			float: left;
			margin-right: 0;
		}
		.texts {
			width: 50%;
			float: right;
			text-align: right;
			&.red {
				color: #f94b4b;
			}
			.iconfont {
				margin-left: 20rpx;
				color: $color-system;
			}
		}
	}
}
.freight-pay {
	position: absolute;
	right: -60%;
	transform: scale(0.7);
}
</style>
