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

import { 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_switch,
} from '#/components/mj/mj-table'
import hud from '#/utils/hud'

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

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

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

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

const courseKnowledgeService = new CourseKnowledgeService()

const gridRef = ref()

function syncData() {
  hud.confirmLoad('确定要同步课程知识点数据吗？', {
    option: '同步',
    hideSuccess: false,
    task: async () => {
      await courseKnowledgeService.sync()
      gridRef.value?.reload()
    },
  })
}

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: '',
      courseId: '',
      depth: '',
      forLite: '',
      parentId: '',
      rootId: '',
      status: 0,
    },
    items: [
      {
        field: 'name',
        title: '知识点名称',
        required: true,
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'courseId',
        title: '课程ID',
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'depth',
        title: '节点深度',
        span: 12,
        itemRender: {
          name: mj_form_input,
          props: { type: 'number' },
        },
      },
      {
        field: 'forLite',
        title: '适用于精简版',
        span: 12,
        itemRender: {
          name: mj_form_switch,
          props: {
            openValue: 1,
            closeValue: 0,
            openLabel: '是',
            closeLabel: '否',
          },
        },
      },
      {
        field: 'parentId',
        title: '父级ID',
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'rootId',
        title: '根节点ID',
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'status',
        title: '状态',
        span: 12,
        itemRender: {
          name: mj_form_switch,
          props: {
            openValue: 0,
            closeValue: 1,
            openLabel: '启用',
            closeLabel: '禁用',
          },
        },
      },
    ],
  },
  addFields: [
    'name',
    'courseId',
    'depth',
    'forLite',
    'parentId',
    'rootId',
    'status',
  ],
  updateFields: [
    'id',
    'name',
    'courseId',
    'depth',
    'forLite',
    'parentId',
    'rootId',
    'status',
  ],
}

const grid: MjGridProps = {
  columns: [
    { type: 'seq' },
    { field: 'name', title: '知识点名称' },
    { field: 'courseId', title: '课程ID', width: 120 },
    { field: 'depth', title: '节点深度', width: 100 },
    {
      field: 'forLite',
      title: '精简版',
      width: 100,
      slots: { default: 'forLite_default' },
    },
    { field: 'parentId', title: '父级ID', width: 120 },
    { field: 'rootId', title: '根节点ID', width: 120 },
    {
      field: 'status',
      title: '状态',
      width: 120,
      slots: { default: 'status_default' },
    },
  ],
}
</script>

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

      <template #forLite_default="{ row }">
        <el-tag :type="row.forLite ? 'success' : 'info'">
          {{ row.forLite ? '是' : '否' }}
        </el-tag>
      </template>

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

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