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.Company.Organizations.Update>
      {...MyModalFormProps.props}
      title={`${props.title}调整`}
      form={form}
      trigger={<MyButtons.Default title="调整" type="primary" size="small" />}
      wrapperCol={{ span: 24 }}
      width="500px"
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.Organizations.Update({
          ...values,
          id: props.item?.id ?? 0,
          name: props.item?.name,
          type: props.item?.type,
          parent_id:
            values?.parent_id?.[values.parent_id.length - 1] ||
            props.item?.parent_id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.OrganizationsTree({
          title: '请选择新的【上级组织】',
          key: 'parent_id',
          params: { companies_id: props?.item?.companies_id },
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
      ]}
    />
  );
}
