import type { ItemRenderParams } from '#/components/mj/mj-table-select'

import {
  parsePrepareParams,
  useItemRender,
} from '#/components/mj/mj-table-select'
import { useSearchSchool } from '#/views/org/hooks/search'

import PaperMgr from '../paper-mgr/home.vue'
import SchoolBook from '../school-book/home.vue'

export const useSearchPaper = (params?: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: PaperMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      item.paperId = value
      item.paperName = label
    },
    props: {
      modal: { title: '请选择试卷' },
      ...params?.props,
    },
  })
}

export const useSearchSchoolBook = (params?: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: SchoolBook,
    setData: (item: any, _: any, value: any, label: any) => {
      item.textBookId = value
      item.textBookName = label
    },
    props: {
      modal: { title: '请选择学校教材' },
      title: (item: any) => item.textbookName || item.name,
      ...params?.props,
    },
  })
}

export const useSearchSchoolAndBook = (
  schoolParams: ItemRenderParams,
  bookParams: ItemRenderParams,
) => {
  // 搜索表单
  const bookRender = useSearchSchoolBook({
    ...bookParams,
    props: {
      ...bookParams?.props,
      disabled: true,
      cmpProps: {
        ...bookParams?.props?.cmpProps,
        fixedSchool: true,
      },
    },
  })
  // 学校
  const schoolRender = useSearchSchool({
    nextRender: bookRender,
    ...schoolParams,
  })
  return {
    schoolRender,
    bookRender,
  }
}

export const useSearchSchoolAndBookAndPaper = (
  schoolParams: ItemRenderParams,
  bookParams: ItemRenderParams,
  paperParams: ItemRenderParams,
) => {
  const paperRender = useSearchPaper({
    ...paperParams,
    props: {
      ...paperParams?.props,
      disabled: true,
      cmpProps: {
        ...paperParams?.props?.cmpProps,
        fixedTextBook: true,
        fixedSchool: true,
      },
    },
  })
  // 表单
  const bookRender = useSearchSchoolBook({
    ...bookParams,
    props: {
      ...bookParams?.props,
      disabled: true,
      cmpProps: {
        ...bookParams?.props?.cmpProps,
        fixedSchool: true,
      },
    },
    nextRender: paperRender,
  })
  // 学校
  const schoolRender = useSearchSchool({
    ...schoolParams,
    nextRender: [bookRender, paperRender],
  })
  return {
    schoolRender,
    bookRender,
    paperRender,
  }
}
