<template>
  <div class="container-jurisdiction">
    <el-card>
      <div class="top-box">
        <div>
          <a :href="exportExcel" download>
            <el-button type="primary">导出EXCEL</el-button>
          </a>
          <el-cascader
            v-model="sellName"
            placeholder="请选择销售"
            style="width: 230px; margin: 0 30px"
            :options="sellOptions"
            :props="defaultSalesProps"
            :show-all-levels="false"
            collapse-tags
            clearable
            filterable
            @change="changeTableDate"
          >
          </el-cascader>
          <el-select style="width: 230px" v-model="clientState" @change="changeTableDate" placeholder="请选择客户状态" clearable>
            <el-option v-for="item in clientOptions" :key="item.label" :label="item.label" :value="item.label"> </el-option>
          </el-select>
        </div>
        <div style="width: 400px">
          <el-input v-model="keyword" placeholder="联系人姓名/客户名称" clearable>
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </div>
      </div>
    </el-card>

    <el-card style="margin-top: 30px">
      <div class="main-section">
        <el-card class="base-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span>
              开通权限用户数
              <el-tooltip class="item" effect="dark" content="根据筛选条件统计表格内联系人数量" placement="top">
                <i class="el-icon-info"></i>
              </el-tooltip>
            </span>
          </div>
          <div class="card-cont">{{ userTotal }}</div>
        </el-card>
        <el-card class="base-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span>
              开通权限客户数
              <el-tooltip class="item" effect="dark" content="根据筛选条件统计表格内客户数量" placement="top">
                <i class="el-icon-info"></i>
              </el-tooltip>
            </span>
          </div>
          <div class="card-cont">{{ companyTotal }}</div>
        </el-card>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="UserName" label="联系人姓名" align="center"></el-table-column>
        <el-table-column prop="CompanyName" label="客户名称" align="center">
          <template slot-scope="scope">
            <span class="editsty" @click="goDetail(scope.row)">{{ scope.row.CompanyName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SellerName" label="所属销售" align="center"></el-table-column>
        <el-table-column prop="CompanyStatus" label="客户状态" align="center"></el-table-column>
        <el-table-column prop="StartTime" label="图库服务期限" align="center">
          <template slot-scope="scope"> {{ scope.row.StartTime }} ~ {{ scope.row.EndTime }} </template>
        </el-table-column>
        <el-table-column prop="RestDay" label="剩余天数" align="center"></el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <m-page :total="total" :page_no="page_no" @handleCurrentChange="handleCurrentChange" />
      </el-col>
    </el-card>
  </div>
</template>

<script>
import mPage from "@/components/mPage.vue";
import { dataMainInterface, customInterence } from "@/api/api.js";

export default {
  name: "",
  components: { mPage },
  props: {},
  data() {
    return {
      defaultSalesProps: {
        multiple: true,
        label: "RealName",
        children: "ChildrenList",
        value: "AdminId",
      }, //销售级联配置
      sellOptions: [], //销售
      clientOptions: [
        {
          label: "正式",
          value: "1",
        },
        {
          label: "试用",
          value: "2",
        },
        {
          label: "永续",
          value: "3",
        },
      ], //状态
      sellName: "", //销售
      clientState: "", //状态
      pageSize: 10,
      page_no: 1,
      total: 0,
      tableData: [],
      keyword: "",
      orderNumber: "1",
      userTotal: "",
      companyTotal: "",
    };
  },
  computed: {
    exportExcel() {
      let arr = this.sellName.length ? this.sellName.map((item) => item[item.length-1]) : "";
      let sellUname = arr.length ? arr.join(",") : "";
      let str = `&Keyword=${this.keyword}&CompanyStatus=${this.clientState}&SellerId${sellUname}`;
      return (process.env.API_ROOT + "/yb/chartCensus/exportChartCompanyAuthCensusList?" + localStorage.getItem("auth") || "") + str;
    },
  },
  watch: {
    keyword: {
      handler(newval) {
        this.sellName = "";
        this.clientState = "";
        this.page_no = 1;
        this.getTableList();
      },
    },
  },
  created() {},
  mounted() {
    this.getTableList();
    this.getSale();
  },
  methods: {
    /* 获取销售 */
    getSale() {
      customInterence.getSale().then((res) => {
        if (res.Ret === 200) {
          this.sellOptions = res.Data.List;
        }
      });
    },
    //分页
    handleCurrentChange(page) {
      this.page_no = page;
      this.getTableList();
    },
    //获取表格数据
    async getTableList() {
      let arr = this.sellName.length ? this.sellName.map((item) => item[item.length-1]) : "";
      const res = await dataMainInterface.getCompanyAuthCensusList({
        PageSize: this.pageSize,
        CurrentIndex: this.page_no,
        SellerId: arr.length ? arr.join(",") : "",
        CompanyStatus: this.clientState,
        Keyword: this.keyword,
      });
      if (res.Ret === 200) {
        this.tableData = res.Data.List;
        this.total = res.Data.Paging.Totals;
        this.userTotal = res.Data.UserTotal;
        this.companyTotal = res.Data.CompanyTotal;
      }
    },
    //表格的 change 事件
    changeTableDate() {
      this.page_no = 1;
      this.getTableList();
    },
    goDetail(item){
      this.$router.push({
        path:'/customDetail',
        query: {
          id: item.CompanyId,
        }
      })
    },
  },
};
</script>
<style scoped lang="scss">
.container-jurisdiction {
  .top-box {
    display: flex;
    justify-content: space-between;
  }
  .main-section {
    margin-bottom: 30px;
    display: flex;
    .base-card {
      width: 240px;
      background: #fff;
      color: #999;
      text-align: center;
      margin-right: 30px;
      cursor: pointer;
      .el-card__header {
        padding: 16px;
      }
      .card-cont {
        font-size: 28px;
        color: #333;
      }
    }
  }
}
</style>
