<template>
  <div class="main animate__animated animate__backInRight" v-loading="loading">
    <custom-search-bar :fields="fields.filter((f) => f.isSearch)" v-model="searchForm" @search="search" @reset="toReset">
      <template #type>
        <el-option v-for="i in typeList" :key="i._id" :label="i.label" :value="i.value"></el-option>
      </template>
      <template #status>
        <el-option v-for="i in statusList" :key="i._id" :label="i.label" :value="i.value"></el-option>
      </template>
      <template #subject>
        <el-option v-for="i in subjectList" :key="i._id" :label="i.label" :value="i.value"></el-option>
      </template>
      <template #grade>
        <el-option v-for="i in gradeList" :key="i._id" :label="i.label" :value="i.value"></el-option>
      </template>
    </custom-search-bar>
    <custom-table :data="data" :fields="fields" @query="search" :total="total" :opera="opera" @view="toView" @exam="toExam"  @delete="toDelete">
      <template #status="{ row }">
        <el-tag v-if="row.status == '1'" type="success">已解决</el-tag>
        <el-tag v-else type="info">未解决</el-tag>
      </template>
    </custom-table>
    <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" @save="toSave">
            <template #status>
              <el-option v-for="i in statusList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #type>
              <el-option disabled v-for="i in typeList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #subject>
              <el-option disabled v-for="i in subjectList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #grade>
              <el-option disabled v-for="i in gradeList" :key="i._id" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #is_show>
              <el-option v-for="i in showList" :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">
            <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 { cloneDeep, get } from 'lodash-es'
const { t } = useI18n()
const $checkRes = inject('$checkRes')
// 接口
import { CourseStore } from '@/store/api/core/course'
import { DictDataStore } from '@/store/api/system/dictData'
const dictDataStore = DictDataStore()
const store = CourseStore()
const data = ref([])
const searchForm = ref({})
const fields = [
  { label: t('pages.course.teacher_name'), model: 'teacher_name' },
  { label: t('pages.course.teacher_phone'), model: 'teacher_phone' },
  { label: t('pages.course.name'), model: 'name', isSearch: true },
  { label: t('pages.course.money'), model: 'money' },
  { label: t('pages.course.subject'), model: 'subject', format: (i) => getDict(i, 'subject'), type: 'select', isSearch: true },
  { label: t('pages.course.grade'), model: 'grade', format: (i) => getDict(i, 'grade'), type: 'select', isSearch: true },
  { label: t('pages.course.type'), model: 'type', format: (i) => getDict(i, 'type'), type: 'select', isSearch: true },
  { label: t('pages.course.start_time'), model: 'start_time', type: 'datetime' },
  { label: t('pages.course.end_time'), model: 'end_time', type: 'datetime' },
  { label: t('pages.course.status'), model: 'status', format: (i) => getDict(i, 'status'), type: 'select', isSearch: true }
]
let skip = 0
let limit = inject('limit')
const total = ref(0)
const opera = [
  { label: t('common.view'), method: 'view' },
  { label: t('common.exam'), method: 'exam', type: 'warning', display: (i) => i.status === '0' },
  {
    label: t('common.delete'),
    method: 'delete',
    confirm: true,
    type: 'danger'
  }
]
const dialog = ref({ type: '1', show: false, title: t('pages.course.addDialogTitle') })
const form = ref({})
const formFields = [
  { label: t('pages.course.teacher_name'), model: 'teacher_name', options: { readonly: true } },
  { label: t('pages.course.teacher_phone'), model: 'teacher_phone', options: { readonly: true } },
  { label: t('pages.course.name'), model: 'name', options: { readonly: true } },
  { label: t('pages.course.subject'), model: 'subject', type: 'select' },
  { label: t('pages.course.grade'), model: 'grade', type: 'select' },
  { label: t('pages.course.type'), model: 'type', type: 'select' },
  { label: t('pages.course.money'), model: 'money', options: { readonly: true } },
  { label: t('pages.course.start_time'), model: 'start_time', options: { readonly: true } },
  { label: t('pages.course.end_time'), model: 'end_time', options: { readonly: true } },
  { label: t('pages.course.brief'), model: 'brief', type: 'textarea', options: { readonly: true } },
  { label: t('pages.course.is_show'), model: 'is_show', type: 'select' }
]
// 审核
const examFormFields = [{ label: t('pages.teacher.status'), model: 'status', type: 'select' }]
const examRules = reactive({
  status: [{ required: true, message: t('common.statusMessage'), trigger: 'blur' }]
})
const examForm = ref({})
// 加载中
const loading = ref(false)
// 字典表
const statusList = ref([])
const typeList = ref([])
const gradeList = ref([])
const showList = ref([])
const subjectList = ref([])
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search({ skip, limit })
  loading.value = false
})
const searchOther = async () => {
  let result
  // 类型
  result = await dictDataStore.query({ code: 'courseType', is_use: '0' })
  if ($checkRes(result)) typeList.value = result.data
  // 状态
  result = await dictDataStore.query({ code: 'courseStatus', is_use: '0' })
  if ($checkRes(result)) statusList.value = result.data
  // 学科
  result = await dictDataStore.query({ code: 'subject', is_use: '0' })
  if ($checkRes(result)) subjectList.value = result.data
  // 年级
  result = await dictDataStore.query({ code: 'grade', is_use: '0' })
  if ($checkRes(result)) gradeList.value = result.data
  // 是否公开
  result = await dictDataStore.query({ code: 'show', is_use: '0' })
  if ($checkRes(result)) showList.value = result.data
}
const search = async (query = { skip: 0, limit }) => {
  const info = { skip: query.skip, limit: query.limit, ...searchForm.value }
  const res = await store.list(info)
  if (res.errcode == '0') {
    data.value = res.data
    total.value = res.total
  }
}
// 字典数据转换
const getDict = (data, model) => {
  let res
  if (model == 'subject') res = subjectList.value.find((f) => f.value == data)
  else if (model == 'type') res = typeList.value.find((f) => f.value == data)
  else if (model == 'status') res = statusList.value.find((f) => f.value == data)
  else if (model == 'grade') res = gradeList.value.find((f) => f.value == data)
  else if (model == 'is_show') res = showList.value.find((f) => f.value == data)
  return get(res, 'label')
}
// 查看
const toView = (data) => {
  form.value = data
  dialog.value = { type: '1', show: true, title: t('pages.course.dialogTitle') }
}
// 审核
const toExam = (data) => {
  examForm.value = data
  dialog.value = { type: '2', show: true, title: t('pages.course.examDialogTitle') }
}
// 审核保存
const toExamSave = async () => {
  const data = cloneDeep(examForm.value)
  delete data.teacher_name
  delete data.teacher_phone
  let res = await store.update(data)
  if ($checkRes(res, true)) {
    search({ skip: 0, limit })
    toClose()
  }
}
// 删除
const toDelete = async (data) => {
  const res = await store.del(data._id)
  if ($checkRes(res, true)) {
    search({ skip: 0, limit })
  }
}
// 重置
const toReset = async () => {
  searchForm.value = {}
  await search({ skip, limit })
}
const toClose = () => {
  form.value = {}
  dialog.value = { show: false }
}
// 保存
const toSave = async () => {
  const data = cloneDeep(form.value)
  delete data.teacher_name
  delete data.teacher_phone
  let res = await store.update(data)
  if ($checkRes(res, true)) {
    search({ skip: 0, limit })
    toClose()
  }
}
</script>
<style scoped lang="scss">
.images {
  width: 100px;
  height: 100px;
  margin: 0 1vw 0 0;
}
</style>
