var app = getApp();
Component({
  properties: {
    DataInfo: {
      type: null
    },
    isType: {
      type: Boolean
    }
  },
  data: {
    levelsList: [],
    discount_value: {},
    level_discount_type: true,
    discount_method: '1'
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    ready() {
      if (this.properties.isType) {
        this.getDataInfo();
      }
    },
    moved() {},
    detached() {}
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {},

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {}
  },

  methods: {
    getDataInfo() {
      // 获取会员等级
      let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-store-goods-setting');
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              levelsList: res.data.member_level
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
      if (this.properties.isType) {
        // 发布
      }else {
        // 编辑
        // 判断传递过来数据不为空的时候
        if (app._isTextEmpty(this.data.DataInfo)) {
          console.log('刚刚开启');
        } else {
          let level_discount_type = Boolean(this.data.DataInfo.discount.level_discount_type);
          try{
            var discount_method = this.data.DataInfo.discount.discount_method.toString();
          }catch(err){console.log(err);}
          let discount_value = this.data.DataInfo.discount.discount_value;
          this.setData({
            discount_method,
            level_discount_type,
            discount_value});
        }
      }
    },
    DiscountJson() {
      let json = {};
      json = {
        level_discount_type: this.data.level_discount_type ? 1 : 0,
        discount_method: this.data.discount_method == '1' ? 1 : 2,
        discount_value: this.data.discount_value
      };
      this.triggerEvent('getChildthreeData', json, { bubbles: false });
      this.triggerEvent('getSignthree', this.data.levelsList.length, {bubbles: false});
    },
    onChange(val) {
      let id = val.target.dataset.id || val.currentTarget.dataset.id;
      let discount_value = this.data.discount_value;
      discount_value[id] = val.detail.value;
      this.setData({
        discount_value});
    },
    onChangeRadio(val) {
      this.setData({
        discount_method: val.detail
      });
    },
    checkAll(val) {
      this.setData({
        level_discount_type: val.detail
      });
    }

  }
});
