import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { EmergencyTeamMembersPositionEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Emergency.EmergencyEventTeams.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      width="800px"
      wrapperCol={{ span: 24 }}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Emergency.EmergencyEventTeams.Store(values)
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          title: '名称',
          key: 'name',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        Selects?.Employees({
          key: 'company_employees_id',
          title: '组长',
          colProps: { span: 12 },
          required: true,
        }),
        {
          title: '是否启用',
          key: 'is_enabled',
          valueType: 'switch',
        },
        {
          valueType: 'formList',
          dataIndex: 'members',
          // title: '团队成员',
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                Selects?.Employees({
                  key: 'company_employees_id',
                  title: '成员',
                  colProps: { span: 12 },
                  required: true,
                }),
                MyFormItems.EnumSelect({
                  key: 'position',
                  title: '身份',
                  valueEnum: EmergencyTeamMembersPositionEnum,
                  colProps: { span: 12 },
                  formItemProps: { ...rulesHelper.text },
                }),
              ],
            },
          ],
        },
      ]}
    />
  );
}
