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 Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyReceiptAccounts.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.CompanyReceiptAccounts.Store({
          ...values,
          is_default: 0,
          pay_channel: values?.merchant_id ? 'TongLian' : 'BankTransfer',
        })
          .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: 'company_name',
          title: '收款账户',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'company_bank',
          title: '开户行',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'company_account',
          title: '收款账号',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'boolean',
          title: '是否开通【通联支付】',
          valueType: 'switch',
          colProps: { span: 24 },
        },
        {
          name: ['boolean'],
          valueType: 'dependency',
          columns: ({ boolean }: any) => {
            return boolean
              ? [
                  {
                    key: 'merchant_id',
                    title: '通联商户号',
                    colProps: { span: 24 },
                    fieldProps: {
                      placeholder: '如已开通，必须输入通联商户号',
                    },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [];
          },
        },
      ]}
    />
  );
}
