<template>
    <view class="page-main">
        <liu-chooseAddress ref="liuChooseAddress" @submit="submitAddress" @detele="deteleAddress">
        </liu-chooseAddress>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				id: null,
				type: 'add',
                // 'RBWBZ-CV5RW-VX7RW-36ZQL-YZVYF-DDBMM' 此处为作者为方便H5体验所留测试用key,可以用来体验，但请勿在项目内直接使用
            }
        },
		onLoad(option) {
			if (option) {
				this.id = option.id
				if (this.id) {
					this.getDetail()
				}
				
			}
			if(option && option.type){
				this.type = option.type
			}
		},
        methods: {
            //添加地址成功回调
            submitAddress(e) {
                console.log('添加调地址信息：', e)
				this.Post({
					username: e.userName,
					mobile: e.userPhone,
					is_default: e.agreeState ? '1' : '0',
					street_number: e.detailAddress,
					detail_addr: e.address,
					lat: e.latitude,
					lon: e.longitude,
					id: this.id || null,
					// 省市区后台会解析 这里是江苏省苏州市吴中区
					province_id: 320000,
					city_id: 320500,
					district_id: 320506,
					contactType: 'BSAMECITY'
					
				},'/api/uservice/user/' + (this.id > 0 ? 'edit' : 'addNew') + 'Consignee').then(res => {
					if(res.code == 1){
						uni.showModal({
							title: '提示',
							content: this.id>0?'编辑成功':'添加成功',
							showCancel: false,
							success: res => {
								if(res.confirm){
									uni.navigateBack({})
								}
							}
						})
					}else{
						uni.showModal({
							title: '提示',
							content: res.msg,
							showCancel: false,
							success: res => {
								
							}
						})
					}
				})
            },
            //删除地址回调
            deteleAddress(e) {
                console.log('所删除的地址信息：', e)
            },
            // 地址回显(编辑)调用此方法

			getDetail() {
				this.Post({
					id: this.id
				}, '/api/uservice/user/getContactInfoById').then(res => {
					if (res.code === 1) {
						res = res.data
						if (res && res.id > 0) {

							
							let obj = {
							    id: res.id,
							    detailAddress: res.street_number, //门牌号(详细地址)
							    userName: res.name, //联系人姓名
							    userPhone: res.tel, //手机号
							    title: '', //地址概述
							    address: res.detail_addr, //详细地址
							    longitude: res.lon, //经度
							    latitude: res.lat, //纬度
							    agreeState: res.is_default == 1 ? true : false, //是否是默认地址
							}
							this.$refs.liuChooseAddress.setData(obj)
						}
					}
				})
			},
        }
    }
</script>

<style lang="scss" scoped>
    .page-main {
        width: 100%;
        height: 100vh;
    }
</style>