<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      :address-info="{
        name: editAddressData.name,
        tel: editAddressData.tel,
        addressDetail: editAddressData.detailAddress,
        areaCode: areaCode,
      }"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>
<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
var _ = require("lodash");
import { memberAddress } from "../../ajax/getData";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      areaCode: "",
    };
  },
  props: ["editAddressData"],
  computed: {},
  methods: {
    async onSave(content) {
      //   Toast('save');
      let provinceId = Number(content.areaCode.substring(0, 2)); //省份id
      let cityId = Number(content.areaCode.substring(2, 4)); //城市id
      let districtId = Number(content.areaCode.substring(4, 7)); //区域编码
      //id     详细地址   /收货人姓名 电话号码 省份id/城市id/区域编码
      let memberId = JSON.parse(localStorage.getItem("userInfo")).data.id;
      let postData = {
        memberId,
        provinceId: 25,
        cityId: 36,
        districtId: 418,
        // provinceId,
        // cityId,
        // districtId,
        detailAddress: content.addressDetail,
        recipient: content.name,
        recipientPhone: content.tel,
        default: content.default,
      };
      let saveType = this.editAddressData.saveType ? "update" : "add";
      let data = await memberAddress(saveType, postData);
      if (data) {
        this.$message({
          type: "warning",
          message: data.msg,
        });
        if (data.code == "0") {
          this.$emit("isClose", false);
        }
      }
    },
    async onDelete() {
      // Toast("delete");
      let postData = {
        ids: this.editAddressData.id,
        memberId: this.editAddressData.memberId,
      };
      let data = await memberAddress("delete", postData);
      if (data) {
        this.$message({
          type: "warning",
          message: data.msg,
        });
        if (data.code == "0") {
          this.$emit("isClose", false);
        }
      }
    },
    async areaCodeInit() {
      var city = this.editAddressData.city;
      var district = this.editAddressData.district;
      this.areaCode = "1310001";
      // _.forEach(this.areaList.city_list, (o, c) => {
      //   if (o == city) {
      //     // let cityId = String(_.take(c, 2));
      //     let cityId = c.substring(0, 4);
      //     _.forEach(this.areaList.county_list, (i, a) => {
      //       if (i == district) {
      //         // let districtId = String(_.take(a, 2));
      //         let districtId = a.substring(0, 4);
      //         if (cityId == districtId) {
      //           this.areaCode = a;
      //         }
      //       }
      //     });
      //   }
      // });
    },
  },
  mounted() {
    this.areaCodeInit();
  },
};
</script>

<style>
.van-area {
  width: 400px;
  margin: 0 auto;
  border: 1px solid #808285;
}
.van-popup {
  background-color: transparent;
}
.van-overlay {
  background-color: transparent;
}
.v-modal {
  background: transparent;
}
</style>