<template>
    <van-address-edit
            :area-list="areaList"
            show-postal
            :show-delete="showDelete"
            show-set-default
            show-search-result
            :address-info="addressinfo"
            :search-result="searchResult"
            @save="onSave"
            @delete="onDelete"
            @change-detail="onChangeDetail"
    />
</template>

<script>
import request from '@/utils/request'
export default {
  name: 'address_add',
  data () {
    return {
      showDelete: false,
      areaList: {},
      searchResult: [],
      addressinfo: {
        'name': '',
        'tel': '',
        'country': '',
        'province': '',
        'city': '',
        'county': '',
        'areaCode': '',
        'postalCode': '',
        'addressDetail': '',
        'isDefault': false
      },
      api: '/shop/ajax/AddAddress.html'
    }
  },
  created () {
    this.getAreaList()
  },
  methods: {
    onSave (e) {
      let self = this
      let d = 0
      if (e.isDefault) {
        d = 1
      }
      let senddata = {
        areaCode: e.areaCode,
        row: {
          obj: e,
          name: e.name,
          tel: e.tel,
          address: e.addressDetail,
          isdefault: d

        }}
      if (self.$route.query.id > 0) {
        senddata.row['id'] = self.$route.query.id
      }
      request('post',this.api,senddata).then((res) => {
        if (res.data.code == 1) {
          self.$toast(res.data.msg)
          window.history.go(-1)
        } else {
          this.$toast(res.data.msg)
        }
      })
    },
    onDelete (e) {
      let self = this
      self.$toast.loading({
        mask: true,
        message: '处理中...'
      })
      request('get','/shop/ajax/deleteAddress.html', {id: self.$route.query.id}).then((res) => {
        self.$toast.clear()
        window.history.go(-1)
      })
    },
    onChangeDetail () {},
    getAreaList () {
      let self = this
      self.$toast.loading({
        mask: true,
        message: '处理中...'
      })
      request('get','/shop/ajax/getAreaList.html').then((res) => {
        self.$toast.clear()
        this.areaList = res.data
        if (self.$route.query.id > 0) {
          self.showDelete = true
          self.$toast.loading({
            mask: true,
            message: '处理中...'
          })
          self.api = '/shop/ajax/UpdateAddress.html'
          request('get','/shop/ajax/getAddressInfo.html',{id: self.$route.query.id}).then((res) => {
            self.$toast.clear()
            if (res.data.data.obj.isDefault == 'true') {
              res.data.data.obj.isDefault = true
            } else {
              res.data.data.obj.isDefault = false
            }
            self.addressinfo = res.data.data.obj
          })
        }
      })
    }
  }
}
</script>
