<template>
  <div class="edit-address">
    <create-address
      v-if="address"
      :address="address"
      @handle-update="handleUpdate"
      @handle-delete="handleDelete"
    ></create-address>
  </div>
</template>

<script>
import CreateAddress from '@/pagesAddress/create-address-cop/index.vue'
import {mapActions} from 'vuex'
import {msgData} from '@/config.js'
import {modal, errToast} from '@/utils/uniapi.js'
export default {
  components: {
    CreateAddress
  },
  data () {
    return {
      address: null
    }
  },
  onLoad (query) {
    console.log('onload')
    let address = JSON.parse(decodeURIComponent(query.address))
    // console.log(address)
    this.address = address
  },
  methods: {
    ...mapActions({
      delAddressItem: 'addressVue/delListItem',
      updateAddressItem: 'addressVue/updateListItem'
    }),
    async handleUpdate (json) {
      try {
        this.showLoad()
        await this.updateAddressItem(json)
        uni.navigateBack({delta: 1})
      } catch (e) {
        console.error(e)
        errToast(e)
      } finally {
        this.hideLoad()
      }
    },
    async handleDelete (id) {
      try {
        this.showLoad()
        console.log(id)
        const confirm = await modal('', msgData.delWaybillAddress)
        if (confirm) {
          // 删除地址
          await this.delAddressItem(id)
          uni.navigateBack({delta: 1})
        }
      } catch (e) {
        console.error(e)
        errToast(e)
      } finally {
        this.hideLoad()
      }
    }
  }
}
</script>

<style lang="scss">
@import './edit-address.scss';
</style>