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

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

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

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

const textbookVersionService = new TextbookVersionService()

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 textbookVersionService.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: '',
      year: '',
      status: 0,
    },
    items: [
      {
        field: 'name',
        title: '版本名称',
        span: 12,
        itemRender: { name: mj_form_input },
      },
      {
        field: 'courseId',
        title: '学科',
        span: 12,
        itemRender: {
          name: mj_form_select,
          options: computed(() => subjectOptions.value) as any,
          props: { clearable: true },
        },
      },
      {
        field: 'year',
        title: '年份',
        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', 'year', 'status'],
  updateFields: ['id', 'name', 'courseId', 'year', 'status'],
}

const grid: MjGridProps = {
  columns: [
    { type: 'seq' },
    { field: 'name', title: '版本名称' },
    {
      field: 'courseId',
      title: '学科',
      width: 120,
      slots: { default: 'courseId_default' },
    },
    { field: 'year', title: '年份', width: 120 },
    {
      field: 'status',
      title: '状态',
      width: 120,
      slots: { default: 'status_default' },
    },
    // {
    //   title: '操作',
    //   width: 200,
    //   slots: { default: 'operation' },
    //   fixed: 'right',
    // },
  ],
}
</script>

<template>
  <div class="textbook-version g-container">
    <MjTable
      ref="gridRef"
      :grid="grid"
      :service="textbookVersionService"
      :search-form="searchForm"
      :edit-form="editForm"
    >
      <template #toolbarButtonsExtra>
        <el-button type="warning" :icon="Refresh" @click="syncData">
          同步数据
        </el-button>
      </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>
.textbook-version {
  height: 100% !important;
}
</style>
