import * as API from 'api/virtual/flow_pools';

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    month: {
      type: String,
      default: false
    },
    pool_id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      my_show: false,
      loading: false,
      flowPool: {},
      data: {
        pool_name: '',
        total: 0,
        settings: []
      },
      params: {
        pool_id: '',
        month: '',
        total_flows: 0,
        settings: []
      }
    };
  },
  watch: {
    show(bool) {
      this.my_show = bool;
      if (bool) {
        let month = this.month;
        this.params.month = this.moment(month).format('YYYY-MM');
        this.index();
      }
    }
  },
  methods: {
    index() {
      this.params.settings = [];
      this.params.pool_id = this.pool_id;
      this.params.month = this.moment(this.params.month).format('YYYY-MM');

      let params = {
        pool_id: this.pool_id,
        month: this.params.month
      };

      this.loading = true;

      API.getFlows(params).then(res => {
        if (res.code === 0) {
          this.flowPool = res.data.flowPool;
          this.params.total_flows = Number(res.data.total_flows);

          this.data = {
            pool_name: res.data.flowPool.name,
            total: res.data.total,
            settings: res.data.settings
          };

          let cards = [];

          this.data.settings.map(item => {
            if (!item.cards) {
              cards = [{
                counts: item.total,
                flow_range: [0, 0]
              }];
            } else {
              item.cards.map(value => {
                value.flow_range[0] = Number(value.flow_range[0]);
                value.flow_range[1] = Number(value.flow_range[1]);
              });
              cards = item.cards;
            }

            let obj = {
              package_id: item.package_id,
              package_name: item.package_name,
              total: item.total,
              news: item.news,
              cards: cards
            };

            this.params.settings.push(obj);
          });
        }
        this.loading = false;
      });
    },
    ok() {
      this.params.month = this.moment(this.params.month).format('YYYY-MM');

      for (const objIndex in this.params.settings) {
        let obj = this.params.settings[objIndex];

        let total = 0;
        for (const index in obj.cards) {
          total += obj.cards[index].counts;
        }

        if (total !== obj.total) {
          this.$Message.error(`套餐(${obj.package_name})卡数量设置不正确`);
          return;
        }
      }

      // 添加
      API.postFlows(this.params).then(res => {
        this.loading = false;
        if (res.code == 0) {
          this.$emit('add-success');
          this.$Message.success('设置成功');
          this.clear();
        }
      }).catch(err => {
        this.loading = false;
      });
    },

    visibleChange(bool) {
      if (!bool) {
        this.$emit('update:show', false);
      }
    },
    clear() {
      for (let k in this.params) {
        if (k == 'month') {
          this.params[k] = this.moment().subtract('1', 'months').format('YYYY-MM');
        } else if (k == 'total_flows') {
          this.params[k] = 0;
        } else {
          this.params[k] = '';
        }
      }

      this.params.settings = [];

      this.my_show = false;
    },
    handleAdd(objIndex, index) {
      this.params.settings[objIndex].cards.splice(index + 1, 0, {
        counts: 0,
        flow_range: [0, 0]
      });
    },
    handleRemove(objIndex, index) {
      this.params.settings[objIndex].cards.splice(index, 1);
    }
  }
};
