"use strict";
const common_vendor = require("../../../common/vendor.js");
const sheep_index = require("../../../sheep/index.js");
const sheep_validate_form = require("../../../sheep/validate/form.js");
const sheep_api_system_area = require("../../../sheep/api/system/area.js");
const sheep_api_member_address = require("../../../sheep/api/member/address.js");
require("../../../sheep/helper/index.js");
require("../../../sheep/api/index.js");
require("../../../sheep/api/distri/score.js");
require("../../../sheep/request/index.js");
require("../../../sheep/config/index.js");
require("../../../sheep/store/index.js");
require("../../../sheep/store/app.js");
require("../../../sheep/api/promotion/diy.js");
require("../../../sheep/platform/index.js");
require("../../../sheep/platform/provider/wechat/index.js");
require("../../../sheep/platform/provider/wechat/miniProgram.js");
require("../../../sheep/api/member/auth.js");
require("../../../sheep/api/member/social.js");
require("../../../sheep/api/member/user.js");
require("../../../sheep/platform/provider/apple/index.js");
require("../../../sheep/platform/share.js");
require("../../../sheep/router/index.js");
require("../../../sheep/hooks/useModal.js");
require("../../../sheep/helper/test.js");
require("../../../sheep/api/member/signin.js");
require("../../../sheep/helper/throttle.js");
require("../../../sheep/url/index.js");
require("../../../sheep/platform/pay.js");
require("../../../sheep/api/pay/order.js");
require("../../../sheep/store/user.js");
require("../../../sheep/store/cart.js");
require("../../../sheep/api/trade/cart.js");
require("../../../sheep/api/pay/wallet.js");
require("../../../sheep/api/trade/order.js");
require("../../../sheep/api/promotion/coupon.js");
require("../../../sheep/store/sys.js");
require("../../../sheep/store/modal.js");
require("../../../sheep/api/distri/share.js");
require("../../../sheep/api/distri/team.js");
require("../../../sheep/api/infra/file.js");
require("../../../sheep/api/member/point.js");
require("../../../sheep/api/migration/app.js");
require("../../../sheep/api/migration/chat.js");
require("../../../sheep/api/migration/index.js");
require("../../../sheep/api/migration/third.js");
require("../../../sheep/api/pay/channel.js");
require("../../../sheep/api/product/category.js");
require("../../../sheep/api/product/comment.js");
require("../../../sheep/api/product/favorite.js");
require("../../../sheep/api/product/history.js");
require("../../../sheep/api/product/spu.js");
require("../../../sheep/api/promotion/activity.js");
require("../../../sheep/api/promotion/article.js");
require("../../../sheep/api/promotion/bargain.js");
require("../../../sheep/api/promotion/combination.js");
require("../../../sheep/api/promotion/rewardActivity.js");
require("../../../sheep/api/promotion/seckill.js");
require("../../../sheep/api/system/voice.js");
require("../../../sheep/api/trade/afterSale.js");
require("../../../sheep/api/trade/brokerage.js");
require("../../../sheep/api/trade/config.js");
require("../../../sheep/api/trade/delivery.js");
require("../../../sheep/config/zIndex.js");
require("../../../sheep/helper/digit.js");
if (!Array) {
  const _easycom_uni_easyinput2 = common_vendor.resolveComponent("uni-easyinput");
  const _easycom_uni_forms_item2 = common_vendor.resolveComponent("uni-forms-item");
  const _easycom_uni_icons2 = common_vendor.resolveComponent("uni-icons");
  const _easycom_su_switch2 = common_vendor.resolveComponent("su-switch");
  const _easycom_uni_forms2 = common_vendor.resolveComponent("uni-forms");
  const _easycom_su_fixed2 = common_vendor.resolveComponent("su-fixed");
  const _easycom_su_region_picker2 = common_vendor.resolveComponent("su-region-picker");
  const _easycom_s_layout2 = common_vendor.resolveComponent("s-layout");
  (_easycom_uni_easyinput2 + _easycom_uni_forms_item2 + _easycom_uni_icons2 + _easycom_su_switch2 + _easycom_uni_forms2 + _easycom_su_fixed2 + _easycom_su_region_picker2 + _easycom_s_layout2)();
}
const _easycom_uni_easyinput = () => "../../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.js";
const _easycom_uni_forms_item = () => "../../../uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.js";
const _easycom_uni_icons = () => "../../../uni_modules/uni-icons/components/uni-icons/uni-icons.js";
const _easycom_su_switch = () => "../../../sheep/ui/su-switch/su-switch.js";
const _easycom_uni_forms = () => "../../../uni_modules/uni-forms/components/uni-forms/uni-forms.js";
const _easycom_su_fixed = () => "../../../sheep/ui/su-fixed/su-fixed.js";
const _easycom_su_region_picker = () => "../../../sheep/ui/su-region-picker/su-region-picker.js";
const _easycom_s_layout = () => "../../../sheep/components/s-layout/s-layout.js";
if (!Math) {
  (_easycom_uni_easyinput + _easycom_uni_forms_item + _easycom_uni_icons + _easycom_su_switch + _easycom_uni_forms + _easycom_su_fixed + _easycom_su_region_picker + _easycom_s_layout)();
}
const _sfc_main = {
  __name: "edit",
  setup(__props) {
    const addressFormRef = common_vendor.ref(null);
    const state = common_vendor.reactive({
      showRegion: false,
      model: {
        name: "",
        mobile: "",
        detailAddress: "",
        defaultStatus: false,
        areaName: ""
      },
      rules: {}
    });
    const rules = {
      name: {
        rules: [{
          required: true,
          errorMessage: "请输入收货人姓名"
        }]
      },
      mobile: sheep_validate_form.mobile,
      detailAddress: {
        rules: [{
          required: true,
          errorMessage: "请输入详细地址"
        }]
      },
      areaName: {
        rules: [{
          required: true,
          errorMessage: "请选择您的位置"
        }]
      }
    };
    const onRegionConfirm = (e) => {
      state.model.areaName = `${e.province_name} ${e.city_name} ${e.district_name}`;
      state.model.areaId = e.district_id;
      state.showRegion = false;
    };
    const getAreaData = () => {
      if (common_vendor._.isEmpty(common_vendor.index.getStorageSync("areaData"))) {
        sheep_api_system_area.AreaApi.getAreaTree().then((res) => {
          if (res.code === 0) {
            common_vendor.index.setStorageSync("areaData", res.data);
          }
        });
      }
    };
    const onSave = async () => {
      console.log(state);
      const validate = await common_vendor.unref(addressFormRef).validate().catch((error) => {
        console.log("error: ", error);
      });
      if (!validate) {
        return;
      }
      const formData = {
        ...state.model
      };
      const {
        code
      } = state.model.id > 0 ? await sheep_api_member_address.AddressApi.updateAddress(formData) : await sheep_api_member_address.AddressApi.createAddress(formData);
      if (code === 0) {
        sheep_index.sheep.$router.back();
      }
    };
    const onDelete = () => {
      common_vendor.index.showModal({
        title: "提示",
        content: "确认删除此收货地址吗？",
        success: async function(res) {
          if (!res.confirm) {
            return;
          }
          const {
            code
          } = await sheep_api_member_address.AddressApi.deleteAddress(state.model.id);
          if (code === 0) {
            sheep_index.sheep.$router.back();
          }
        }
      });
    };
    common_vendor.onLoad(async (options) => {
      getAreaData();
      if (options.id) {
        let {
          code,
          data
        } = await sheep_api_member_address.AddressApi.getAddress(options.id);
        if (code !== 0) {
          return;
        }
        state.model = data;
      }
      if (options.data) {
        let data = JSON.parse(options.data);
        const areaData = common_vendor.index.getStorageSync("areaData");
        let provinceArr = areaData.filter((item) => item.name == data.province_name);
        data.province_id = provinceArr[0].id;
        let provinceArr2 = provinceArr[0].children.filter((item) => item.name == data.city_name);
        data.city_id = provinceArr2[0].id;
        let provinceArr3 = provinceArr2[0].children.filter((item) => item.name == data.district_name);
        data.district_id = provinceArr3[0].id;
        state.model = {
          name: data.consignee,
          mobile: data.mobile,
          detailAddress: data.address,
          defaultStatus: data.is_default,
          areaName: data.province_name + " " + data.city_name + " " + data.district_name,
          areaId: data.district_id
        };
      }
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.o(($event) => state.model.name = $event),
        b: common_vendor.p({
          placeholder: "请填写收货人姓名",
          inputBorder: false,
          placeholderStyle: "color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal",
          modelValue: state.model.name
        }),
        c: common_vendor.p({
          name: "name",
          label: "收货人"
        }),
        d: common_vendor.o(($event) => state.model.mobile = $event),
        e: common_vendor.p({
          type: "number",
          placeholder: "请输入手机号",
          inputBorder: false,
          placeholderStyle: "color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal",
          modelValue: state.model.mobile
        }),
        f: common_vendor.p({
          name: "mobile",
          label: "手机号"
        }),
        g: common_vendor.p({
          type: "right"
        }),
        h: common_vendor.o(($event) => state.model.areaName = $event),
        i: common_vendor.p({
          disabled: true,
          inputBorder: false,
          styles: {
            disableColor: "#fff",
            color: "#333"
          },
          placeholderStyle: "color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal",
          placeholder: "请选择省市区",
          modelValue: state.model.areaName
        }),
        j: common_vendor.o(($event) => state.showRegion = true),
        k: common_vendor.p({
          name: "areaName",
          label: "省市区"
        }),
        l: common_vendor.o(($event) => state.model.detailAddress = $event),
        m: common_vendor.p({
          inputBorder: false,
          type: "textarea",
          placeholderStyle: "color:#BBBBBB;font-size:30rpx;font-weight:400;line-height:normal",
          placeholder: "请输入详细地址",
          clearable: true,
          modelValue: state.model.detailAddress
        }),
        n: common_vendor.p({
          name: "detailAddress",
          label: "详细地址",
          formItemStyle: {
            alignItems: "flex-start"
          },
          labelStyle: {
            lineHeight: "5em"
          }
        }),
        o: common_vendor.o(($event) => state.model.defaultStatus = $event),
        p: common_vendor.p({
          modelValue: state.model.defaultStatus
        }),
        q: common_vendor.sr(addressFormRef, "e04c42e1-1,e04c42e1-0", {
          "k": "addressFormRef"
        }),
        r: common_vendor.o(($event) => state.model = $event),
        s: common_vendor.p({
          rules,
          validateTrigger: "bind",
          labelWidth: "160",
          labelAlign: "left",
          border: true,
          labelStyle: {
            fontWeight: "bold"
          },
          modelValue: state.model
        }),
        t: common_vendor.o(onSave),
        v: state.model.id
      }, state.model.id ? {
        w: common_vendor.o(onDelete)
      } : {}, {
        x: common_vendor.p({
          bottom: true,
          opacity: false,
          bg: "",
          placeholder: true,
          noFixed: false,
          index: 10
        }),
        y: common_vendor.o(($event) => state.showRegion = false),
        z: common_vendor.o(onRegionConfirm),
        A: common_vendor.p({
          show: state.showRegion
        }),
        B: common_vendor.p({
          title: state.model.id ? "编辑地址" : "新增地址"
        })
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e04c42e1"], ["__file", "D:/zx/mall-front-app/pages/user/address/edit.vue"]]);
wx.createPage(MiniProgramPage);
