import { sumBy } from 'service/util';
export default {
  name: 'StatsCompanyCount',
  data() {
    return {
      search: {
        show: true
      },
      options: {
        name: '',
        time: [
          this.moment().subtract('1', 'months').startOf('month').format('YYYY-MM-DD'),
          this.moment().subtract('1', 'months').endOf('month').format('YYYY-MM-DD')
        ]
      },
      data: [],
      list: [],
      stats: {},
      page: {
        total: 0,
        limit: 10,
        page: 1
      },
      columns: [
        {
          title: '企业ID',
          key: 'id',
          width: 80
        },
        {
          title: '企业名称',
          key: 'name'
        },
        {
          title: '总用户数',
          key: 'total',
          width: 120
        },
        {
          title: '新增用户数',
          key: 'counts',
          width: 120
        },
        {
          title: '续费用户数',
          key: 'renewed_counts',
          width: 120
        }
      ]
    };
  },
  created() {
    this.index();
  },
  methods: {
    /**
     * [index 列表]
     * @param  {Number} page [description]
     * @return {[type]}      [description]
     */
    index() {
      let options = Object.assign({
        orderBy: 'id',
        sortedBy: 'asc'
      },
      this.options);

      let params = this.searchDataHandle({}, {
        limit: 0
      }, options);

      this.isShowLoading(true);

      service.get('api/stats/company-count', {
        params
      }).then(res => {
        this.isShowLoading(false);
        if (res.code == 0) {
          this.list = res.data;
          this.page.total = this.list.length;
          this.changePage(1);
        }
      }).catch(() => {
        this.isShowLoading(false);
      });
    },

    /**
     * [request 刷新]
     * @return {[type]} [description]
     */
    request() {
      let page = this.page.page;

      if (this.data.length == 1) {
        page = this.returnPage(this.page.total, this.page.page, this.page.limit);
      }

      this.index();
      this.changePage(page);
    },

    resetSearch() {
      for (let k in this.options) {
        if (k === 'month') {
          this.options[k] = this.moment().subtract('1', 'months').startOf('month').format('YYYY-MM');
        } else {
          this.options[k] = '';
        }
      }

      this.index();
    },
    changeLimit(limit) {
      this.page.limit = limit;
      this.changePage(1);
    },
    changePage(page) {
      this.page.page = page;
      this.data = this.list.slice((page - 1) * this.page.limit, page * this.page.limit);

      this.data.push({
        id: "总计",
        total: sumBy(this.list, "total"),
        counts: sumBy(this.list, "counts"),
        renewed_counts: sumBy(this.list, "renewed_counts")
      });
    },

    exportExcel() {
      this.isShowLoading(true);

      let options = Object.assign({
        orderBy: 'id',
        sortedBy: 'asc'
      },

      this.options);

      let params = this.searchDataHandle({}, {}, options);

      this.isShowLoading(true);

      service.get('api/stats/company-count/export', {
        params
      }).then((res) => {
        if (res.code === 0) {
          this.downloadFile(res.data);
        }

        this.isShowLoading(false);
      }).catch(() => {
        this.isShowLoading(false);
      });
    }
  }
};
