import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersTypeEnum } 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
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields();
        }
      }}
      onFinish={async (values: any) =>
        Apis.Asset.AssetProjectAdmins.BatchStore(values)
          .then(() => {
            props.reload?.();
            message.success('提交成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          dataIndex: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
          },
          required: true,
        }),
        Selects?.Employees({
          title: '选择员工',
          dataIndex: 'company_employees_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
          },
          required: true,
        }),
        MyFormItems.EnumCheckbox({
          key: 'types',
          title: '工单类型',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersTypeEnum,
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            options: Object.entries(HouseWorkOrdersTypeEnum).map(
              ([key, value]) => ({
                label: value.text,
                value: value.value,
              }),
            ),
            optionType: 'default',
            style: {
              display: 'grid',
              gridTemplateColumns: 'repeat(3, 1fr)',
              gap: '8px',
            },
          },
        }),
      ]}
    />
  );
}
