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 Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyPositions.Store>
      {...MyModalFormProps.props}
      title={`添加岗位`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Create title={`岗位`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyPositions.Store({
          ...values,
          companies_id: values?.companies_id || props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '选择机构',
                colProps: { span: 24 },
                required: true,
              }),
            ]),
        {
          key: 'name',
          title: '岗位名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'code',
          title: '岗位编号',
          colProps: { span: 24 },
          // formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'remark',
          title: '岗位说明',
          colProps: { span: 24 },
          valueType: 'textarea',
        },
      ]}
    />
  );
}
