// packageF/storeManagement/components/discount/discount.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    levelList: {
      type: [String, Array, Object],
      value: []
    },
    widgetsInfo: {
      type: [String, Number, Object],
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    level_discount_type:true,
    discount_method:1
  },
  lifetimes:{
    attached(){
      this.getDataInfo();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    changeType(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        [name]: evt.detail
      });
    },
    changeText(evt){
      let index = evt.currentTarget.dataset.index;
      let value = evt.detail;
      this.data.levelList[index].discount_value=value;
    },
    // 过滤数据
    filertMap (list=[]) {
      let arr = list.map(item => {
        return {level_id: item.id, discount_value: item.discount_value};
      });
      return arr;
    },
    validatorData() {
      let json = {};
      json = {
        level_discount_type: this.data.level_discount_type ? 1 : 0,
        discount_method: this.data.discount_method,
        discount_value: this.filertMap(this.data.levelList)
      };
      return json;
    },
    getDataInfo() {
      if (this.data.widgetsInfo && this.data.widgetsInfo.discount) {
        let {level_discount_type, discount_method} = this.data.widgetsInfo.discount;
        let changeData = {};
        changeData.level_discount_type = Boolean(level_discount_type);
        changeData.discount_method = discount_method;
        this.setData(changeData);
      }
    },
  }
});
