import { sumBy } from "service/util";
export default {
  name: "StatsCompanyReport",
  components: {
    UiDetail: resolve => require(["views/stats/company-report/detail"], resolve)
  },
  data() {
    return {
      search: {
        show: true
      },
      detailObj: {
        type: null,
        options: {}
      },
      types: [],
      options: {
        company_name: "",
        package_name: "",
        type: [],
        month: this.moment()
          .subtract("1", "months")
          .startOf("month")
          .format("YYYY-MM")
      },
      data: [],
      list: [],
      stats: {},
      page: {
        total: 0,
        limit: 10,
        page: 1
      },
      columns: [
        {
          title: "企业名称",
          key: "company_name",
          width: 300
        },
        {
          title: "套餐名称",
          key: "package_name",
          width: 170
        },
        {
          title: "套餐周期(月)",
          key: "service_months",
          width: 150
        },
        {
          title: "套餐价格(元/周期)",
          key: "unit_price",
          width: 150
        },
        {
          title: "套餐单价(元/月)",
          key: "month_price",
          width: 150
        },
        {
          title: "收费人数",
          key: "members",
          width: 130
        },
        {
          title: "收费数",
          key: "counts",
          width: 130
        },
        {
          title: "收费总价(元)",
          key: "total_price",
          width: 150
        },
        {
          title: "收费类型",
          key: "type_name",
          width: 120
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          render: (h, { row, column, index }) => {
            if (index == this.data.length - 1) {
              return;
            }

            let html = [];

            html.push(
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    disabled: false,
                    icon: "md-eye"
                  },
                  class: ["btn"],
                  on: {
                    click: event => {
                      this.isShowLoading(true);

                      let params = {
                        page: 1,
                        limit: 10,
                        type: row.type,
                        company_id: row.company_id,
                        month: this.moment(this.options.month).format(
                          "YYYY-MM"
                        ),
                        package_id: row.package_id,
                        unit_price: Number(row.unit_price * 100)
                      };

                      service
                        .get("api/stats/company-report/detail", {
                          params
                        })
                        .then(res => {
                          this.isShowLoading(false);
                          if (res.code == 0) {
                            this.detailObj = {
                              show: true,
                              options: params,
                              list: res.data
                            };
                          }
                        })
                        .catch(() => {
                          this.isShowLoading(false);
                        });
                    }
                  }
                },
                "明细"
              )
            );

            if (html.length) {
              return h("div", html);
            }
          }
        }
      ]
    };
  },
  created() {
    this.types = this.$route.params.type == 1 ? [0, 1] : [2, 3];
    this.index();
  },
  methods: {
    /**
     * [index 列表]
     * @param  {Number} page [description]
     * @return {[type]}      [description]
     */
    index() {
      this.isShowLoading(true);

      this.data = [];

      service
        .get("api/stats/company-report", {
          params: this.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);
        });
    },

    exportExcel() {
      this.isShowLoading(true);

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

          this.isShowLoading(false);
        })
        .catch(() => {
          this.isShowLoading(false);
        });
    },
    params() {
      if (!this.options.month) {
        this.options.month = this.moment()
          .subtract("1", "months")
          .startOf("month")
          .format("YYYY-MM");
      }

      if (!this.options.type.length) {
        this.options.type = this.types;
      }

      this.options.month = this.moment(this.options.month).format("YYYY-MM");

      let params = {
        limit: 0,
        type: this.options.type,
        month: this.moment(this.options.month).format("YYYY-MM"),
        company_name: this.options.company_name,
        package_name: this.options.package_name
      };

      return params;
    },

    /**
     * [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 if (k === "type") {
          this.options[k] = [];
        } else {
          this.options[k] = "";
        }
      }

      this.page = {
        total: 0,
        limit: 10,
        page: 1
      };

      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.tableFooter();
    },
    tableFooter() {
      this.data.push({
        company_name: "总计",
        members: sumBy(this.list, "members"),
        counts: sumBy(this.list, "counts"),
        total_price: Number(sumBy(this.list, "total_price")).toFixed(2)
      });
    }
  }
};
