var app = getApp();
Component({
  properties: {
    DataInfo: {
      type: null
    },
    isType: {
      type: null
    }
  },
  data: {
    show_levels_CheckedAll: false,
    buy_levels_CheckedAll: false,
    show_groups_CheckedAll: false,
    buy_groups_CheckedAll: false,

    // isType: false,
    show1: false,
    show2: false,
    show3: false,
    show4: false,

    levelsList: [], // 会员等级,
    buy_levels: [],
    show_levels: [],
    groupsList: [], // 会员分组,
    show_groups: [],
    buy_groups: [],

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

    hyll_Model: [],
    hygm_Model: [],
    hyzll_Model: [],
    hyzgm_Model: []
  }, // 私有数据，可用于模板渲染

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

  // 生命周期函数，可以为函数，或一个在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,
              groupsList: res.data.member_group
            });
            if (!this.properties.isType) {
              let data = this.properties.DataInfo.privilege;
              console.log(data, '有值？');

              if (app._isTextEmpty(data)) {
                console.log('刚刚开启');
              } else {
                this.setData({
                  hyll_Model: data.show_levels,
                  hygm_Model: data.buy_levels,
                  show_levels: data.show_levels,
                  buy_levels: data.buy_levels
                });
                this.setData({
                  hyzll_Model: data.show_groups,
                  hyzgm_Model: data.buy_groups,
                  show_groups: data.show_groups,
                  buy_groups: data.buy_groups
                });
                this.setData({
                  once_buy_limit: String(data.once_buy_limit),
                  total_buy_limit: String(data.total_buy_limit),
                  day_buy_limit: String(data.day_buy_limit),
                  week_buy_limit: String(data.week_buy_limit),
                  month_buy_limit: String(data.month_buy_limit)
                });
              }
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg || '错误',
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    },
    // 弹窗相关------
    change(value) {
      this.setData({
        show_levels: value.detail
      });
      if (this.data.show_levels.length == this.data.levelsList.length) {
        this.setData({
          show_levels_CheckedAll: true
        });
      } else {
        this.setData({
          show_levels_CheckedAll: false
        });
      }
    },
    listCheckAllChange(value) {
      this.setData({
        buy_levels: value.detail
      });
      if (this.data.buy_levels.length == this.data.levelsList.length) {
        this.setData({
          buy_levels_CheckedAll: true
        });
      } else {
        this.setData({
          buy_levels_CheckedAll: false
        });
      }
    },
    showGroupsChange(value) {
      this.setData({
        show_groups: value.detail
      });
      if (this.data.show_groups.length == this.data.groupsList.length) {
        this.setData({
          show_groups_CheckedAll: true
        });
      } else {
        this.setData({
          show_groups_CheckedAll: false
        });
      }
    },
    buyGroupsChange(value) {
      this.setData({
        buy_groups: value.detail
      });
      if (this.data.buy_groups.length == this.data.groupsList.length) {
        this.setData({
          buy_groups_CheckedAll: true
        });
      } else {
        this.setData({
          buy_groups_CheckedAll: false
        });
      }
    },
    // 全选 --------------------
    checkAll(val) {
      this.setData({
        show_levels_CheckedAll: val.detail
      });
      if (this.data.show_levels_CheckedAll) {
        let datas = this.data.levelsList;
        let show_levels = [];
        datas.forEach(item => {
          show_levels.push(item.level_name);
        });
        this.setData({
          show_levels
        });
      } else {
        this.setData({
          show_levels: []
        });
      }
    },
    checkAllBuyLevels(val) {
      this.setData({
        buy_levels_CheckedAll: val.detail
      });
      if (this.data.buy_levels_CheckedAll) {
        let data = this.data.levelsList;
        let buy_levels = [];
        data.forEach(item => {
          buy_levels.push(item.level_name);
        });
        this.setData({
          buy_levels
        });
      } else {
        this.setData({
          buy_levels: []
        });
      }
    },
    checkAllShowGroups(val) {
      this.setData({
        show_groups_CheckedAll: val.detail
      });
      if (this.data.show_groups_CheckedAll) {
        let arrs = this.data.groupsList;
        let show_groups = [];
        arrs.forEach(item => {
          show_groups.push(item.group_name);
        });
        this.setData({
          show_groups
        });
      } else {
        this.setData({
          show_groups: []
        });
      }
    },
    checkAllBuyGroups(val) {
      this.setData({
        buy_groups_CheckedAll: val.detail
      });
      if (this.data.buy_groups_CheckedAll) {
        let arrss = this.data.groupsList;
        let buy_groups = [];
        arrss.forEach(item => {
          buy_groups.push(item.group_name);
        });
        this.setData({
          buy_groups
        });
      } else {
        this.setData({
          buy_groups: []
        });
      }
    },
    PermissionsJson() {
      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
      };
      this.triggerEvent('getChildtwoData', json, { bubbles: false });
      // }
    },
    showPopup(e) {
      let index = parseInt(e.target.dataset.index || e.currentTarget.dataset.index);
      this.triggerEvent('CoverLayer', true, { bubbles: false });
      switch (index) {
      case 1:
        this.setData({
          show1: true
        });
        if (!this.data.isType) {
          this.setData({
            show_levels: this.data.hyll_Model
          });
          if (this.data.show_levels.length == this.data.levelsList.length) {
            this.setData({
              show_levels_CheckedAll: true
            });
          } else {
            this.setData({
              show_levels_CheckedAll: false
            });
          }
        }
        break;
      case 2:
        this.setData({
          show2: true
        });
        if (!this.data.isType) {
          this.setData({
            buy_levels: this.data.hygm_Model
          });
          if (this.data.buy_levels.length == this.data.levelsList.length) {
            this.setData({
              buy_levels_CheckedAll: true
            });
          } else {
            this.setData({
              buy_levels_CheckedAll: false
            });
          }
        }
        break;
      case 3:
        this.setData({
          show3: true
        });
        if (!this.data.isType) {
          this.setData({
            show_groups: this.data.hyzll_Model
          });
          if (this.data.show_groups.length == this.data.groupsList.length) {
            this.setData({
              show_groups_CheckedAll: true
            });
          } else {
            this.setData({
              show_groups_CheckedAll: false
            });
          }
        }
        break;
      case 4:
        this.setData({
          show4: true
        });
        if (!this.data.isType) {
          this.setData({
            buy_groups: this.data.hyzgm_Model
          });
          if (this.data.buy_groups.length == this.data.groupsList.length) {
            this.setData({
              buy_groups_CheckedAll: true
            });
          } else {
            this.setData({
              buy_groups_CheckedAll: false
            });
          }
        }
        break;
      default:
      {
        this.setData({
          show1: false,
          show2: false,
          show3: false,
          show4: false
        });
        let hyll_Model = this.data.hyll_Model;
        let hygm_Model = this.data.hygm_Model;
        let hyzll_Model = this.data.hyzll_Model;
        let hyzgm_Model = this.data.hyzgm_Model;
        hygm_Model = this.data.buy_levels;
        hyll_Model = this.data.show_levels;
        hyzll_Model = this.data.show_groups;
        hyzgm_Model = this.data.buy_groups;
        this.setData({
          hyll_Model,
          hygm_Model,
          hyzll_Model,
          hyzgm_Model
        });
        this.triggerEvent('CoverLayer', false, { bubbles: false });
        break;
      }
      }
    },
    isCoverLayer() {
      this.setData({
        show1: false,
        show2: false,
        show3: false,
        show4: false
      });
      this.triggerEvent('CoverLayer', false, { bubbles: false });
    },
    // 输入框改变
    onChangenum(val) {
      this.setData({
        once_buy_limit: val.detail.value
      });
    },
    onChangetoaut(val) {
      this.setData({
        total_buy_limit: val.detail.value
      });
    },
    onChangebuy(val) {
      this.setData({
        day_buy_limit: val.detail.value
      });
    },
    onChangeweek(val) {
      this.setData({
        week_buy_limit: val.detail.value
      });
    },
    onChangemonth(val) {
      this.setData({
        month_buy_limit: val.detail.value
      });
    }
  }
});
