import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanySuppliersGradeEnum } 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.CompanySupplier.CompanySuppliers.Update>
      {...MyModalFormProps.props}
      title={`供应商评级`}
      trigger={
        <MyButtons.Default
          title="评级"
          type="primary"
          size={props?.item?.size || 'small'}
        />
      }
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.resetFields(); // 清空表单数据
          const formValues = {
            ...props.item,
          };
          form.setFieldsValue(formValues);
        }
      }}
      onFinish={async (values: any) =>
        Apis.CompanySupplier.CompanySupplierGrades.Store({
          ...values,
          company_suppliers_id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'grade',
          title: '等级评定',
          valueEnum: CompanySuppliersGradeEnum,
          required: true,
          colProps: { span: 24 },
        }),
        {
          key: 'grade_remark',
          title: '评级说明',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'grade_files',
          title: '评级文件',
          // tooltip: '限传 1 份，仅支持docx,doc,pdf格式',
          uploadType: 'file',
          // accept: '.docx,.doc,.pdf',
          colProps: { span: 24 },
          // max: 1,
          formItemProps: { ...rulesHelper.array },
        }),
      ]}
    />
  );
}
