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

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

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

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

  const sexOptions = [
    {
      label: '男',
      value: '1',
    },
    {
      label: '女',
      value: '2',
    },
  ]

  const accountTypeOptions = [
    {
      label: '导流',
      value: '1',
    },
    {
      label: '新开户体验',
      value: '2',
    },
  ]

  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 addItems = obj.joinObjs<any>(
    {
      required: true,
      field: 'name',
      title: '姓名',
      span: 12,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      field: 'phone',
      rules: rule_phone,
      title: '手机号',
      span: 12,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      title: '辅助',
      span: 12,
      slots: { default: 'form_tool' },
    },
    {
      field: 'account',
      title: '账号',
      span: 12,
      pattern: String.raw`^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$`,
      message: '账号必须由英文字母和数字组成',
      itemRender: {
        name: mj_form_text,
        props: {
          placeholder: `初始密码为${defaultPwd}`,
        },
      },
    },
    {
      required: true,
      field: 'accountType',
      title: '账号类型',
      span: 12,
      itemRender: {
        name: 'VxeRadioGroup',
        options: accountTypeOptions,
      },
    },
    {
      required: true,
      field: 'sex',
      title: '性别',
      span: 12,
      itemRender: {
        name: 'VxeRadioGroup',
        options: sexOptions,
      },
    },
    {
      field: 'year',
      title: '出生年份',
      validator: yearValidator,
      span: 12,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      field: 'gradeId',
      title: '年级',
      span: 12,
      itemRender: {
        name: mj_form_select,
        options: grades,
      },
    },
    userInfo.value.isOnlyStaff || {
      required: true,
      field: 'schoolName',
      title: '学校',
      span: 24,
      itemRender: {
        name: mj_form_text,
        props: {
          linkedRender: schoolRender,
        },
      },
    },
    {
      field: 'classId',
      title: '班级',
      span: 12,
      itemRender: classRender,
    },
    {
      field: 'userRemark',
      title: '备注',
      span: 12,
      itemRender: {
        name: mj_form_input,
      },
    },
  )

  const updateItems = obj.joinObjs<any>(
    {
      required: true,
      field: 'name',
      title: '姓名',
      span: 24,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      required: true,
      field: 'nickName',
      title: '昵称',
      span: 24,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      field: 'year',
      title: '出生年份',
      validator: yearValidator,
      span: 12,
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      field: 'gradeId',
      title: '年级',
      span: 12,
      itemRender: {
        name: mj_form_select,
        options: grades,
      },
    },
    {
      field: 'userRemark',
      title: '备注',
      span: 24,
      itemRender: {
        name: mj_form_input,
      },
    },
  )

  // 编辑表单
  const editForm = {
    modal: {
      width: 860,
    },
    addForm: {
      items: addItems,
    },
    updateForm: {
      items: updateItems,
    },
    form: {
      titleWidth: 100,
      data: {},
    },
    addFields: [
      'phone',
      'sex',
      'name',
      'schoolId',
      'account',
      'classId',
      'userRemark',
      'accountType',
      'gradeId',
      'birthday',
    ],
    updateFields: [
      'id',
      'name',
      'nickName',
      'gradeId',
      'birthday',
      'userRemark',
    ],
    beforeSaveOrUpdate(formData: any) {
      if (!formData.id && userInfo.value.isOnlyStaff) {
        // 添加
        formData.schoolId = userInfo.value.schoolId
      }
      // 转换为yyyy-MM-dd格式
      if (formData.year) {
        formData.birthday = `${formData.year}-01-01 00:00:00`
      }
      return formData
    },
  } as MjEditFormProps

  return {
    editForm,
  }
}
