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

import {
  mj_form_input,
  mj_form_phone,
  mj_form_select,
  mj_form_text,
} from '#/components/mj/mj-table'
import { refreshNextRender } from '#/components/mj/mj-table-select'
import { rule_phone } from '#/components/mj/mj-table/fmt'
import { grades, subjects } from '#/store/basic-data'
import obj from '#/utils/obj'

import { useSearchClass } from '../../hooks/search'

export const userEditForm = (data: any) => {
  const {
    roles_options,
    userInfo,
    gridRef,
    schoolClassRender,
    schoolRender,
    defaultPwd,
  } = data

  let classRender = schoolClassRender.classRender
  if (userInfo.value.isOnlyStaff) {
    classRender = useSearchClass({
      getData: () => gridRef.value.getEditFormData(),
      props: userInfo.value.isOnlyStaff
        ? {
            cmpProps: { schoolId: userInfo.value.schoolId, fixedSchool: true },
          }
        : undefined,
    })
  }

  const editForm = {
    modal: {
      width: 900,
    },
    form: {
      items: obj.joinObjs<any>(
        {
          required: true,
          field: 'name',
          title: '姓名',
          span: 12,
          itemRender: {
            name: mj_form_input,
          },
        },
        {
          field: 'phone',
          title: '手机',
          span: 12,
          rules: rule_phone,
          itemRender: {
            name: mj_form_phone,
          },
        },
        {
          title: '辅助',
          span: 12,
          slots: { default: 'form_tool' },
        },
        {
          required: true,
          field: 'account',
          title: '账号',
          span: 12,
          itemRender: {
            name: mj_form_input,
            props: {
              placeholder: `初始密码为${defaultPwd}`,
            },
          },
        },
        {
          required: true,
          field: 'roleId',
          title: '角色',
          span: 12,
          itemRender: {
            name: mj_form_select,
            options: roles_options,
          },
        },
        {
          field: 'gradeId',
          title: '年级',
          span: 12,
          itemRender: {
            name: mj_form_select,
            options: grades,
          },
        },
        {
          field: 'subjectId',
          title: '学科',
          span: 12,
          itemRender: {
            name: mj_form_select,
            options: subjects,
          },
        },
        userInfo.value.isOnlyStaff || {
          required: true,
          field: 'schoolName',
          title: '学校',
          span: 12,
          itemRender: {
            name: mj_form_text,
            props: {
              linkedRender: schoolRender,
            },
          },
        },
        {
          field: 'classId',
          title: '班级',
          span: 12,
          itemRender: classRender,
        },
      ),
      data: {},
    },
    updateFields: [
      'id',
      'name',
      'account',
      'roleId',
      'gradeId',
      'subjectId',
      'classId',
      'schoolId',
      'phone',
    ],
    beforeSaveOrUpdate(formData: any) {
      console.log('提交的参数:', formData)
      if (userInfo.value.isOnlyStaff) {
        formData.schoolId = userInfo.value.schoolId
      }
      if (!formData.id) {
        // 添加
        formData.password = defaultPwd
      }
      return formData
    },
    beforeEditFormShow(data, edit) {
      // 更新
      if (edit) {
        gridRef.value.hideEditFormItems(['schoolName', 'classId'])
        // 刷新一下班级
        refreshNextRender(
          classRender,
          (item: any) => {
            item.schoolId = data.schoolId
            item.schoolName = data.schoolName
          },
          data.schoolId,
        )
        return data
      }
      gridRef.value.showEditFormItems(['schoolName', 'classId'])
      return data
    },
  } as MjEditFormProps

  return {
    editForm,
  }
}
