<template>
  <section id="dev_check">
    <el-select v-model="selected" @change="changeSelect" placeholder="筛选">
      <el-option v-for="(item, index) of statuses" :key="index" :label="item.name" :value="item.id">
        <span>{{item.name}}</span>
      </el-option>
    </el-select>
    <section class="table">
      <el-table
        v-if="tableData.length"
        height="100%"
        border
        style="width: 100%"
        :data="tableData"
        :row-class-name="tableRowClassName"
      >
        <el-table-column
          v-for="(prop, index) of tableProps"
          :key="index"
          :prop="prop"
          :label="tableHeaders[index]"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="prop === 'uid'"
              @click="clickUID(scope.row)"
            >{{scope.row[prop]}}</el-button>
            <el-button
              type="text"
              v-else-if="prop === 'ctrl'"
              @click="clickCtrl(scope.row, index)"
            >查看详情</el-button>
            <img class="img" v-else-if="prop === 'img'" :src="scope.row.img" alt="img">
            <span v-else>{{scope.row[prop]}}</span>
          </template>
        </el-table-column>
      </el-table>
      <span v-else>暂无数据</span>
    </section>
    <el-pagination
      background
      @current-change="changePagination"
      @size-change="changePageSize"
      :current-page.sync="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
    ></el-pagination>
  </section>
</template>

<script>
const tableHeaders = [
  "uid",
  "昵称",
  "职业方向",
  "类型",
  "申请时间",
  "审核时间",
  "申请状态",
  "详情",
]
const tableProps = [
  "uid",
  "nickname",
  "occupation_name",
  "name",
  "create_date",
  "chk_date",
  "status_name",
  "ctrl",
]

let env = 'test'

export default {
  data() {
    return {
      // 筛选状态
      statuses: [],
      selected: "",
      // 数据总条目
      totalCount: 0,
      currentPage: 1,
      currentPageSize: 20,
      certID: '',
      // 列表头显示内容
      tableHeaders,
      // 列表头字段
      tableProps,
      // 列表数据
      tableData: []
    }
  },
  watch: {
    '$route'(to, from) {
      this.handleRouteParams()
    }
  },
  mounted() {
    // console.log(this.currentPage)
    this.getFilters()
  },
  methods: {
    // 页码变动
    changePagination(page) {
      // console.log(this.certID, page)
      this.$router.replace({
        // path: `/main/dev_check`,
        query: { page, cert_id: this.certID }
      })
    },
    changePageSize(pageSize) {
      this.currentPageSize = pageSize
    },
    // 筛选框变动
    changeSelect(id) {
      this.certID = id
      this.$router.replace({
        // path: `/main/dev_check`,
        query: { page: this.currentPage, cert_id: id }
      })
    },
    // 获取筛选
    async getFilters() {
      const res = await this.$post("/api/admin/cert/getEnum")
      const data = res.data
      data.cert_type_list.unshift({
        id: 0,
        name: '全部'
      })
      this.statuses = data.cert_type_list || []
      this.selected = this.statuses[0].name
      this.handleRouteParams()
    },
    /**
     * 路由钩子触发获取路由携带query, 首次进入也算一次
     */
    handleRouteParams() {
      this.currentPage = Number(this.$route.query.page)
      this.certID = this.$route.query.cert_id
      for (var i=0; i< this.statuses.length; i++){
        if (this.certID == this.statuses[i].id) {
          this.selected = this.statuses[i].name
        }
      }
      this.getTableData()
    },
    clickCtrl(item, index) {
      this.$router.push({
        path: '/main/dev_check_detail',
        query: {
          uid: item.uid,
          cert_id: item.cert_id
        }
      })
    },
    // 点击用户的 uid
    clickUID(item) {
      window.open(`https://www.proginn.com/rooter/user/${item.uid}`)
    },
    // 根据状态显示图表样式
    tableRowClassName({ row, rowIndex }) {
      let className = ""
      if(row.status === "1") className = "success-row"
      return className
    },
    // 格式化列表数据
    formatTableData(data) {
      return data.map(i => ({
        ...i,
        statusShow: i.status === "1" ? "到账" : "失败",
        created_atShow: new Date(Number(i.created_at) * 1000).toLocaleString(),
        taken_atShow: new Date(Number(i.taken_at) * 1000).toLocaleString(),
        ctrl: 1
      }))
    },
    // 获取列表数据
    async getTableData() {
      this.tableData = []
      let page = this.currentPage
      let page_size = this.currentPageSize
      let cert_id = Number(this.certID)
      let body = { page, page_size, cert_id }
      let res = await this.$post("/api/admin/audit/getList", body)
      let data = res.data
      env = data.current_env
      this.tableData = this.formatTableData(res.data.list)
      this.totalCount = Number(data.total)
      this.totalPage = data.totalPage
    }
  }
};
</script>

<style scoped>
.table {
  height: calc(100% - 80px);
}
.img {
  width: 100%;
  height: auto;
}
</style>
