<template>
	<!-- 物流报价器 -->
	<view class="back">
		<!-- 城市 -->
		<view class="add u-flex u-row-around">
			<text class='address' @click="change(0)">{{original||i18n.Starting}}</text>
			<image src="static/images/arrows.png" mode="" style="width: 130rpx;height: 10rpx;"></image>
			<text class='address' @click="change(1)">{{bourn||i18n.destination}}</text>
		</view>
		<!-- 商品类型 -->
		<view class="type">
			<view class="title u-flex">
				<text>{{i18n.shoptype}}</text>
				<image src="" mode="" style="width: 12rpx;height: 12rpx;"></image>
			</view>
			<view class="u-flex" style="margin-top: 24rpx;">
				<view :class="index==idx?'left':'right'" @click="good(item,idx)" style="margin-right: 20rpx;"
					v-for="(item,idx) in goods" :key="idx">
						{{item.value=='normal'? i18n.Normal :i18n.special}}
					</view>
				<!-- <view class="right" style="margin-left: 20rpx;">特殊商品</view> -->
			</view>
		</view> 
		<!-- 跨境物流 -->
		<view class="logistics u-flex u-row-between">
			<text style="font-weight: 600;">{{i18n.flow}}</text>
			<view class="u-flex" @click="changewu">
				<text class="change">{{leftname?leftname+' |  '+rightname : i18n.selection}}</text>
				<u-icon name="arrow-right" size='16'></u-icon>
			</view>
		</view>
		<!-- 商品重量 -->
		<view class="weight ">
			<view class="u-flex u-row-between">
				<text style="font-weight: 600;">{{i18n.Commodity}}</text>
				<u-number-box v-model="value">
					<view slot="minus" class="minus">
						<u-icon name="minus" size="12"></u-icon>
					</view>
					<view slot="input"
						style="width: 50px;text-align: center;height: 30px;line-height: 30px;background: #f5f5f5;"
						class="input u-flex">
						<input type="text" v-model="value" />
						<text>kg</text>
					</view>
					<view slot="plus" class="plus">
						<u-icon name="plus" size="12"></u-icon>
					</view>
				</u-number-box>
			</view>
			<view class="u-flex u-row-between" style="margin-top: 30rpx;">
				<text style="font-weight: 600;">{{i18n.volume}}</text>
				<text style="font-size: 20rpx;
color: rgba(34, 34, 34, 0.5);">{{i18n.accurately}}</text>
			</view>
			<view class="u-flex u-row-between" style="margin-top: 32rpx;">
				<view class="vite u-flex">
					<input type="text" :placeholder="i18n.length" v-model="length" />
					<text>cm</text>
				</view>
				<view class="">*</view>
				<view class="vite u-flex">
					<input type="text" :placeholder="i18n.wide" v-model="width" />
					<text>cm</text>
				</view>
				<view class="">*</view>
				<view class="vite u-flex">
					<input type="text" :placeholder="i18n.height" v-model="height" />
					<text>cm</text>
				</view>
			</view>
		</view>
		<!-- 获取价格 -->
		<view @click="gain" :class="gat==1?'yuan':'price'">
			{{i18n.Acquisition}}
		</view>
		<view class="gain" v-if="gatShow">
			<view class="">
				<text class='weigh'>{{i18n.Calculated}}：</text>
				<text class='weigh' style="color: rgba(34, 34, 34, 1);">{{value}}kg</text>
			</view>
			<view class="u-flex u-row-between"
				style="border-top: 2rpx solid #979797;margin-top: 32rpx;padding-top: 20rpx;">
				<view class="">
					<text class='weigh'>{{i18n.Estimated}}</text>
					<text style="margin-left: 6rpx;" class='red'>¥</text>
					<text class='red' style="font-size: 32rpx;">{{money}}</text>
					<text class='red'>{{i18n.since}}</text>
				</view>
				<view class="info" @click="logistics">{{i18n.details}}</view>
			</view>
		</view>
		<!-- 地址选择 -->
		<u-picker :show="show" ref="uPicker" keyName="name" :columns="columns" @confirm="confirm"
			@change="changeHandler"></u-picker>
		<!-- 跨境物流 -->
		<u-popup round='28' :show="logshow" @close="close">
			<view class="pop">
				<kj-flow :goodstype='goodstype' :province_id='province_id' ref="child" @success='success'
					@close='logshow=false'></kj-flow>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logshow: false, //物流选择
				value: 1,
				show: false,
				original: '', //起始地
				bourn: '', //目的地
				type: 0,
				gat: 0,
				gatShow: false,
				columns: [

				],
				columnData: [

				],
				goods: [],
				index: 0,
				domestic: 1,
				province_id: '',
				goodstype: 'normal',
				container_id: '', //货柜id
				leftname: '',
				rightname: '',
				money: '',
				length: '',
				width: '',
				height: ''
			};
		},
		onLoad() {
			// this.goods = [{
			// 		name: this.i18n.Normal
			// 	},
			// 	{
			// 		name: this.i18n.special
			// 	}
			// ]
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.i18n.compute
			})
			// this.getarea()
			this.goodtype()
		},
		computed: {
			i18n() {
				return this.$t('index')
			}
		},
		methods: {
			//商品类型
			goodtype() {
				uni.$u.http.get('/api/express-goods-type', ).then((res) => {
					this.goods = res
				}).catch(() => {

				})
			},
			//物流选择
			changewu() {
				if (this.province_id) {
					this.logshow = true
					setTimeout(() => {
						this.$refs.child.transport();
					}, 800)
				} else {
					this.$u.toast('请先选择城市')
				}
			},
			//物流计算器
			calculate() {
				if (this.container_id == '') {
					this.$u.toast('请选择货柜')
					return
				}
				uni.$u.http.get('/api/express-order/calculate', {
					params: {
						container_id: this.container_id, //		是	String	货柜ID
						weight: this.value, //		是	String	重量(kg)
						length: this.length, //		是	String	长(m)
						width: this.width, //		是	String	宽(m)
						height: this.height, //		是	String	高(m)
						type: this.goodstype, //		是	String	商品类型: normal. 普通商品, special.特殊商品
					}
				}).then((res) => {
					this.money = res
				}).catch(() => {

				})
			},
			//地区列表
			getarea() {
				this.columns = []
				this.columnData = []
				uni.$u.http.get('/api/area/tree', {
					params: {
						is_domestic: this.domestic,
						pid: 0
					}
				}).then((res) => {
					console.log(res)
					// this.columns.splice(0,1, [{name:res[0].name}]) 
					this.columns = [res]
					this.columns[1] = res[0].children
					// this.columnData[0] = res[0].children
					res.forEach(item => {
						this.columnData.push(item.children)
					})
				}).catch(() => {

				})
			},
			success(msg) {
				this.logshow = false
				console.log(msg);
				this.leftname = msg.leftname
				this.container_id = msg.dateid
				this.rightname = msg.rightname
			},
			close() {
				this.logshow = false
			},
			//切换商品类型
			good(item, idx) {
				this.goodstype = item.value
				this.index = idx
			},
			//物流详情
			logistics() {
				uni.navigateTo({
					url: '/pageA/logisticsinfo?weight=' + this.value + '&money=' + this.money
				})
			},
			//获取价格
			gain() {
				if (this.container_id == '') {
					this.$u.toast('请选择货柜')
				} else {
					this.gat = 1
					this.gatShow = true
					this.calculate()
				}
			},
			//开启地址选择器
			change(type) {
				this.type = type
				this.show = true
				if (type == 0) {
					this.domestic = 1
					this.getarea()
				} else {
					this.domestic = 0
					this.getarea()
				}
			},
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, this.columnData[index])
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				console.log('confirm', e)
				this.show = false
				if (this.type == 0) {
					this.original = e.value[0].name + '-' + e.value[1].name
					// console.log(e.value[0]);
				} else {
					this.bourn = e.value[0].name + '-' + e.value[1].name
					this.province_id = e.value[1].id
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.vite {
		width: 198rpx;
		height: 60rpx;
		background: #F4F4F4;
		border-radius: 36rpx;
		padding: 12rpx 28rpx;
		box-sizing: border-box;
	}

	.pop {
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 36rpx;
		color: #333333;
		line-height: 50rpx;
		text-align: left;
		font-style: normal;
		padding: 32rpx 28rpx 0 0;
		box-sizing: border-box;
	}

	.back {
		background-color: rgba(244, 244, 244, 1);
		padding: 20rpx 24rpx;
		box-sizing: border-box;

		.yuan {
			width: 702rpx;
			height: 96rpx;
			background: #F83224;
			box-shadow: 0rpx 16rpx 40rpx -12rpx rgba(255, 21, 21, 0.5);
			border-radius: 48rpx;
			margin-top: 62rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 96rpx;
			text-align: center;
			font-style: normal;
		}

		.gain {
			width: 702rpx;
			height: 208rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 68rpx;
			padding: 40rpx 24rpx 34rpx 24rpx;
			box-sizing: border-box;

			.weigh {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #777777;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}

			.info {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #333333;
				line-height: 34rpx;
				text-align: left;
				font-style: normal;
			}

			.red {
				font-family: HarmonyOS_Sans_Medium;
				font-size: 20rpx;
				color: #F83224;
				line-height: 26rpx;
				text-align: left;
				font-style: normal;
			}
		}

		//步进器
		.minus {
			width: 22px;
			height: 22px;
			// border-width: 1px;
			// border-color: #E6E6E6;
			// border-style: solid;
			border-top-left-radius: 100px;
			border-top-right-radius: 100px;
			border-bottom-left-radius: 100px;
			border-bottom-right-radius: 100px;
			@include flex;
			justify-content: center;
			align-items: center;
		}

		.input {
			padding: 0 10px;
		}

		.plus {
			width: 22px;
			height: 22px;
			// background-color: #FF0000;
			border-radius: 50%;
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			justify-content: center;
			align-items: center;
		}

		//商品重量
		.weight {
			padding: 34rpx 20rpx;
			box-sizing: border-box;
			width: 702rpx;
			// height: 108rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 20rpx;
		}


		//获取价格
		.price {
			width: 702rpx;
			height: 96rpx;
			background: #F83224;
			box-shadow: 0rpx 16rpx 40rpx -12rpx rgba(255, 21, 21, 0.5);
			border-radius: 48rpx;
			position: fixed;
			bottom: 72rpx;
			left: 24rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 96rpx;
			text-align: center;
			font-style: normal;
		}

		//跨境物流
		.logistics {
			width: 702rpx;
			height: 96rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 28rpx 22rpx;
			box-sizing: border-box;
			margin-top: 20rpx;

			.change {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #F83224;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
				margin-right: 8rpx;
			}
		}

		// 商品类型
		.type {
			padding: 28rpx 20rpx;
			box-sizing: border-box;
			width: 702rpx;
			height: 190rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 20rpx;

			.title {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 28rpx;
				color: #222222;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}

			.left {
				// width: 202rpx;
				padding: 0 30rpx;
				height: 60rpx;
				border-radius: 36rpx;
				border: 1rpx solid #F83224;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #F83224;
				line-height: 60rpx;
				text-align: center;
				font-style: normal;
				box-sizing: border-box;
			}

			.right {
				// width: 202rpx;
				height: 60rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				border: 1rpx solid #979797;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #979797;
				line-height: 60rpx;
				box-sizing: border-box;
				text-align: center;
				font-style: normal;
			}
		}

		//城市
		.add {
			width: 702rpx;
			height: 144rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 50rpx 8rpx;
			box-sizing: border-box;

			.address {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #222222;
				line-height: 44rpx;
				text-align: right;
				font-style: normal;
			}
		}
	}
</style>