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

import {
  parsePrepareParams,
  useItemRender,
} from '#/components/mj/mj-table-select'

import ClassMgr from '../class-mgr/home.vue'
import SelectStudent from '../common/select-student/index.vue'
import SelectUser from '../common/select-user/index.vue'
import SchoolMgr from '../school-mgr/home.vue'

export const useSearchSchool = (params?: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: SchoolMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      if (!item) return
      item.schoolId = value
      item.schoolName = label
    },
    props: {
      modal: { title: '请选择学校' },
      ...params?.props,
    },
  })
}

export const useSearchClass = (params: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: ClassMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      item.classId = value
      item.className = label
    },
    props: {
      modal: { title: '请选择班级' },
      ...params?.props,
    },
  })
}

export const useSearchSchoolClass = (params: ItemRenderParams) => {
  parsePrepareParams(params)
  const isChangeArray = Array.isArray(params.change)
  // 搜索表单
  const classRender = useSearchClass({
    getData: params.getData,
    change: isChangeArray ? (params.change as any)?.[1] : params.change,
    props: {
      disabled: true,
      cmpProps: {
        fixedSchool: true,
      },
    },
  })
  const schoolRender = useSearchSchool({
    getData: params.getData,
    change: isChangeArray ? (params.change as any)?.[0] : params.change,
    nextRender: classRender,
  })
  return {
    schoolRender,
    classRender,
  }
}

export const useSearchUser = (params: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: SelectUser,
    setData: (item: any, _: any, value: any, label: any) => {
      item.userId = value
      item.userName = label
    },
    props: {
      modal: { title: '请选择用户' },
      ...params?.props,
    },
  })
}

export const useSearchStudent = (params: ItemRenderParams) => {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: SelectStudent,
    setData: (item: any, _: any, value: any, label: any) => {
      item.studentId = value
      item.studentName = label
    },
    props: {
      modal: { title: '请选择学员' },
      ...params?.props,
    },
  })
}
