<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      团练宝分润
    </h2>
    <div class="m-core">
      <el-card header="" style="width:460px" class="myCard">
        <statistic class="statistic" :cols="0">
          <statistic-item>
            <span>
              意向机构
            </span>
            <span> <count-to :endVal="detail.tenantAgency" :decimals="2" /></span>
          </statistic-item>
             <statistic-item>
            <span>
              入驻机构

            </span>
            <span> <count-to :endVal="detail.preTenant" :decimals="2" /></span>
          </statistic-item>
             <statistic-item>
            <span>
              团练宝销售数(个月)
            </span>
            <span> <count-to :endVal="detail.memberCount" :decimals="2" /></span>
          </statistic-item>
        </statistic>
      </el-card>
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="proxyId">
          <el-select
            class="multiple"
            v-model.trim="searchForm.proxyId"
            filterable
            clearable
            placeholder="请选代理商"
          >
            <el-option
              v-for="(item, index) in proxyList"
              :key="index"
              :label="item.proxyName"
              :value="item.proxyId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-form-item prop="recommenderId">
            <remote-search
              :commit="'setTenantUser'"
              aria-placeholder="请选择推荐人"
              v-model="searchForm.recommenderId"
            />
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            :clearable="false"
            v-model.trim="searchForm.timer"
            style="width: 420px"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="proxyId"
            label="代理商编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="proxyName"
            label="代理商名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="tenantId"
            label="机构编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="tenantName"
            label="机构名称"
          ></el-table-column>
          <el-table-column align="center" prop="activeDate" label="激活时间">
            <template slot-scope="scope">
              {{ scope.row.activeDate | formatTimer }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="activeCount"
            label="激活数量(个月)"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="recommenderName"
            label="推荐人"
          ></el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import { getTimes } from "@/utils";
import load from "@/utils/loading";
import { getProxyUserList, getProxyDividend } from "./api";
import countTo from 'vue-count-to'
export default {
  components: { pagination ,   'count-to': countTo},
  data() {
    return {
      searchForm: {
        search: null,
        timer: [],
      },
      detail:{
        memberCount:0,
          preTenant:0,
          tenantAgency:0
      },
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      proxyList: [],
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部
    this.searchForm.timer = this.getInitDate();
    this.init();
  },
  methods: {
    init() {
      this.getProxList();
      this.getList();
    },
    async getProxList() {
      try {
        const res = await getProxyUserList({
          page: 1,
          rows: 9999,
          state: "0",
        });
        this.proxyList = res.data.rows;
      } catch (e) {}
    },
    async getList() {
      let { timer, ...rest } = this.searchForm;
      let params = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        ...getTimes(timer, ["startData", "endData"]),
      };
      try {
        const res = await getProxyDividend({ ...params });
        this.tableList = res.data.pageData.rows;
        this.rules.total = res.data.pageData.total;
        this.detail = {
          memberCount:res.data.memberCount,
          preTenant:res.data.preTenant,
          tenantAgency:res.data.tenantAgency
        }
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$nextTick(()=>{
         this.searchForm.timer = this.getInitDate();
         this.search()
      })
    },
    getInitDate() {
      const end = this.$helpers.dayjs(new Date()).format("YYYY-MM-DD");
      const start = this.$helpers
        .dayjs(new Date())
        .set("date", 1)
        .format("YYYY-MM-DD");
      return [start, end];
    },
  },
};
</script>
<style lang='scss' scoped>
.myCard {
  margin-bottom: 20px;
 /deep/.el-card__body {
    padding: 0 20px!important;
  }
}
</style>
