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.OrganizationProjects.Store>
      {...MyModalFormProps.props}
      title={`${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Default title="组织" type="primary" />}
      key={new Date().getTime().toString()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            asset_projects_id: props.item?.id,
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.OrganizationProjects.Store({
          ...values,
          asset_projects_id: props.item?.id,
          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 },
        }),
      ]}
    />
  );
}
