<template name="freight">
	<!-- 运费信息 -->
	<view>
		<view class="goods-pros-m">
			<view class="freight-main">
				<view class="freight-left">运费:</view>
				<view class="freight-right" @click.stop="handlePupShow">
					<template v-if="handlePostageFlag === 0">
						<view class="freight-text" v-if="handleSupplier.isColdChina"
							>¥{{ handleSupplier.coldChain | NumFormat }}</view
						>
						<view class="freight-text" v-else>包邮</view>
					</template>
					<view class="freight-text" v-if="handlePostageFlag === 1"
						>¥{{ handleTotalPostage | NumFormat }}</view
					>
					<template v-if="handlePostageFlag === 2">
						<view class="freight-text" v-if="handleSupplier.isColdChina"
							>¥{{ handleSupplier.coldChain | NumFormat }}</view
						>
						<view class="freight-text" v-else>到付</view>
					</template>
					<view class="select"><text class="iconfont icon-xiangyou"></text></view>
				</view>
			</view>
			<tui-bottom-popup :radius="true" :show="popupShow" @close="handleComfirm">
				<view class="freight-title">运费设置</view>
				<view class="freight-content">
					<radio-group @change="radioChange">
						<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"
							></radio>
							<label :for="item.value" class="label">
								<text>{{ item.name }}</text>
							</label>
						</label>
					</radio-group>
					<template v-if="handlePostageFlag === 0">
						<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>
					<template v-if="handlePostageFlag === 1">
						<view class="freight-form">
							<view class="freight-form-item">
								<input
									class="form-input"
									type="number"
									v-model="handlePostageVal"
									@input="$event => (handlePostageVal = checkedInput($event))"
									placeholder="请填写运费"
									maxlength="20"
								/>
							</view>
							<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">¥{{ 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="handleComfirm"> 确定 </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,
			handleColdChain: 0, // 记录冷链费
			handlePostage: 0, // 记录原始运费
			handlePostageVal: 0, // 记录修改的运费
			handlePostageFlag: 0, //  记录运费类型
			radioItems: [
				{
					name: '包邮',
					value: 0,
					checked: false
				},
				{
					name: '不包邮',
					value: 1,
					checked: false
				},
				{
					name: '到付',
					value: 2,
					checked: false
				}
			],
			handleTotalPrice: 0, // 记录原始供应商总价
			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.handleColdChain = data.coldChain
			this.handleTotalPrice = data.totalPrice
			this.handlePostage = this.handlePostageVal = data.postage
			this.handlePostageFlag = data.handlePostageFlag
			console.log('handlePostageFlag', this.handlePostageFlag)
			switch (this.handlePostageFlag) {
				case 0:
					this.radioItems[0].checked = true
					break
				case 1:
					this.radioItems[1].checked = true
					break
				case 2:
					this.radioItems[2].checked = true
					break
			}
		},
		//处理格式
		checkedInput(e) {
			const value = e.detail.value
			const pattern = /[^0-9.]/g
			return Number(value.replace(pattern, ''))
		},
		// 勾选冷链费
		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)
			}
		},
		// 切换运费类型0包邮 1 不包邮 2 到付
		radioChange($event) {
			this.handlePostageFlag = this.handleSupplier.handlePostageFlag = $event.detail.value * 1
			if (this.handlePostageFlag === 1) {
				this.handleSupplier.postage = this.handlePostage
			} else {
				this.handleSupplier.postage = this.handlePostage
			}
		},
		// 确定运费
		handleComfirm() {
			if (this.handlePostageFlag === 0) {
				let handleTotalPrice = this.isColdChina
					? this.handleTotalPrice
					: Number(this.repiceNumSub(this.handleTotalPrice, this.handleColdChain))
				this.handleSupplier.totalPrice = Number(
					this.repiceNumSub(handleTotalPrice, this.handleSupplier.postage)
				)
				this.$emit('changePostage', this.handleSupplier, this.index)
			} else if (this.handlePostageFlag === 1) {
				if (this.handlePostageVal == 0 || !this.handlePostageVal) {
					this.$util.msg('请填写运费', 2000)
					return
				}
				let handleTotalPrice = this.isColdChina
					? this.handleTotalPrice
					: Number(this.repiceNumSub(this.handleTotalPrice, this.handleColdChain))
				this.handleSupplier.totalPrice = Number(
					(handleTotalPrice * 100 - this.handlePostage * 100 + this.handlePostageVal * 100) / 100
				)
				this.handleSupplier.postage = this.handlePostageVal
				this.$emit('changePostage', this.handleSupplier, this.index)
			} else {
				let handleTotalPrice = this.isColdChina
					? this.handleTotalPrice
					: Number(this.repiceNumSub(this.handleTotalPrice, this.handleColdChain))
				this.handleSupplier.totalPrice = Number(
					this.repiceNumSub(handleTotalPrice, this.handleSupplier.postage)
				)
				this.$emit('changePostage', this.handleSupplier, this.index)
			}
			this.popupShow = false
		},
		//减法精确，arg2:被减数，arg1:减数
		repiceNumSub(arg1, arg2) {
			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: 60rpx;
		line-height: 60rpx;
		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;
			}
		}
		.form-input {
			width: 100%;
			height: 60rpx;
			line-height: 60rpx;
			flex-grow: 1;
			box-sizing: border-box;
			padding: 0 20rpx;
			border: 1px solid #e1e1e1;
			border-radius: 4rpx;
		}
	}
}
.freight-pay {
	position: absolute;
	right: -60%;
	transform: scale(0.7);
}
</style>
