import { DictValueEnumObj } from '@/components/DictTag';
import KFModal from '@/components/KFModal';
import {
  ProForm,
  ProFormDigit,
  ProFormRadio,
  ProFormText,
  ProFormTreeSelect,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form } from 'antd';
import { DataNode } from 'antd/es/tree';
import React, { useEffect } from 'react';
export type DeptFormData = Record<string, unknown> & Partial<API.System.Dept>;

export type DeptFormProps = {
  onCancel: (flag?: boolean, formVals?: DeptFormData) => void;
  onSubmit: (values: DeptFormData) => Promise<void>;
  open: boolean;
  values: Partial<API.System.Dept>;
  deptTree: DataNode[];
  statusOptions: DictValueEnumObj;
};

const DeptForm: React.FC<DeptFormProps> = (props) => {
  const [form] = Form.useForm();
  const { statusOptions, deptTree } = props;
  const formLayout = {
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
  };

  const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
  };

  useEffect(() => {
    form.resetFields();
    form.setFieldsValue({
      deptId: props.values.deptId,
      parentId: props.values.parentId,
      ancestors: props.values.ancestors,
      deptName: props.values.deptName,
      orderNum: props.values.orderNum ?? 1,
      leader: props.values.leader,
      phone: props.values.phone,
      email: props.values.email,
      status: props.values.status ?? Object.keys(statusOptions)[0],
      delFlag: props.values.delFlag,
      createBy: props.values.createBy,
      createTime: props.values.createTime,
      updateBy: props.values.updateBy,
      updateTime: props.values.updateTime,
    });
  }, [form, props, statusOptions]);

  const intl = useIntl();
  const handleOk = () => {
    form.submit();
  };
  const handleCancel = () => {
    props.onCancel();
  };
  const handleFinish = async (values: Record<string, any>) => {
    props.onSubmit(values as DeptFormData);
  };

  return (
    <KFModal
      width={680}
      title={intl.formatMessage({
        id: 'system.dept.title',
        defaultMessage: '编辑部门',
      })}
      open={props.open}
      forceRender
      destroyOnClose
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <ProForm
        form={form}
        grid={true}
        submitter={false}
        layout="horizontal"
        onFinish={handleFinish}
        {...formLayout}
        size="large"
      >
        <ProFormDigit
          name="deptId"
          label={intl.formatMessage({
            id: 'system.dept.dept_id',
            defaultMessage: '部门id',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入部门id"
          disabled
          hidden={true}
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入部门id！" defaultMessage="请输入部门id！" />,
            },
          ]}
        />
        <ProFormTreeSelect
          name="parentId"
          label={intl.formatMessage({
            id: 'system.dept.parent_dept',
            defaultMessage: '上级部门:',
          })}
          request={async () => {
            return deptTree;
          }}
          {...formItemLayout}
          placeholder="请选择上级部门"
          rules={[
            {
              required: true,
              message: <FormattedMessage id="请输入用户昵称！" defaultMessage="请选择上级部门!" />,
            },
          ]}
        />
        <ProFormText
          name="deptName"
          label={intl.formatMessage({
            id: 'system.dept.dept_name',
            defaultMessage: '部门名称',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入部门名称"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入部门名称！" defaultMessage="请输入部门名称！" />,
            },
          ]}
        />
        <ProFormDigit
          name="orderNum"
          label={intl.formatMessage({
            id: 'system.dept.order_num',
            defaultMessage: '显示顺序',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入显示顺序"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入显示顺序！" defaultMessage="请输入显示顺序！" />,
            },
          ]}
        />
        <ProFormText
          name="leader"
          label={intl.formatMessage({
            id: 'system.dept.leader',
            defaultMessage: '负责人',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入负责人"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入负责人！" defaultMessage="请输入负责人！" />,
            },
          ]}
        />
        <ProFormText
          name="phone"
          label={intl.formatMessage({
            id: 'system.dept.phone',
            defaultMessage: '联系电话',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入联系电话"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入联系电话！" defaultMessage="请输入联系电话！" />,
            },
          ]}
        />
        <ProFormText
          name="email"
          label={intl.formatMessage({
            id: 'system.dept.email',
            defaultMessage: '邮箱',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入邮箱"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入邮箱！" defaultMessage="请输入邮箱！" />,
            },
            {
              type: 'email',
              message: (
                <FormattedMessage
                  id="请输入正确的邮箱格式！"
                  defaultMessage="请输入正确的邮箱格式！"
                />
              ),
            },
          ]}
        />
        <ProFormRadio.Group
          valueEnum={statusOptions}
          name="status"
          label={intl.formatMessage({
            id: 'system.dept.status',
            defaultMessage: '部门状态',
          })}
          colProps={{ md: 12, xl: 12 }}
          placeholder="请输入部门状态"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请输入部门状态！" defaultMessage="请输入部门状态！" />,
            },
          ]}
        />
      </ProForm>
    </KFModal>
  );
};

export default DeptForm;
