import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';

import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { CompanyEmployeesTypeEnum } from '@/gen/Enums';
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.CompanyEmployees.Update>
      {...MyModalFormProps.props}
      title={`组织调整`}
      trigger={
        <MyButtons.Edit
          title="组织"
          disabled={
            props.item?.type !== CompanyEmployeesTypeEnum.External.value
          }
          type="primary"
        />
      }
      wrapperCol={{ span: 24 }}
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.CompanyEmployees.Update({
          ...values,
          id: props.item?.id ?? 0,
          name: props.item?.name ?? '',
          phone: props.item?.phone ?? '',
          type: props.item?.type ?? '',
          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',
          params: { companies_id: props?.item?.companies_id },
          colProps: { span: 24 },
          fieldProps: {
            showSearch: true,
          },
          formItemProps: { ...rulesHelper.text },
        }),
      ]}
    />
  );
}
