<template>
	<!-- 填写收件人信息 -->
	<view class="back">
		<view class="info">
			<view class="item u-flex u-row-between">
				<text class="title">{{i18n.goods}}</text>
			</view>
			<view class="item u-flex " @click="tocitychange">
				<text class="left">{{i18n.country}}</text>
				<view class="right u-flex u-row-between" style="flex: 1;">
					<input type="text" :placeholder="i18n.selection" v-model="addname" />
					<image src="../static/express/next1.png" style="width: 36rpx;height: 36rpx;" mode=""></image>
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.name}}</text>
				<view class="right">
					<input type="text" :placeholder="i18n.name" v-model="name" />
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.phone}}</text>
				<view class="right">
					<input type="text" :placeholder="i18n.pleasePhoneNumber" v-model="phone" />
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.postalCode}}</text>
				<view class="right">
					<input type="text" :placeholder="i18n.PleacePostalCode" v-model="postalCode" />
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.city}}</text>
				<view class="right u-flex u-row-between " style="flex: 1;" @click="openCity">
					<input type="text" :placeholder="i18n.selection" disabled="" v-model="cityname" />
					<image src="../static/express/next1.png" style="width: 36rpx;height: 36rpx;" mode=""></image>
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.detailed}}</text>
				<view class="right">
					<input type="area" :placeholder="i18n.sect" v-model="detailed" />
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.companyName}}</text>
				<view class="right">
					<input type="area" :placeholder="i18n.company" v-model="companyName" />
				</view>
			</view>
			<view class="item u-flex ">
				<text class="left">{{i18n.email}}</text>
				<view class="right">
					<input type="area" :placeholder="i18n.pleaseEmail" v-model="email" />
				</view>
			</view>
			<!-- 默认地址 -->
			<view class="u-flex u-row-between" style="margin-top: 22rpx;">
				<view class="u-flex">
					<view class="">
						<u-checkbox-group v-model="checkboxValue1" placement="row" @change="checkboxChange">
							<u-checkbox activeColor="red" shape='circle' :customStyle="{marginRight: '4px'}" :name="active">
							</u-checkbox>
						</u-checkbox-group>
					</view>
					<text class="clear">{{i18n.addressBook}}</text>
				</view>
				<text class="clear" @click="clear">
					{{i18n.empty}}
				</text>
			</view>
		</view>
		<view class="" style="height: 166rpx;"></view>
		<view class="bottom">
			<view class="btn" @click="submit">
				{{i18n.enter}}
			</view>
		</view>
		<!-- 选择省市区 -->
		<u-picker :show="show" :columns="cityList" keyName="name" @confirm="countrycConfirm" @cancel="show = false"
			:closeOnClickOverlay="true" @close="close"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkboxValue1: [1],
				active: 1,
				addname: '',
				addid: '',
				show: false,
				cityList: [],
				cityid: '',
				cityname: "",

				name: "",
				phone: "",
				postalCode: "",
				detailed: "",
				email: "",
				companyName: "",
				is_default: 0
			};
		},
		onLoad(options) {
			if (options.id) {
				this.addressId = options.id;
				this.getAddressDetail(options.id);
			}
		},
		methods: {
			//回显数据
			getAddressDetail(id) {
				uni.$u.http.get(`/api/address/${id}`).then((res) => {
					console.log(res);
					this.cityname = res.province_name;
					this.name = res.name;
					this.phone = res.mobile;
					this.detailed = res.address;
					this.companyName = res.company_name;
					this.is_default = res.is_default;
					this.addname = res.country_name;
					this.email = res.email;
					this.addid = res.country_id;
					this.cityid = res.province_id;
					this.postalCode = res.zip_code;
				});
			},
			clear() {
				this.phone = ''
				this.name = ''
				this.addid = ''
				this.cityid = ''
				this.detailed = ''
				this.companyName = ''
				this.email = ''
				this.postalCodee = ''
				this.is_default = ''
				this.checkboxValue1 = []
				this.cityname = ''
				this.addname = ''
			},
			//确定
			enter(){
				if(this.is_default==1){
					this.submit()
				}else{
					
				}
			},
			submit() {
				const obj = {
					mobile: this.phone,
					name: this.name,
					country_id: this.addid,
					province_id: this.cityid,
					address: this.detailed,
					company_name: this.companyName,
					email: this.email,
					zip_code: this.postalCodee,
					// is_default: this.is_default
				};
				if (this.addressId) {
					uni.$u.http.put(`/api/address/${this.addressId}`, obj).then((res) => {
						const eventchannel = this.getOpenerEventChannel();
						eventchannel.emit('bottomadd', res)
						uni.navigateBack()
					});
				} else {
					uni.$u.http.post(`/api/address`, obj).then((res) => {
						const eventchannel = this.getOpenerEventChannel();
						eventchannel.emit('bottomadd', res)
						uni.navigateBack()
						// console.log(res);
						// this.$router.back();
					});
				}

				uni.$u.http.post(`/api/address`, obj).then((res) => {
					const eventchannel = this.getOpenerEventChannel();
					eventchannel.emit('bottomadd', res)
					uni.navigateBack()
					// console.log(res);
					// this.$router.back();
				});
			},
			//选择省市区
			countrycConfirm(e) {
				console.log(e);
				this.cityid = e.value[0].id
				this.cityname = e.value[0].name
				this.show = false;
			},
			//打开选择城市的弹窗
			openCity() {
				//判断是否选择国家
				if (this.addid) {
					this.show = true
					//判断选中的国家有没有城市数据
					// if (this.cityList[0].length > 0) {
					//   this.show = true;
					// } else {
					//   uni.$u.toast("该国家没有城市数据，请直接填写详细地址");
					// }
				} else {
					uni.$u.toast(this.i18n.Pleasecountry);
				}
			},
			checkboxChange(n) {
				console.log('change', n);
				this.checkboxValue1 = n
				if (n.length > 0) {
					this.is_default = 1
				} else {
					this.is_default = 0
				}
			},
			//获取国家
			tocitychange() {
				var that = this
				uni.navigateTo({
					url: '/pageB/citychange',
					events: {
						getadd(res) {
							console.log(res.id);
							that.addid = res.id
							that.addname = res.name
							that.getCityList()
						}
					}
				})
			},
			//根据国家获取城市
			getCityList() {
				uni.$u.http.get(`/api/area/tree?pid=` + this.addid).then((res) => {
					this.cityList = [res];
					console.log(this.cityList);
				});
			},
		},
		computed: {
			i18n() {
				return this.$t('index')
			}
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.i18n.goods
			})
		},
	}
</script>

<style lang="scss">
	.back {
		padding: 20rpx 24rpx;
		box-sizing: border-box;

		.bottom {
			width: 750rpx;
			height: 166rpx;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			left: 0;
			padding: 18rpx 24rpx;
			box-sizing: border-box;

			.btn {
				width: 702rpx;
				height: 88rpx;
				background: #F83224;
				border-radius: 44rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 88rpx;
				text-align: center;
				font-style: normal;
			}
		}

		.add {
			width: 702rpx;
			// height: 160rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 24rpx;
			box-sizing: border-box;

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

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

			.phone {
				font-family: SFPro, SFPro;
				font-weight: 400;
				font-size: 24rpx;
				color: #333333;
				line-height: 28rpx;
				text-align: left;
				font-style: normal;
				margin-left: 16rpx;
			}
		}

		.history {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			color: #333333;
			line-height: 42rpx;
			text-align: left;
			font-style: normal;
			margin: 28rpx 0 24rpx;
		}

		.info {
			padding: 0 24rpx 28rpx;
			box-sizing: border-box;
			width: 702rpx;
			// height: 742rpx;
			background: #FFFFFF;
			border-radius: 16rpx;

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

			.item {
				border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);
				min-height: 100rpx;
				padding: 20rpx 0;
				box-sizing: border-box;

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

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

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

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


			}
		}
	}
</style>