<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span>培训医师姓名:</span>
        <el-input v-model="listQuery.doctorName" placeholder="培训医师姓名" @keyup.enter.native="handleFilter" />
      </div>
      <div class="filter-control">
        <span>审核状态:</span>
        <el-select v-model="listQuery.auditStatus" placeholder="审核状态" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="待审核" :value="2" />
          <el-option label="审核通过" :value="1" />
          <el-option label="审核未通过" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>上线状态:</span>
        <el-select v-model="listQuery.status" placeholder="上线状态" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="已上线" :value="1" />
          <el-option label="待上线" :value="2" />
          <el-option label="未上线" :value="0" />
        </el-select>
      </div>
      <div class="filter-control">
        <permission-button type="primary" @click="getList">查询</permission-button>
        <permission-button type="primary" @click="$_navigationTo('add?type=add')">添加</permission-button>
      </div>
    </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="140px" align="center">
        <template slot-scope="{ row }">
          <!-- 只有审核通过了才能操作上下线 auditStatus ：审核状态 -->
          <template v-if="row.auditStatus === 1">
            <template v-if="row.status === 0">
              <span style="margin-right:10px;" class="status danger">已下线</span>
              <permission-button type="primary" size="mini" @click="handleChangeStatus(row)">上线</permission-button>
            </template>
            <template v-else>
              <span style="margin-right:10px;" class="status success ">已上线</span>
              <permission-button type="info" size="mini" @click="handleChangeStatus(row)">下线</permission-button>
            </template>
          </template>
          <template v-else-if="row.auditStatus === 2">
            <!-- <el-tag type="warning">待上线</el-tag> -->
            <span style="margin-right:10px;" class="status warning">待上线</span>
          </template>
          <template v-else>
            <!-- <el-tag type="warning">待上线</el-tag> -->
            <span style="margin-right:10px;" class="status danger">已下线</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" class-name="status-col" width="300px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | formatTime }}</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 }">
          <permission-button type="primary" size="mini" @click="$_navigationTo(`edit?type=edit&id=${row.doctorId}`)">
            编辑
          </permission-button>
          <permission-button type="danger" size="mini" @click="handleRemoveDoctor(row)">
            删除
          </permission-button>
        </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 PermissionButton from '@/views/components/PermissionButton'
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, PermissionButton },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      total: 0,
      listQuery: {
        listType: 1,
        doctorName: '',
        auditStatus: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      list: []
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.listQuery.authUserId = this.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></style>
