<template>
  <div class="app-container">
    <div class="filter-container">
      <span>医师姓名:</span>
      <el-input v-model="listQuery.doctorName" placeholder="医师姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <span>从业资格证编号:</span>
      <el-input v-model="listQuery.certificateNo" placeholder="从业资格证编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <span>审核状态:</span>
      <el-select
        v-model="listQuery.auditStatus"
        placeholder="审核状态"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="待审核" :value="2" />
        <el-option label="审核通过" :value="1" />
        <el-option label="审核未通过" :value="0" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
    </div>
    <!-- 表格区域 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      header-row-class-name="tableHeader"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" align="center" width="80" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="从业资格证编号" align="center" prop="certificateNo" />

      <el-table-column label="审核状态" width="220px" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.auditStatus === 2" size="small" type="warning">待审核</el-tag>
          <el-tag v-if="row.auditStatus === 1" size="small" type="success">审核通过</el-tag>
          <!-- 未通过原因展示 -->
          <template v-if="row.auditStatus === 0">
            <!-- <span class="status danger">审核未通过&nbsp;</span> -->
            <el-popover
              placement="top-start"
              title="审核说明"
              width="400"
              trigger="hover"
              :content="row.invalidReason"
            >
              <el-tag slot="reference" size="small" type="danger" class="reason">
                <span>审核未通过</span>
                <span class="el-icon-question status danger " />
              </el-tag>
            </el-popover>
            <!-- 未通过原因展示END -->
          </template>
        </template>
      </el-table-column>

      <el-table-column label="审核时间" width="250px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.auditStatus!==2">{{ row.auditTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <el-table-column label="审核人" align="center">
        <template slot-scope="{row}">
          <span v-if="row.auditStatus!==2">{{ row.auditBy }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <!-- <el-table-column v-if="false" label="创建人" width="180px" align="center" prop="createBy" /> -->
      <el-table-column label="操作" align="center" width="240px" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button v-if="row.auditStatus !== 1" type="primary" size="mini" @click="$_navigationTo(`doctor-detail?id=${row.doctorId}`)">
            审核
          </el-button>
          <span v-else class="status success el-icon-check">&nbsp;已审核</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils'
import { fetchDoctorList, removeDoctor, doctorStatusChange } from '@/api/doctor'

export default {
  components: { Pagination },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      total: 0,
      listQuery: {
        authUserId: '',
        listType: 2,
        doctorName: '',
        auditStatus: '',
        certificateNo: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'proxyInfo'])
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.listQuery.authUserId = this.$route.query.authUserId

      console.log(this.listQuery.authUserId)
      fetchDoctorList(this.listQuery).then(res => {
        console.log(res)
        this.list = res.data.list
        this.total = res.data.total
      })
    },
    // 修改状态
    handleChangeStatus(row) {
      const status = row.status === 1 ? 0 : 1

      doctorStatusChange({ doctorId: row.doctorId, status }).then(res => {
        this.$message.success('状态修改成功')
        this.getList()
      })
    },
    // 删除
    async handleRemoveDoctor(row) {
      const text = await this.$confirm('确认删除该医师信息吗？删除后不可恢复', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message.info('已取消操作')
      })
      if (text !== 'confirm') return

      removeDoctor({ doctorId: row.doctorId }).then(res => {
        this.$message.success('删除成功')
        this.getList()
      })
    },
    // 表格索引
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container{
  span{
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
    font-size: 14px;
  }
  .el-button{
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
  }
  .el-input,.el-select{
    margin-right: 10px;
    margin-left: 10px;
  }
}
</style>
