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

export default function OrganizationHasProjects(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Company.OrganizationHasProjects.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Create title={`${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.OrganizationHasProjects.BatchStore({
          ...values,
          organizations_id:
            values?.organizations_id?.[values.organizations_id.length - 1],
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.OrganizationsTree({
          title: '选择组织',
          key: 'organizations_id',
          colProps: { span: 24 },
          fieldProps: {
            showSearch: true,
          },
          formItemProps: { ...rulesHelper.text },
        }),
        Selects?.AssetProjects({
          key: 'asset_projects_ids',
          title: '选择项目',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            showSearch: true,
            mode: 'multiple',
          },
        }),
      ]}
    />
  );
}
