export default {
  name: 'StatsRenewal',
  data() {
    return {
      search: {
        show: true
      },
      options: {
        name: '',
        activate_time: [],
        renewal_time: []
      },
      data: [],
      list: [],
      stats: {},
      page: {
        total: 0,
        limit: 10,
        page: 1
      },
      columns: [
        {
          title: '企业ID',
          key: 'company_id',
          width: 80
        },
        {
          title: '企业名称',
          key: 'company_name'
        },
        {
          title: '激活数',
          key: 'activates',
          width: 120
        },
        {
          title: '续费数',
          key: 'renewals',
          width: 120
        },
        {
          title: '续费率',
          key: 'ratio',
          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);

      console.log(params);

      for (let k in params) {
        if (k == "activate_time") {
          if (params.activate_time && params.activate_time[0] && params.activate_time[1]) {
            params.activate_start_time = this.moment(params.activate_time[0]).format("YYYY-MM-DD") + " 00:00:00";
            params.activate_end_time = this.moment(params.activate_time[1]).format("YYYY-MM-DD") + " 23:59:59";
            params.activate_time = undefined;
          }
        }

        if (k == "renewal_time") {
          if (params.renewal_time && params.renewal_time[0] && params.renewal_time[1]) {
            params.renewal_start_time = this.moment(params.renewal_time[0]).format("YYYY-MM-DD") + " 00:00:00";
            params.renewal_end_time = this.moment(params.renewal_time[1]).format("YYYY-MM-DD") + " 23:59:59";
            params.renewal_time = undefined;
          }
        }
      }

      this.isShowLoading(true);

      service.get('api/stats/renewal', {
        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);
    },

    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);
      });
    }
  }
};
