import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { CompanySealsTypeEnum } 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.CompanySeal.CompanySeals.Update>
      {...MyModalFormProps.props}
      title={`编辑印章`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 18 }}
      align="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.CompanySeal.CompanySeals.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects.Supplier({
          key: 'company_suppliers_id',
          title: '选择企业',
          params: {
            counterparty: 'InternalCompany',
          },
          colProps: { span: 24 },
          required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'type',
          title: '印章类型',
          valueEnum: CompanySealsTypeEnum,
          required: true,
          colProps: { span: 24 },
        }),
        Selects.Employees({
          key: 'company_employees_id',
          title: '保管人',
          colProps: { span: 24 },
          required: true,
        }),
        {
          key: 'use_date',
          title: '启用日期',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            style: { width: '100%' },
          },
        },
        MyFormItems.UploadImages({
          key: 'seal_image',
          title: '印章图片',
          colProps: { span: 24 },
          max: 1,
        }),
        {
          title: '备注',
          key: 'remark',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
