import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
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.CompanySupplier.CompanySuppliers.Update>
      {...MyModalFormProps.props}
      title={`黑名单`}
      trigger={
        <MyButtons.Default
          title="拉黑"
          disabled={props?.item?.is_blacklisted === 1}
        />
      }
      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) {
          const formValues = {
            ...props.item,
          };
          form.setFieldsValue(formValues);
        }
      }}
      onFinish={async (values: any) =>
        Apis.CompanySupplier.CompanySuppliers.Update({
          ...values,
          is_blacklisted: values.is_blacklisted ? 1 : 0,
          id: props.item?.id ?? 0,
          counterparty: props.item?.counterparty ?? 0,
          supplier_type: props.item?.supplier_type ?? 0,
          name: props.item?.name ?? '',
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'is_blacklisted',
          title: '是否拉黑',
          valueType: 'switch',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.boolean },
        },
        {
          key: 'reason',
          title: '拉入原因',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
