/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-06 20:53:23
 * @LastEditTime: 2019-08-19 16:54:13
 * @LastEditors: Please set LastEditors
 */
// 商品选择器方法

export default {
  data() {
    return {
      // ----线下适用商品开始----
      xxSysp: {
        visible: false, // 适用商品选择器dialog
        goodsEnable: false,
        filterAbbrInfo: [], // 选择后回显的label数据
        goodsFilterId: null,
        conditionList: null, // 商品选择器的回显值
        conditionList_copy: [], // 没有只读功能，备份数据
        isResign: false // 编辑后赋值
      }
      // ----线下适用商品结束----
    };
  },
  methods: {
    // ------线下适用商品开始-------
    // 清空线下适用商品 ，重置商品选择器
    handleXxSyspReset() {
      this.$refs.selector.conditions[0].goodsList = [];
      this.$refs.selector.complexData = [];
      this.xxSysp.filterAbbrInfo = [];
      this.xxSysp.conditionList = null;
      this.xxSysp.goodsFilterId = null;
    },
    // 编辑查看btn点击
    handleXxSyspClick(visible = true) {
      this.xxSysp.visible = visible;
      // refs.selector放在modal里，初始化的时候并没有渲染
      if (!this.xxSysp.isResign && (this.isEdit || this.isInfo)) {
        this.$nextTick(() => {
          this.$refs.selector.conditions = this.xxSysp.conditionList;
          this.xxSysp.isResign = true;
        });
      }
      // if (this.xxSysp.filterAbbrInfo.length !== 0) {
      //   this.$refs.selector.changeComplexInfo(); // 强制展开
      // }
    },
    // 商品选择器反显label
    xxSyspLabel(filterAbbrInfo) {
      let _filterAbbrInfo = [];
      // 设置显示label
      filterAbbrInfo &&
        filterAbbrInfo.forEach(y => {
          _filterAbbrInfo = y.map(v => {
            return {
              optName: v.optName,
              text: `${v.typeName}${v.belong}${v.childNames
                .map(v => {
                  return `【${v}】`;
                })
                .join('')}`
            };
          });
        });
      return _filterAbbrInfo;
    },
    // 商品选择器确认
    xxSyspConfirm() {
      this.$refs.selector
        .collectConditions()
        .then(res => {
          if (res == false) {
            throw new Error('商品选择器条件不能为空！');
          } else {
            this.xxSysp.goodsFilterId = res.id; // 保存id
            this.xxSysp.conditionList = res.conditionList; // 保存回显list
            this.xxSysp.filterAbbrInfo = this.xxSyspLabel(res.filterAbbrInfo);
            this.handleXxSyspClick(false); // 强制展开
            this.xxSysp.visible = false;
          }
        })
        .catch(err => {
          this.$tips({ message: err.message, type: 'error' });
        });
    }
    // ------线下适用商品开始-------
  }
};
