"use strict";
const common_vendor = require("../../../common/vendor.js");
const common_js_net_shop = require("../../../common/js/net/shop.js");
const common_js_net_mainUrl = require("../../../common/js/net/mainUrl.js");
const common_js_conver = require("../../../common/js/conver.js");
const _sfc_main = {
  setup() {
  },
  data() {
    return {
      animationClass: "",
      totalHeight: 50,
      statusBarHeight: 24,
      id: "",
      msgType: "info",
      msgHint: "",
      msgShow: false,
      goods: null,
      isagree: false
    };
  },
  onLoad(options) {
    const id = options.id;
    if (id != "") {
      this.id = id;
      this.doGetDetail();
    } else {
      this.setData({
        msgType: "error",
        msgHint: "数据有误,请刷新重试",
        msgShow: true
      });
      this.msgType = "error";
      this.msgHint = "数据有误,请刷新重试";
      this.$refs.msg.open();
      setTimeout(() => {
        common_vendor.index.navigateBack();
      }, 1200);
    }
    this.countBarHeight();
  },
  onShow() {
    this.animationClass = "fade-in";
  },
  onHide() {
    this.animationClass = "fade-out";
  },
  methods: {
    moneyTxt: common_js_conver.moneyTxt,
    //计算状态栏等高度
    countBarHeight() {
      const deviceInfo = common_vendor.index.getSystemInfoSync();
      common_vendor.index.__f__("log", "at pages/shop/goodsDetail/goodsDetail.vue:100", "设备信息", deviceInfo);
      this.statusBarHeight = deviceInfo.statusBarHeight;
      this.totalHeight = deviceInfo.osName.toLowerCase() == "ios" ? 48 : 50;
      common_vendor.index.__f__("log", "at pages/shop/goodsDetail/goodsDetail.vue:103", "系统平台", deviceInfo.osName);
    },
    doBack() {
      common_vendor.index.navigateBack();
    },
    doPreImg(e) {
      const url = e.currentTarget.dataset.url;
      common_vendor.index.previewImage({
        urls: [url]
      });
    },
    doChangeDeal() {
      this.isagree = !this.isagree;
    },
    //显示购买协议
    doShowDeal() {
      common_vendor.index.navigateTo({
        url: "/pages/common/rule/rule?id=db805bf2-eef9-47d5-941b-cdc3b83ec316&needToken=true"
      });
    },
    //提交购买
    doConfirm() {
      if (this.isagree) {
        this.doSaveOrder();
      } else {
        this.msgHint = "请阅读并同意购买协议";
        this.msgType = "error";
        this.$refs.msg.open();
      }
    },
    async doSaveOrder() {
      try {
        common_vendor.index.showLoading({
          title: "购买中.."
        });
        const res = await common_js_net_shop.Shop.doSaveOrder(this.id);
        common_vendor.index.hideLoading();
        if (res && res.data != "") {
          await this.doConfirmOrder(res.data);
        } else {
          this.msgHint = "网络错误,请稍后重试";
          this.msgType = "error";
          this.$refs.msg.open();
        }
      } catch (err) {
        common_vendor.index.hideLoading();
        this.msgHint = err.msg ? err.msg : "网络错误,请稍后重试";
        this.msgType = "error";
        this.$refs.msg.open();
      }
    },
    async doConfirmOrder(id) {
      try {
        common_vendor.index.showLoading({
          title: "购买中..."
        });
        await common_js_net_shop.Shop.doConfirmOrder(id);
        common_vendor.index.hideLoading();
        this.msgHint = "购买成功";
        this.msgType = "success";
        this.$refs.msg.open();
        setTimeout(() => {
          this.backPageRefresh();
        }, 1500);
      } catch (err) {
        common_vendor.index.hideLoading();
        this.msgHint = err.msg ? err.msg : "网络错误,请稍后重试";
        this.msgType = "error";
        this.$refs.msg.open();
        this.upPagerefresh();
      }
    },
    doGetDetail() {
      const that = this;
      common_vendor.index.showLoading({
        title: "加载中..."
      });
      common_js_net_shop.Shop.doGetGoodsDetail(that.id).then((res) => {
        common_vendor.index.hideLoading();
        common_vendor.index.__f__("log", "at pages/shop/goodsDetail/goodsDetail.vue:188", res);
        if (res.goodsPhoto && res.goodsPhoto != "") {
          res.goodsPhoto = common_js_net_mainUrl.sImgPrefix + res.goodsPhoto;
          this.goods = res;
        } else {
          that.msgHint = "网络错误,请稍后重试";
          that.msgType = "error";
          that.$refs.msg.open();
          setTimeout(() => {
            common_vendor.index.navigateBack();
          }, 1200);
        }
      }).catch((err) => {
        common_vendor.index.hideLoading();
        that.msgHint = err.msg ? err.msg : "网络错误,请稍后重试";
        that.msgType = "error";
        that.$refs.msg.open();
        setTimeout(() => {
          common_vendor.index.navigateBack();
        }, 1200);
      });
    },
    backPageRefresh: function() {
      var pages = getCurrentPages();
      var beforePage = pages[pages.length - 2];
      beforePage.$vm.needRefresh = true;
      common_vendor.index.navigateBack();
    },
    upPagerefresh() {
      var pages = getCurrentPages();
      var beforePage = pages[pages.length - 2];
      beforePage.$vm.needRefresh = true;
    }
  }
};
if (!Array) {
  const _easycom_uni_popup_message2 = common_vendor.resolveComponent("uni-popup-message");
  const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
  (_easycom_uni_popup_message2 + _easycom_uni_popup2)();
}
const _easycom_uni_popup_message = () => "../../../uni_modules/uni-popup/components/uni-popup-message/uni-popup-message.js";
const _easycom_uni_popup = () => "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
if (!Math) {
  (_easycom_uni_popup_message + _easycom_uni_popup)();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_vendor.o((...args) => $options.doBack && $options.doBack(...args)),
    b: $data.totalHeight + "px",
    c: $data.statusBarHeight + "px",
    d: $data.goods != null
  }, $data.goods != null ? {
    e: common_vendor.o((...args) => $options.doPreImg && $options.doPreImg(...args)),
    f: $data.goods.goodsPhoto,
    g: $data.goods.goodsPhoto,
    h: common_vendor.t($data.goods.goodsName),
    i: common_vendor.t($data.goods.goodsDescription),
    j: $options.moneyTxt(12, 16, $data.goods.goodsOpenPrice),
    k: common_vendor.t($data.goods.goodsStatusTime),
    l: $data.isagree,
    m: common_vendor.o((...args) => $options.doChangeDeal && $options.doChangeDeal(...args)),
    n: common_vendor.o((...args) => $options.doShowDeal && $options.doShowDeal(...args)),
    o: common_vendor.n($data.isagree ? "" : "active"),
    p: common_vendor.o((...args) => $options.doConfirm && $options.doConfirm(...args))
  } : {}, {
    q: common_vendor.p({
      isTop: false,
      duration: 2e3,
      type: $data.msgType,
      message: $data.msgHint
    }),
    r: common_vendor.sr("msg", "eee34b24-0"),
    s: common_vendor.p({
      type: "message"
    }),
    t: common_vendor.n($data.animationClass)
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-eee34b24"]]);
swan.createPage(MiniProgramPage);
//# sourceMappingURL=../../../../.sourcemap/mp-baidu/pages/shop/goodsDetail/goodsDetail.js.map
