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

  /**
   * 组件的初始数据
   */
  data: {
    isShowPopup: false,
    popTitle:'',

    show_levels_CheckedAll: false,
    buy_levels_CheckedAll: false,
    show_groups_CheckedAll: false,
    buy_groups_CheckedAll: false,

    show1: false,
    show2: false,
    show3: false,
    show4: false,

    buy_levels: [],
    show_levels: [],
    show_groups: [],
    buy_groups: [],

    once_buy_limit: '',
    total_buy_limit: '',
    day_buy_limit: '',
    week_buy_limit: '',
    month_buy_limit: '',

    hyll_Model: '',
    hygm_Model: '',
    hyzll_Model: '',
    hyzgm_Model: '',


  },
  lifetimes: {
    attached() {
      this.getDataInfo();
    }
  },


  /**
   * 组件的方法列表
   */
  methods: {

    changeType(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        [name]: evt.detail
      });
    },
    getDataInfo(){
      if (this.data.widgetsInfo && this.data.widgetsInfo.privilege) {
        let {
          show_levels, show_groups, buy_levels, buy_groups, once_buy_limit, total_buy_limit,
          day_buy_limit, week_buy_limit, month_buy_limit
        } = this.data.widgetsInfo.privilege;
        let changeData={};
        changeData.show_levels = show_levels ? show_levels.map(item=>item) : [];
        changeData.show_groups = show_groups ? show_groups.map(item=>item) : [];
        changeData.buy_levels = buy_levels ? buy_levels.map(item=>item) : [];
        changeData.buy_groups = buy_groups ? buy_groups.map(item=>item) : [];
        changeData.once_buy_limit = String(once_buy_limit);
        changeData.total_buy_limit = String(total_buy_limit);
        changeData.day_buy_limit = String(day_buy_limit);
        changeData.week_buy_limit = String(week_buy_limit);
        changeData.month_buy_limit = String(month_buy_limit);
        this.setData(changeData);
      }
    },
    validatorData() {
      let json = {};
      json = {
        show_levels: this.data.show_levels,
        buy_levels: this.data.buy_levels,
        show_groups: this.data.show_groups,
        buy_groups: this.data.buy_groups,
        once_buy_limit: this.data.once_buy_limit,
        total_buy_limit: this.data.total_buy_limit,
        day_buy_limit: this.data.day_buy_limit,
        week_buy_limit: this.data.week_buy_limit,
        month_buy_limit: this.data.month_buy_limit
      };
      return json;
    },
    setShowPopup(evt) {
      let index = evt.currentTarget.dataset.index;
      this.setData({
        isShowPopup: true,
        show1: false,
        show2: false,
        show3: false,
        show4: false,
      });
      switch (index) {
      case 1:
        this.setData({
          popTitle:'会员等级浏览权限',
          show1:true
        });
        break;
      case 2:
        this.setData({
          popTitle:'会员等级购买权限',
          show2:true
        });
        break;
      case 3:
        this.setData({
          popTitle:'会员组浏览权限',
          show3:true
        });
        break;
      case 4:
        this.setData({
          popTitle:'会员组购买权限',
          show4:true
        });
        break;
      default:
        break;
      }
    },
    cancelShowPop(){
      this.setData({
        isShowPopup:false
      });
    }
  },
  observers: {
    show_levels(newList){
      let arr = this.properties.levelList.filter((v)=>{
        return newList.indexOf(v.id+"") != -1;
      });
      let arr2 = arr.map(function(user) {
        return user.level_name;
      });
      this.setData({
        hyll_Model:arr2.join(',')
      });
    },
    buy_levels(newList){
      let arr = this.properties.levelList.filter((v)=>{
        return newList.indexOf(v.id+"") != -1;
      });
      let arr2 = arr.map(function(user) {
        return user.level_name;
      });
      this.setData({
        hygm_Model:arr2.join(',')
      });
    },
    show_groups(newList){
      let arr = this.properties.groupsList.filter((v)=>{
        return newList.indexOf(v.id+"") != -1;
      });
      let arr2 = arr.map(function(user) {
        return user.group_name;
      });
      this.setData({
        hyzll_Model:arr2.join(',')
      });
    },
    buy_groups(newList){
      let arr = this.properties.groupsList.filter((v)=>{
        return newList.indexOf(v.id+"") != -1;
      });
      let arr2 = arr.map(function(user) {
        return user.group_name;
      });
      this.setData({
        hyzgm_Model:arr2.join(',')
      });
    },
    show_levels_CheckedAll(newVal) {
      if (newVal) {
        let levels = this.properties.levelList.map((item) => {
          return item.id + "";
        });
        this.setData({
          show_levels: levels
        });
      } else {
        this.setData({
          show_levels: []
        });
      }
    },
    buy_levels_CheckedAll(newVal){
      if (newVal) {
        let levels = this.properties.levelList.map((item) => {
          return item.id + "";
        });
        this.setData({
          buy_levels: levels
        });
      } else {
        this.setData({
          buy_levels: []
        });
      }
    },
    show_groups_CheckedAll(newVal){
      if (newVal) {
        let levels = this.properties.groupsList.map((item) => {
          return item.id + "";
        });
        this.setData({
          show_groups: levels
        });
      } else {
        this.setData({
          show_groups: []
        });
      }
    },
    buy_groups_CheckedAll(newVal){
      if (newVal) {
        let levels = this.properties.groupsList.map((item) => {
          return item.id + "";
        });
        this.setData({
          buy_groups: levels
        });
      } else {
        this.setData({
          buy_groups: []
        });
      }
    }
  }
});