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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetProjects.BindCompany>
      {...MyModalFormProps.props}
      title={`换绑${props.title}`}
      trigger={<MyButtons.Default title="换绑" type="primary" size="small" />}
      wrapperCol={{ span: 24 }}
      width="500px"
      request={() =>
        Promise.resolve({
          ...props.item,
        })
      }
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetProjects.BindCompany({
          ...values,
          projects_id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '关联机构修改成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Companies({
          key: 'companies_id',
          title: '所属机构',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
        }),
      ]}
    />
  );
}
