<template>
  <div class="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <el-col :span="24" class="one">
          <div class="one_right">
            <el-input v-model="searchForm.name" style="width: 250px" size="large" placeholder="搜索" @change="search" :suffix-icon="Search" />
          </div>
        </el-col>
        <el-col :span="24" class="two">
          <el-table :data="list" style="width: 100%" size="large" :header-cell-style="{ backgroundColor: '#edf3ff' }">
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <el-table-column prop="name" align="center" label="姓名" />
            <el-table-column prop="phone" align="center" label="手机号" width="180" />
            <el-table-column prop="time" align="center" label="报名时间" width="180" />
            <el-table-column prop="status" align="center" label="状态" width="180">
              <template #default="scope">
                <div>{{ getDict(scope.row.status, 'status') }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="180">
              <template #default="{ row }">
                <el-link v-if="row.status == '0'" :underline="false" type="warning" size="mini" @click="toExam(row)" style="margin-right: 10px">审核</el-link>
                <el-link :underline="false" type="primary" size="mini" @click="toView(row)" style="margin-right: 10px">查看</el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="24" class="thr">
          <el-pagination background layout="prev, pager, next" :total="total" :page-size="limit" v-model:current-page="currentPage" @current-change="changePage" @size-change="sizeChange" />
        </el-col>
      </el-col>
    </el-row>
    <el-dialog v-model="dialog.show" :title="dialog.title" :destroy-on-close="false" @close="toClose">
      <el-row>
        <el-col :span="24" v-if="dialog.type == '1'">
          <custom-form v-model="form" :fields="formFields" :useSave="false">
            <template #cardType>
              <el-option v-for="i in cardTypeList" :key="i.id" :label="i.label" :value="i.value"></el-option>
            </template>
          </custom-form>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '2'">
          <custom-form v-model="examForm" :fields="examFormFields" :rules="examRules" @save="toExamSave" :DraftSave="false" submitText="保存">
            <template #status>
              <el-option v-for="i in statusList" :key="i.id" :label="i.label" :value="i.value"></el-option>
            </template>
          </custom-form>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { cloneDeep, get } from 'lodash-es'
const $checkRes = inject('$checkRes')
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 接口
import { SignStore } from '@/store/api/platform/sign'
import { DictDataStore } from '@/store/api/system/dictData'
const store = SignStore()
const dictDataStore = DictDataStore()
// 加载中
const loading = ref(false)

const searchForm = ref({})
// 列表
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(0)
const currentPage = ref(1)

const props = defineProps({
  matchForm: { type: Object, default: () => {} }
})

const { matchForm } = toRefs(props)

// 字典表
const cardTypeList = ref([])
const statusList = ref([])

const form = ref({})
const dialog = ref({ type: '1', show: false, title: '报名管理' })
const formFields = ref([
  { label: '姓名', model: 'name' },
  { label: '手机号', model: 'phone' },
  { label: '证件类型', model: 'cardType', type: 'select' },
  { label: '证件号码', model: 'card' },
  { label: '报名时间', model: 'time' },
  { label: '微信/QQ', model: 'communication' },
  { label: '电子邮箱', model: 'email' },
  { label: '备注', model: 'remark', type: 'textarea' }
])
// 审核
const examFormFields = [{ label: '状态', model: 'status', type: 'select' }]
const examRules = reactive({
  status: [{ required: true, message: '请选项状态', trigger: 'blur' }]
})
const examForm = ref({})
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search()
  loading.value = false
})
const search = async (query = { skip, limit }) => {
  skip = query.skip
  limit = query.limit
  const info = {
    skip: query.skip,
    limit: query.limit,
    match: matchForm.value.id,
    ...searchForm.value
  }
  const res = await store.query(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
const searchOther = async () => {
  let result
  // 证件类型
  result = await dictDataStore.query({ code: 'cardType', is_use: '0' })
  if ($checkRes(result)) cardTypeList.value = result.data
  // 状态
  result = await dictDataStore.query({ code: 'examStatus', is_use: '0' })
  if ($checkRes(result)) statusList.value = result.data
}
// 字典数据转换
const getDict = (data, model) => {
  if (data) {
    let res
    if (model == 'status') res = statusList.value.find((f) => f.value == data)
    return get(res, 'label')
  }
}
// 查看
const toView = (data) => {
  form.value = data
  dialog.value = { type: '1', show: true, title: '查看报名' }
}
// 审核
const toExam = (data) => {
  examForm.value = data
  dialog.value = { type: '2', show: true, title: '报名审核' }
}
// 审核保存
const toExamSave = async () => {
  const data = cloneDeep(examForm.value)
  let res = await store.update({ id: data.id, status: data.status })
  if ($checkRes(res, true)) {
    search({ skip, limit })
    toClose()
  }
}
const toClose = () => {
  form.value = {}
  dialog.value = { show: false }
}
// 分页
const changePage = (page = currentPage.value) => {
  search({ skip: (page - 1) * limit, limit: limit })
}
const sizeChange = (limits) => {
  limit = limits
  currentPage.value = 1
  search({ skip: 0, limit: limit })
}
</script>
<style scoped lang="scss">
.main {
  .one {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 10px 0;
    .one_left {
      background: #1875df;
      padding: 0 10px;
      height: 30px;
      color: #fff;
      line-height: 30px;
      text-align: center;
      font-size: 16px;
      cursor: default;
    }
  }
  .thr {
    display: flex;
    justify-content: center;
    margin: 20px 0 0 0;
  }
}
</style>
