<template>
	<view class="app">
		<view class="address" v-for="(item,index) in goodPatient" :key="index">
			<view class="addressinfo">
				<view class="item" @tap='goorder(item)'>
					{{item.receiveName}},
					{{item.receivePhone}}
				</view>
				<view class="item detail" @tap='goorder(item)'>
					<span v-if="item.areaName">{{item.areaName}}</span>
					<span v-if="item.receiveAddress">{{item.receiveAddress}}</span>
				</view>
				<view class="item detail" style="border: none;font-size: 32rpx;margin-top: 40rpx;">
					<view class="" style='width: 50%;display: inline-block;'>
						<view class="" style="display: inline-block;">
							默认地址
						</view>
						<view class="" style="transform: translateY(16%);display: inline-block;margin-left: 20rpx;">
							<u-switch v-model="item.defaultAddressFlag" size='36' @change='change(item)'></u-switch>
						</view>
					</view>
					<view class="" style="display: inline-block;width: 50%;">
						<view class="update">
							<view class="up ups" @tap='updataxg(item)'>
								修改
							</view>
							<view class="up" @tap='del(item)'>
								删除
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="ADDress" @tap='add()'>
			<image src="@/static/pagesB/add.png" mode=""></image>
			<span class="ADDtext">
				添加地址
			</span>
		</view>
		<!-- 弹框 -->
		<u-popup v-model="show" mode="center" width="85%" height="600rpx" border-radius="30" @close='cencel()'>
			<view class="payment" @click.stop=''>
				<span>{{shipAddress}}</span>
				<view class="cencel" @tap="cencel()">
					<image src="@/static/pagesB/gb.png" mode=""></image>
				</view>
				<view class="addinfos">
					<u-field v-model="infolist.receiveName" label="收货人" placeholder="姓名" class="items" maxlength='10'>
					</u-field>
					<u-field v-model="infolist.receivePhone" label="手机号" placeholder="电话" class="items" maxlength='11'>
					</u-field>
					<view class="area" @click="showPicker">
						<!-- <view class="area" @tap='areashow=true'> -->
						<u-field v-model="infolist.address" label="区域" placeholder="请选择" class="items" disabled>
						</u-field>
						<image src="../../static/huijiantou.png" mode=""></image>
					</view>
					<u-field v-model="infolist.receiveAddress" label-width="170" label="详细地址" placeholder="如街道、门牌号、小区等"
						class="items" maxlength='50'>
					</u-field>
					<!-- <view class="" style="font-size: 28rpx;height: 80rpx">
						<view class="" style="display: inline-block;">
							默认地址
						</view>
						<view class="" style="transform: translateY(8%);display: inline-block;margin-left: 20rpx;">
							<u-switch v-model="infolist.defaultAddressFlag" size='32'></u-switch>
						</view>
					</view> -->
				</view>
				<view class="save" @tap='submit()'>
					保存
				</view>
			</view>
		</u-popup>
		<!-- <u-select v-model="areashow" mode="mutil-column-auto" label-name='areaName' value-name='areaCode'
			:list="arealist" @confirm="areaconfirm"></u-select> -->
		<u-toast ref="uToast" />
		<view class="" v-if="cityPickershow">
			<m-city style='z-index: 10076;' :provinceData="list" headTitle="地区选择" ref="cityPicker"
				@funcValue="getpickerParentValue" pickerSize="4">
			</m-city>
		</view>
	</view>
</template>

<script>
	import {
		goodPatientInfo,
		addnursingStation,
		delnursingStation,
		nursingStationGoodsinfo,
		updatenursingStation,
		getSubordinateRegions,
		getSubordinate,
		updateDefaultAddress
	} from '@/api/pagesB/modifyAddress/modifyAddress.js';
	import baseurl from '@/api/baseurl.js'
	import gkcity from "../../components/m-city/m-city.vue";
	export default {
		components: {
			"m-city": gkcity
		},
		data() {
			return {
				timer: null,
				isedit: true, //标识
				delshow: false, //删除弹框
				content: '确认要删除信息吗？',
				show: false, //弹框
				// areashow: false, //经纬度开关
				// arealist: [], //区街道list
				areaCode: '',
				shipAddress: '添加地址',
				infolist: {
					receiveName: '',
					receivePhone: '',
					receiveAddress: '',
					areaCode: null,
					patientId: '',
					address: null, //页面所属区域
					defaultAddressFlag: false,
				},
				goodPatient: [], // 查询地址信息
				patientId: null,
				url: '',
				updata: [],
				list: [{
					id: "",
					localName: "请选择",
					children: [],
				}, ],
				cityPickershow: false,
				addresslength: null,
			}
		},
		onShow() {
			this.goodsList()
			this.areaInfo()
		},
		onLoad(options) {
			let that = this
			console.log(options)
			this.updata = JSON.parse(options.updata)
		},
		methods: {
			change(status) {
				if (!status.defaultAddressFlag) {
					status.defaultAddressFlag = true
				} else {
					this.goodPatient.forEach(e => {
						e.defaultAddressFlag = 0
					})
					status.defaultAddressFlag = 1
					updateDefaultAddress(this.goodPatient).then(res => {
						if (res.code == 200) {
							this.goodsList();
							uni.$emit('updata', {
								updata: JSON.stringify(this.updata),
								useritem: JSON.stringify(status)
							})
							uni.navigateBack({
								delta: 1
							});
						}
					})
				}
			},
			// 三级地址联动回调
			getpickerParentValue(e) {
				console.log(e)
				e = e.filter(ele => ele.localName != '暂不选择')
				e = e.filter(ele => ele.localName != '暂无需选择')
				this.addresslength = e
				this.infolist.address = ''
				this.infolist.areaCode = ''
				if (e && e.length >= 1) {
					e.forEach(el => {
						this.infolist.address = this.infolist.address + el.localName
					})
					this.infolist.areaCode = e[e.length - 1].id
				}
				// if (e) {
				// 	if (e.length == 4) {
				// 		this.infolist.address = e[0].localName + '-' + e[1].localName + '-' + e[2].localName + '-' + e[3]
				// 			.localName
				// 		this.infolist.areaCode = e[3].id
				// 	} else if (e.length == 3) {
				// 		this.infolist.address = e[0].localName + '-' + e[1].localName + '-' + e[2].localName
				// 		this.infolist.areaCode = e[2].id
				// 	} else if (e.length == 2) {
				// 		this.infolist.address = e[0].localName + '-' + e[1].localName
				// 		this.infolist.areaCode = e[1].id
				// 	} else if (e.length == 1) {
				// 		this.infolist.address = e[0].localName
				// 		this.infolist.areaCode = e[0].id
				// 	} else {
				// 		this.infolist.address = ''
				// 		this.infolist.areaCode = ''
				// 	}
				// }
			},
			// 显示三级地址联动
			showPicker() {
				this.cityPickershow = true
				this.$refs.cityPicker.show();
			},
			//将信息携带至订单页面
			goorder(item) {
				uni.$emit('updata', {
					updata: JSON.stringify(this.updata),
					useritem: JSON.stringify(item)
				})
				uni.navigateBack({
					delta: 1
				});
			},
			// 查询地址信息
			goodsList() {
				let that = this
				const value = uni.getStorageSync('userinfo');
				if (value) {
					that.patientId = value.id
					goodPatientInfo(that.patientId).then(res => {
						res.data.forEach(e => {
							e.address = e.areaName + e.receiveAddress
							if (e.defaultAddressFlag == 1) {
								e.defaultAddressFlag = true
							} else {
								e.defaultAddressFlag = false
							}
						})
						that.goodPatient = res.data
					})
				} else {
					that.$refs.uToast.show({
						title: '登录状态异常',
						type: 'error'
					})
					if (that.timer) {
						clearTimeout(that.timer)
					}
					that.timer = setTimeout(e => {
						uni.navigateBack({
							delta: 2
						})
					}, 1500)
				}
			},
			// 保存按钮
			submit() {
				let that = this
				if (that.isedit == true) {
					if (that.addresslength) {
						if (that.addresslength.length > 2) {
							addnursingStation(that.infolist).then(res => {
								if (res.code == 200) {
									that.goodsList()
									that.$refs.uToast.show({
										title: '新增成功',
										type: 'success'
									})
									that.cencel();
								} else {
									that.$refs.uToast.show({
										title: res.msg,
										type: 'error'
									})
								}
							})
						} else {
							this.Toast()
						}
					} else {
						this.Toast()
					}
				} else {
					if (that.addresslength) {
						if (that.addresslength.length > 2) {
							this.xgdata();
						} else {
							this.Toast()
						}
					} else {
						this.xgdata();
					}
				}
			},
			Toast() {
				this.$refs.uToast.show({
					title: '区域应选择所在的区或街道,请重新选择!',
					type: 'error'
				})
			},
			xgdata() {
				var that = this
				updatenursingStation(that.infolist).then(res => {
					if (res.code == 200) {
						that.goodsList()
						that.$refs.uToast.show({
							title: '修改成功',
							type: 'success'
						})
						that.cencel();
					} else {
						that.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						})
					}
				})
			},
			// submit() {
			// if (this.addresslength) {
			// 	if (this.addresslength.length > 2) {
			// 		this.data();
			// 	} else {
			// 		this.$refs.uToast.show({
			// 			title: '所属区域应选择所在的区或街道,请重新选择!',
			// 			type: 'error'
			// 		})
			// 	}
			// } else {
			// 	this.data()
			// }
			// },
			// 修改按钮
			updataxg(item) {
				this.cityPickershow = true
				this.shipAddress = '修改地址'
				this.isedit = false
				nursingStationGoodsinfo(item.id).then(res => {
					getSubordinate(res.data.areaCode).then(resp => {
						if (resp.data.streetName) {
							res.data.address = resp.data.provinceName + resp.data.cityName + resp.data
								.regionName + resp.data.streetName
						} else {
							res.data.address = resp.data.provinceName + resp.data.cityName + resp.data
								.regionName
						}
						this.infolist = res.data
						this.show = true
					})
				})
			},
			// 新增
			add() {
				this.shipAddress = '添加地址'
				this.cityPickershow = true
				this.infolist = {
					receiveName: '',
					receivePhone: '',
					receiveAddress: '',
					areaCode: '',
					patientId: this.patientId,
				}
				this.isedit = true
				this.show = true
			},
			// 删除
			del(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认要删除信息吗',
					success: function(res) {
						if (res.confirm) {
							if (that.goodPatient.length == 1) {
								that.$refs.uToast.show({
									title: '最少保留一条地址信息',
									type: 'error'
								})
							} else {
								delnursingStation(item.id).then(res => {
									that.goodsList()
									that.$refs.uToast.show({
										title: '删除成功',
										type: 'success'
									})
									that.delshow = false;
								})
							}
						} else if (res.cancel) {}
					}
				});
			},
			// 取消
			cencel() {
				this.show = false
				this.cityPickershow = false
			},
			// 选择区街道
			areaInfo() {
				getSubordinateRegions().then(res => {
					//区下无街道添加一个空街道
					this.list = res.data;
				})
			},
			// 选择区街道
			// areaInfo() {
			// 	getSubordinateRegions(this.areaCode).then(res => {
			// 		//区下无街道添加一个空街道
			// 		res.data.forEach(e => {
			// 			if (e.children.length == 0) {
			// 				e.children.push({
			// 					areaCode: "",
			// 					areaName: "暂无街道",
			// 					children: null,
			// 					id: '',
			// 				})
			// 			}
			// 		})
			// 		this.arealist = res.data;
			// 	})
			// },
			//区街道选择
			areaconfirm(e) {
				this.infolist.areaCode = e[e.length - 1].value
				this.infolist.address = e[0].label + '-' + e[1].label
			},
		},
		onPullDownRefresh() { //下拉刷新
			this.goodsList()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
	}
</script>

<style lang="scss">
	@import './modifyAddress.scss'
</style>
