// packageA/mycomponent/goodsComponent/plugin/groupWork/groupWork.js
var yz_pay = require("../../../../../mycomponent/yz_pay/yz_pay");
const app = getApp();
Component({
  behaviors: [yz_pay],
  /**
   * 组件的属性列表
   */
  properties: {
    gooddatas: {
      type: Object,
    }
  },
  observers: {},
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.initDisabled();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    goods: {},
    showSpecs: false,
    groupLevel: [],
    groupModel: null,
    groupWork_disabled: false, //当前发起拼团按钮是否禁用

    Payshow: false,
    btnData: [], //支付方式集合,
    payKeyboardShow: false, //键盘显示
    isPlaying: false //防止重复支付
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initDisabled() {
      //是否禁用，活动未开始、已结束
      let status = false;
      if (!this.data.gooddatas.end_time || this.data.gooddatas.end_time == "") {
        //不限时
        status = false;
      } else if (app.getTimeDifference(this.data.gooddatas.end_time) == 0) {
        //已结束
        status = true;
      } else if (app.getTimeDifference(this.data.gooddatas.start_time) > 0) {
        //未开始
        status = true;
      }
      this.setData({
        groupWork_disabled: status,
        showSpecs: false,
        Payshow: false,
        payKeyboardShow: false,
        isPlaying: false
      });
    },
    selectGroup(e) {
      const {
        item
      } = e.currentTarget.dataset;
      if (item.status != 2) {
        let text = item.status == 3 ? "已结束" : "未开始";
        wx.showToast({
          title: text,
          duration: 1000,
          icon: "none",
        });
        return;
      }
      this.setData({
        groupModel: item.id,
      });
      this.selectComponent('#yzSpecs').setData({
        popPrice: item.min_price,
        popDescription: `${item.person}人团`
      });
    },
    rewardPayCancelBtn() {
      this.setData({
        Payshow: false
      });
    },
    showSpecsPop() {
      if (Number(this.data.gooddatas.surplus) === 0) {
        //没次数
        wx.showToast({
          title: '开团次数已用完，请升级等级',
          duration: 1000,
          icon: "none",
        });
        return;
      }
      let goods = {};
      goods['id'] = this.data.gooddatas.goods_id;
      goods['thumb'] = this.data.gooddatas.goods_info.thumb;
      goods['stock'] = this.data.gooddatas.stock;
      goods['price'] = this.data.gooddatas.min_price + ' 起';
      this.setData({
        goods: goods,
        showSpecs: true
      });
    },
    //获取支付类型参数
    getPayData() {
      if (!this.data.groupModel) {
        wx.showToast({
          title: '请选择开团层级！',
          duration: 1000,
          icon: "none",
        });
        return;
      }
      app._postNetWork({
        url: app.getNetAddresss("plugin.group-work.frontend.modules.alliance.controllers.pay.button"),
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              btnData: res.data.buttons,
              showSpecs: false,
              Payshow: true
            });
          } else {
            wx.showToast({
              title: res.msg,
              duration: 1000,
              icon: "none",
            });
          }
        },
      });
    },
    async payPost(e) {
      if (this.data.isPlaying) {
        return;
      }
      let _data = e.currentTarget.dataset.info;
      let json = {
        pay_type: _data.value,
        spec_id: this.data.groupModel,
        alliance_id: 0,
        client_type: 2
      };

      if (_data.need_password) {
        let pass = await this.getPayKeyboardPassword(); //开启支付密码验证
        json.password = pass;
      }
      this.data.isPlaying = true;
      app._getNetWork({
        url: app.getNetAddresss('plugin.group-work.frontend.modules.alliance.controllers.apply.index'),
        data: json,
        success: (res) => {
          this.data.isPlaying = false;
          if (res.data.result === 1) {
            this.handleOtherPay(_data, _data.value, res.data, "groupWork");
            this.setData({
              Payshow: false,
              popupSpecs: false,
              showSpecs: false
            });
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg,
              success(res) {}
            });
            console.error(res);
          }
        },
        fail: (res) => {
          console.error(res);
        }
      });
    },
  }
});