<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-input v-model="listQuery.certificateNo" 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">
        <el-button type="primary" @click="getList">查询</el-button>
      </div>
    </div>
    <!-- 表格区域 -->
    <el-table
      :key="tableKey"
      :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="120px" align="center">
        <template slot-scope="{ row }">
          <audit-status :status="row.auditStatus" :reason="row.invalidReason" />
        </template>
      </el-table-column>

      <el-table-column label="审核时间" width="160px" 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" width="280px">
        <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="160px" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <audit-button-group
            :review="row.auditStatus !== 2"
            :type="row.auditStatus === 1 ? 'result' : 'audit'"
            :is-dot="row.checkFlag === 0"
            :margin-right="false"
            @click="onClick(row, $event)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <!-- 审核弹窗 -->
    <el-dialog title="驳回" :visible.sync="resultVisible" width="30%" @close="onDialogClose">
      <div class="auth-info">
        <el-form ref="formRef" :model="dialogData" label-width="80px" :rules="rules">
          <el-form-item label="原因：" prop="invalidReason">
            <el-input v-model="dialogData.invalidReason" type="textarea" rows="4" placeholder="请说明原因" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="resultVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { auditDoctor, fetchDoctorList } from '@/api/doctor'

export default {
  name: 'AuditTrainingDoctorList',
  data() {
    return {
      // 是否显示dialog
      resultVisible: false,
      tableKey: 0,
      total: 0,
      listQuery: {
        authUserId: '',
        listType: 2,
        auditStatus: '',
        certificateNo: '',
        doctorName: '',
        doctorType: 2,
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      list: [],
      dialogData: {
        doctorId: '', // 授权机构id
        auditStatus: 0, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: [{ required: true, message: '不通过原因不能为空', tigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'proxyInfo'])
  },
  created() {
    this.listQuery.authUserId = parseInt(this.$route.query.authUserId)
    this.getList()
  },
  activated() {
    this.listQuery.authUserId = parseInt(this.$route.query.authUserId)
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fetchDoctorList()
    },

    async fetchDoctorList() {
      try {
        const res = await fetchDoctorList(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },

    // 按钮组点击事件
    onClick(row, type) {
      const actions = {
        review: this.onReview,
        result: this.onResult,
        audit: this.onAudit
      }
      actions[type](row)
    },

    // 预览
    onReview(row) {
      this.$router.push(`doctor-detail?id=${row.doctorId}&type=review`)
    },
    // 驳回
    onResult(row) {
      this.resultVisible = true
      this.dialogData.doctorId = row.doctorId
    },
    // 审核
    onAudit(row) {
      this.$router.push(`doctor-detail?id=${row.doctorId}&type=audit`)
    },

    // 审核
    async onSubmit() {
      try {
        await this.$refs.formRef.validate()
        await this.onAuthResult()
      } catch (error) {
        console.log(error)
      }
    },

    // 驳回
    async onAuthResult() {
      try {
        await auditDoctor(this.dialogData)
        this.$message.success('驳回成功')
        this.getList()
        this.resultVisible = false
      } catch (error) {
        console.log(error)
      }
    },

    onDialogClose() {
      this.$refs.formRef.resetFields()
    },

    // 表格索引
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

<style lang="scss" scoped></style>
