"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
const utils_emitter = require("../../../utils/emitter.js");
const _sfc_main = {
  __name: "newaddress_Info",
  setup(__props) {
    const addressParam = common_vendor.ref({
      name: "",
      phone: "",
      region: "",
      detailAddress: "",
      isDefault: 0
    });
    const isAdd = common_vendor.ref(false);
    common_vendor.onLoad((options) => {
      if (JSON.stringify(options) != "{}") {
        addressParam.value = JSON.parse(options.editInfo);
      } else {
        isAdd.value = true;
      }
    });
    common_vendor.onShow(() => {
    });
    common_vendor.onMounted(() => {
      utils_emitter.emitter.on("region", (val) => {
        addressParam.value.region = val;
      });
      if (!isAdd.value)
        utils_emitter.emitter.emit("addRegion", addressParam.value.region);
    });
    const defaultAddress = (event) => {
      event.detail.value[0] ? addressParam.value.isDefault = 1 : addressParam.value.isDefault = 0;
    };
    const newAddress = async () => {
      const values = Object.values(addressParam.value);
      if (values.some((value) => value === null || value === void 0 || value === "")) {
        await common_vendor.index.showToast({
          icon: "error",
          title: "字段不能为空"
        });
        return;
      }
      console.log(addressParam.value);
      if (addressParam.value.id != void 0) {
        const res = await common_vendor.index.request({
          url: api_request.baseUrl + "/address/update",
          method: "POST",
          header: {
            "cookie": common_vendor.wx$1.getStorageSync("cookie")
          },
          data: { ...addressParam.value }
        });
        console.log("res1==>", res.data);
        sucRes(res.data.code);
      } else {
        const res = await common_vendor.index.request({
          url: api_request.baseUrl + "/address/add",
          method: "POST",
          header: {
            "cookie": common_vendor.wx$1.getStorageSync("cookie")
          },
          data: { ...addressParam.value }
        });
        console.log("res2==>", res.data);
        sucRes(res.data.code);
      }
    };
    const sucRes = (res) => {
      if (res === 1) {
        common_vendor.index.navigateBack({
          //返回上一页
        });
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "新增地址失败"
        });
        return;
      }
    };
    const chooseLocation = () => {
      common_vendor.index.chooseLocation({
        success: (res) => {
          addressParam.value.region = res.address;
          addressParam.value.detailAddress = res.name;
        }
      });
    };
    const bindTimeChange = (e) => {
      addressParam.value.region = e.detail.value[0] + e.detail.value[1] + e.detail.value[2];
    };
    return (_ctx, _cache) => {
      return {
        a: addressParam.value.name,
        b: common_vendor.o(($event) => addressParam.value.name = $event.detail.value),
        c: addressParam.value.phone,
        d: common_vendor.o(($event) => addressParam.value.phone = $event.detail.value),
        e: addressParam.value.region,
        f: common_vendor.o(($event) => addressParam.value.region = $event.detail.value),
        g: common_vendor.o(bindTimeChange),
        h: common_vendor.o(($event) => chooseLocation()),
        i: addressParam.value.detailAddress,
        j: common_vendor.o(($event) => addressParam.value.detailAddress = $event.detail.value),
        k: common_vendor.o(defaultAddress),
        l: common_vendor.o(newAddress)
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-fb57d854"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/Shopping-cart/newaddress_Info/newaddress_Info.vue"]]);
wx.createPage(MiniProgramPage);
