<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 TextbookChapterService extends BasicService {
  async sync(): Promise<any> {
    return await get<any>('/xkw/textbookCatalog/sync')
  }

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

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

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

const textbookChapterService = new TextbookChapterService()

const gridRef = ref()

const textbookOptions = ref<any[]>([])
const textbookMap = ref<Record<string, string>>({})

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

function syncData() {
  hud.confirmLoad('确定要同步教材章节数据吗？', {
    option: '同步',
    hideSuccess: false,
    task: async () => {
      await textbookChapterService.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: '',
      ordinal: 0,
      parentId: '',
      textbookId: '',
      status: 0,
    },
    items: [
      {
        field: 'name',
        title: '章节名称',
        required: true,
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'textbookId',
        title: '所属教材',
        span: 12,
        itemRender: {
          name: mj_form_select,
          options: computed(() => textbookOptions.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', 'ordinal', 'parentId', 'textbookId', 'status'],
  updateFields: ['id', 'name', 'ordinal', 'parentId', 'textbookId', 'status'],
}

const grid: MjGridProps = {
  columns: [
    { type: 'seq' },
    { field: 'name', title: '章节名称' },
    {
      field: 'textbookId',
      title: '所属教材',
      width: 160,
      slots: { default: 'textbookId_default' },
    },
    { field: 'parentId', title: '父级ID', width: 120 },
    { field: 'ordinal', title: '排序', width: 80 },
    {
      field: 'status',
      title: '状态',
      width: 120,
      slots: { default: 'status_default' },
    },
  ],
}
</script>

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

      <template #textbookId_default="{ row }">
        {{ textbookMap[row.textbookId] || row.textbookId }}
      </template>

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

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