export default {
  name: "SoldActivated",
  data() {
    return {
      params: {
        company_id: "",
        starttime: this.moment()
          .startOf("year")
          .format("YYYY-MM"),
        endtime: this.moment()
          .subtract("1", "months")
          .format("YYYY-MM")
      },
      search: {
        show: false
      },
      inputValue: "",
      companies: [],
      company: { id: 0, name: "请选择企业" },
      data: [],
      columns: []
    };
  },
  watch: {
    inputValue(value) {
      this.handleCompleteCompanies(value).then(res => {
        this.companies = res.filter(item => {
          return item.status === 0;
        });
      });
    }
  },
  created() {
    this.initCompleteCompanies().then(res => {
      this.companies = res.filter(function(item) {
        return item.status === 0;
      });
    });
  },
  methods: {
    setColumns(maxTime) {
      let columns = [
        {
          title: "销售时间",
          key: "order_month",
          minWidth: 110
        },
        {
          title: "销售数",
          key: "sells",
          minWidth: 110
        }
      ];

      for (
        let i = 0;
        i <=
        this.moment(maxTime).diff(this.moment(this.params.starttime), "months");
        i++
      ) {
        columns.push({
          title: this.moment(this.params.starttime)
            .add(i, "months")
            .format("YYYY-MM"),
          key: this.moment(this.params.starttime)
            .add(i, "months")
            .format("YYYY-MM"),
          minWidth: 110
        });
      }

      this.columns = columns;
    },

    /**
     * [index 列表]
     * @param  {Number} company_id [description]
     * @return {[type]}      [description]
     */
    index(company_id = null) {
      if (company_id) {
        this.params.company_id = company_id;
        this.company = this.companies.find(item => {
          return item.id === company_id;
        });
      }

      if (this.params.company_id === "") {
        return this.$Message.error("请先选择企业");
      }

      this.isShowLoading(true);

      let params = JSON.parse(JSON.stringify(this.params));

      params.starttime = this.moment(params.starttime).format("YYYY-MM");
      params.endtime = this.moment(params.endtime).format("YYYY-MM");

      service
        .get("api/stats/sold-activated", {
          params
        })
        .then(res => {
          this.isShowLoading(false);
          if (res.code == 0) {
            this.data = [];
            this.columns = [];

            let data = res.data[this.params.company_id];

            if (data && data.length) {
              let index = Object.keys(data[0]).length - 1;
              let maxTime = Object.keys(data[0])[index];
              this.setColumns(maxTime);
              this.data = data;
            }
          }
        })
        .catch(() => {
          this.isShowLoading(false);
        });
    },

    /**
     * [request 刷新]
     * @return {[type]} [description]
     */
    request() {
      this.index();
    },
    resetSearch() {
      this.params.starttime = this.moment()
        .startOf("year")
        .format("YYYY-MM");
      this.params.endtime = this.moment()
        .subtract("1", "months")
        .format("YYYY-MM");
      this.index();
    },
    exportData() {
      this.$refs.table.exportCsv({
        filename:
          "销售激活统计_" +
          this.company.name +
          "_" +
          this.moment(this.params.starttime).format("YYYYMM") +
          "-" +
          this.moment(this.params.endtime).format("YYYYMM")
      });
    }
  }
};
