<template>
  <div>
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="教员姓名" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.phone" placeholder="手机号" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" clearable filterable placeholder="审核状态">
          <el-option label="待审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column prop="name" label="教员姓名" />
      <el-table-column prop="phone" label="联系方式" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="applyTime" label="申请时间" />
      <el-table-column prop="status" label="审核状态">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1" type="info">待审核</el-tag>
          <el-tag v-else-if="row.status == 2" type="success">已通过</el-tag>
          <el-tag v-else type="danger">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="checkUser" label="审核人" />
      <el-table-column prop="checkTime" label="审核时间" />
      <!-- 操作 -->
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            v-hasPermi="['JoinUs:Station:audit']"
            @click="handleDetail(row, 'edit')"
          >
            修改
          </el-button>
          <el-button
            v-if="row.status == 1"
            type="primary"
            link
            v-hasPermi="['JoinUs:Station:audit']"
            @click="handleDetail(row, 'audit')"
          >
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="dialogVisible" title="教员详情" style="width: 600px">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px">
        <el-form-item label="教员姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :autosize="{ minRows: 2 }"
            placeholder="请输入"
          />
        </el-form-item>
        <div v-if="dialogType == 'audit'">
          <el-divider direction="horizontal" content-position="left">审核信息</el-divider>
          <el-form-item label="审核结果" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="2" :value="2">通过</el-radio>
              <el-radio :label="3" :value="3">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核说明" prop="checkRemark">
            <el-input
              v-model="form.checkRemark"
              type="textarea"
              :autosize="{ minRows: 2 }"
              placeholder="请输入"
            />
          </el-form-item>
        </div>
      </el-form>
      <template #footer>
        <el-button type="primary" :disabled="formLoading" @click="submitForm">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </template>
    </Dialog>
  </div>
</template>

<script setup name="Coach">
import { auditTeacher, searchTeacher, updateTeacher } from '@/api/uav/regiest'

const message = useMessage()

const searchForm = ref({
  name: '',
  phone: '',
  status: 1,
  pageNo: 1,
  pageSize: 20
})

const tableList = ref([])
const total = ref(0)

const form = ref({})
const rules = {
  name: { required: true, message: '请输入名称', trigger: 'blur' },
  phone: { required: true, message: '请输入手机号', trigger: 'blur' }
}
const dialogVisible = ref(false)
const dialogType = ref('audit')

onMounted(() => {
  handleQuery()
})

function handleQuery() {
  searchForm.value.pageNo = 1
  getList()
}

function getList() {
  searchTeacher(searchForm.value).then((response) => {
    tableList.value = response.list
    total.value = response.total
  })
}

function handleDetail(row, type) {
  dialogType.value = type
  dialogVisible.value = true
  form.value = { ...row }
}

const formLoading = ref(false)
const formRef = ref(null)
async function submitForm() {
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return

  try {
    formLoading.value = true
    let params = { ...form.value }
    if (dialogType.value == 'audit') {
      auditTeacher(params).then((response) => {
        if (response) {
          dialogVisible.value = false
          message.success('操作成功')
        }
      })
    } else {
      updateTeacher(params).then((response) => {
        if (response) {
          dialogVisible.value = false
          message.success('操作成功')
        }
      })
    }
  } finally {
    formLoading.value = false
    getList()
  }
}
</script>

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