<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="账号昵称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入账号昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="authStatus">
        <el-select
          v-model="queryParams.authStatus"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <el-table-column label="学校名称" align="center" prop="school.name" fixed="left">
        <template #default="scope">{{ formatName(scope.row.school?.name) }}</template>
      </el-table-column>
      <el-table-column label="账号昵称" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="审核状态" align="center" prop="authStatus">
        <template #default="scope">
          <el-tag :type="statusList.find(e => e.value === scope.row.authStatus)?.color">
            {{ statusList.find(e => e.value === scope.row.authStatus)?.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="handleAction(scope.row, 'details')">详情</el-button>
          <template v-if="scope.row.authStatus === '0'">
            <el-button link type="danger" @click="handleAction(scope.row, 'refuse')">拒绝</el-button>
            <el-button link type="success" @click="handleAction(scope.row, 'agree')">通过</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 拒绝-原因 -->
  <RefuseForm ref="refuseRef" @success="getList" />
</template>

<script setup lang="ts">
/** 门墩儿-学校注册申请 列表 */
defineOptions({ name: 'SchoolAccountRegisterExamine' })

import { dateFormatter } from '@/utils/formatTime'
import { FlameSchoolApi } from '@/api/menduner/schoolRegisterExamine'
import { formatName } from '@/utils'
import RefuseForm from './RefuseForm.vue'

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  phone: undefined,
  authStatus: undefined,
  name: undefined
})
const queryFormRef = ref() // 搜索的表单

const statusList = [
  { value: '0', label: '待审核', color: 'warning' },
  { value: '1', label: '审核通过', color: 'success' },
  { value: '2', label: '审核不通过', color: 'danger' }
]

/** 查询列表 */
const getList = async () => {
  loading.value = true

  try {
    const data = await FlameSchoolApi.flameSchoolPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/* 拒绝 */
const refuseRef = ref()
const handleAction = (item: object, type: string) => {
  refuseRef.value.open(item, type)
}

/** 初始化 **/
onMounted(() => {
  queryParams.pageNo = 1
  getList()
})

</script>