<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)'>
					{{item.address}}
				</view>
				<view class="update">
					<view class="up ups" @tap='updataxg(item)'>
						修改
					</view>
					<view class="up" @tap='del(item)'>
						删除
					</view>
				</view>
			</view>
		</view>
		<view class="ADDress" @tap='add()'>
			<image src="../../static/add.png" mode=""></image>
			<span class="ADDtext">
				添加收货地址
			</span>
		</view>
		<!-- 弹框 -->
		<u-popup v-model="show" mode="center" width="80%" height="50%" border-radius="30">
			<view class="payment">
				<span>{{shipAddress}}</span>
				<view class="cencel" @tap="cencel()">
					<image src="../../static/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" @tap='areashow=true'>
						<u-field v-model="infolist.address" label="地区" placeholder="请选择" class="items" disabled>
						</u-field>
						<image src="../../static/jiantou.png" mode=""></image>
					</view>

					<u-field v-model="infolist.receiveAddress" label-width="170" label="详细地址" placeholder="如街道、门牌号、小区等"
						class="items" maxlength='50'>
					</u-field>
				</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>
</template>

<script>
	import {
		goodPatientInfo,
		addnursingStation,
		delnursingStation,
		nursingStationGoodsinfo,
		updatenursingStation,
		getSubordinateRegions,
		getSubordinate
	} from '@/api/modifyAddress/modifyAddress.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				isedit: true, //标识
				delshow: false, //删除弹框
				content: '确认要删除信息吗？',
				show: false, //弹框
				areashow: false, //经纬度开关
				arealist: [], //区街道list
				areaCode: '',
				shipAddress: '添加收货地址',
				infolist: {
					receiveName: '',
					receivePhone: '',
					receiveAddress: '',
					areaCode: '',
					patientId: '',
					address: '', //页面所属区域
				},
				goodPatient: [], // 查询地址信息
				patientId: null,
				url: '',
				updata: [],
			}
		},
		onLoad(options) {
			let that = this
			this.url = options.url
			this.updata = JSON.parse(options.updata)
			//获取传值
			this.goodsList()
			this.areaInfo()
		},
		methods: {
			//将信息携带至订单页面
			goorder(item) {
				// if (this.url == "appointmenttime") {
				uni.$emit('updata', {
					updata: JSON.stringify(this.updata),
					useritem: JSON.stringify(item)
				})
				uni.navigateBack({
					delta: 1
				});
				// uni.navigateTo({
				// 	url: `/pages/appointmenttime/appointmenttime?updata=${JSON.stringify(this.updata)}&useritem=${JSON.stringify(item)}`
				// })
				// } else {
				// uni.navigateTo({
				// url: `/pages/confirmOrder/confirmOrder?updata=${JSON.stringify(this.updata)}&useritem=${JSON.stringify(item)}`
				// })
				// }
			},
			// 查询地址信息
			goodsList() {
				let that = this
				try {
					const value = uni.getStorageSync('patientId');
					if (value) {
						that.patientId = value
						that.infolist.patientId = value
						goodPatientInfo(that.patientId).then(res => {
							res.data.forEach(e => {
								e.address = e.provinceName + e.cityName + e.regionName + e
									.streetName + e.receiveAddress
							})
							that.goodPatient = res.data
						})
					}
				} catch (e) {}
				// uni.getStorage({
				// 	key: 'patientId',
				// 	success: function(res) {
				// 		that.patientId = res.data
				// 		that.infolist.patientId = res.data
				// 		goodPatientInfo(that.patientId).then(res => {
				// 			res.data.forEach(e => {
				// 				e.address = e.provinceName + e.cityName + e.regionName + e
				// 					.streetName + e.receiveAddress
				// 			})
				// 			that.goodPatient = res.data
				// 		})
				// 	}
				// });
			},
			// 保存按钮
			submit() {
				let that = this
				if (that.isedit == true) {
					addnursingStation(that.infolist).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '新增成功',
								duration: 1500
							});
							that.show = false;
							setTimeout(e => {
								that.goodsList()
							}, 1500)
						}
					})
				} else {
					updatenursingStation(that.infolist).then(res => {
						uni.showToast({
							title: '修改成功',
							duration: 1500
						});
						that.show = false;
						setTimeout(e => {
							that.goodsList()
						}, 1500)
					})
				}
			},
			// 修改按钮
			updataxg(item) {
				this.shipAddress = '修改收货地址'
				this.isedit = false
				nursingStationGoodsinfo(item.id).then(res => {
					this.infolist = res.data
					getSubordinate(res.data.areaCode).then(res => {
						this.infolist.address = res.data.regionName + '-' + res.data.streetName
						this.show = true
					})
				})
			},
			// 新增
			add() {
				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) {
							delnursingStation(item.id).then(res => {
								uni.showToast({
									title: '删除成功',
									duration: 1500,

								});
								setTimeout(e => {
									that.goodsList()
								}, 1500)
								that.delshow = false;
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 取消
			cencel() {
				this.show = false
				this.infolist = {}
				// this.address=""
			},
			// 选择区街道
			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.address = e[0].label + '-' + e[1].label
				this.infolist.address = e[0].label + '-' + e[1].label

			},
		}
	}
</script>

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