<script setup lang="ts">
import type {
  MjEditFormProps,
  MjFormProps,
  MjGridProps,
} from '#/components/mj/mj-table'

import { computed, onMounted, ref } from 'vue'

import { Refresh } from '@element-plus/icons-vue'

import { get } from '#/api/http'
import { BasicService } from '#/api/service/basic'
import MjTable, {
  mj_form_input,
  mj_form_select,
  mj_form_switch,
} from '#/components/mj/mj-table'
import hud from '#/utils/hud'

class QuestionTypeService extends BasicService {
  async sync(): Promise<any> {
    return await get<any>('/xkw/questionType/sync')
  }

  protected findPageUrl(): string {
    return '/xkw/questionType/list'
  }

  protected removeUrl(): string {
    return '/xkw/questionType/batch/delete'
  }

  protected saveOrUpdateUrl(): string {
    return '/xkw/questionType/saveOrUpdate'
  }
}

const questionTypeService = new QuestionTypeService()

const gridRef = ref()

const subjectOptions = ref<any[]>([])
const subjectMap = ref<Record<string, string>>({})

onMounted(async () => {
  const res = await get<any>('/xkw/subject/list', { current: 1, size: 999 })
  if (res?.rows) {
    const map: Record<string, string> = {}
    subjectOptions.value = res.rows.map((item: any) => {
      map[item.id] = item.name
      return { value: item.id, label: item.name }
    })
    subjectMap.value = map
  }
})

function syncData() {
  hud.confirmLoad('确定要同步试题类型数据吗？', {
    option: '同步',
    hideSuccess: false,
    task: async () => {
      await questionTypeService.sync()
      gridRef.value?.reload()
    },
  })
}

const objectiveOptions = [
  { value: 0, label: '主观题' },
  { value: 1, label: '客观题' },
]
const objectiveMap: Record<number, string> = { 0: '主观题', 1: '客观题' }

const searchForm: MjFormProps = {
  items: [
    {
      field: 'keyword',
      title: '关键字',
      itemRender: {
        name: mj_form_input,
        props: { clearable: true, placeholder: '请输入关键字' },
      },
    },
  ],
}

const editForm: MjEditFormProps = {
  modal: { width: 600 },
  form: {
    titleWidth: 100,
    data: {
      name: '',
      objective: 0,
      ordinal: '',
      parentId: '',
      courseId: '',
      status: 0,
    },
    items: [
      {
        field: 'name',
        title: '类型名称',
        required: true,
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'objective',
        title: '客观题标志',
        span: 12,
        itemRender: {
          name: mj_form_select,
          options: objectiveOptions,
          props: { clearable: true },
        },
      },
      {
        field: 'courseId',
        title: '学科',
        span: 12,
        itemRender: {
          name: mj_form_select,
          options: computed(() => subjectOptions.value) as any,
          props: { clearable: true },
        },
      },
      {
        field: 'parentId',
        title: '父级ID',
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'ordinal',
        title: '排序',
        span: 12,
        itemRender: {
          name: mj_form_input,
          props: { type: 'number' },
        },
      },
      {
        field: 'status',
        title: '状态',
        span: 12,
        itemRender: {
          name: mj_form_switch,
          props: {
            openValue: 0,
            closeValue: 1,
            openLabel: '正常',
            closeLabel: '禁用',
          },
        },
      },
    ],
  },
  addFields: ['name', 'objective', 'ordinal', 'parentId', 'courseId', 'status'],
  updateFields: [
    'id',
    'name',
    'objective',
    'ordinal',
    'parentId',
    'courseId',
    'status',
  ],
}

const grid: MjGridProps = {
  columns: [
    { type: 'seq' },
    { field: 'name', title: '类型名称' },
    {
      field: 'objective',
      title: '客观题标志',
      width: 120,
      slots: { default: 'objective_default' },
    },
    {
      field: 'courseId',
      title: '学科',
      width: 120,
      slots: { default: 'courseId_default' },
    },
    { field: 'parentId', title: '父级ID', width: 120 },
    { field: 'ordinal', title: '排序', width: 80 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      slots: { default: 'status_default' },
    },
  ],
}
</script>

<template>
  <div class="question-type g-container">
    <MjTable
      ref="gridRef"
      :grid="grid"
      :service="questionTypeService"
      :search-form="searchForm"
      :edit-form="editForm"
    >
      <template #toolbarButtonsExtra>
        <el-button type="warning" :icon="Refresh" @click="syncData">
          同步数据
        </el-button>
      </template>

      <template #objective_default="{ row }">
        <el-tag :type="row.objective === 1 ? 'primary' : 'warning'">
          {{ objectiveMap[row.objective] || row.objective }}
        </el-tag>
      </template>

      <template #courseId_default="{ row }">
        {{ subjectMap[row.courseId] || row.courseId }}
      </template>

      <template #status_default="{ row }">
        <el-tag :type="row.status === 0 ? 'success' : 'danger'">
          {{ row.status === 0 ? '正常' : '禁用' }}
        </el-tag>
      </template>
    </MjTable>
  </div>
</template>

<style scoped>
.question-type {
  height: 100% !important;
}
</style>
