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.Company.CompanyReceiptAccounts.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="500px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
          form.setFieldValue('boolean', props.item?.pay_channel === 'TongLian');
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.CompanyReceiptAccounts.Update({
          ...values,
          companies_id: props?.item?.companies_id,
          id: props.item?.id ?? 0,
          is_default: 0,
          pay_channel: values?.merchant_id ? 'TongLian' : 'BankTransfer',
          merchant_id: values?.boolean ? values?.merchant_id : '',
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          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 },
          onChange: () => {
            form.setFieldValue('merchant_id', undefined);
          },
        },
        {
          name: ['boolean'],
          valueType: 'dependency',
          columns: ({ boolean }: any) => {
            return boolean
              ? [
                  {
                    key: 'merchant_id',
                    title: '通联商户号',
                    colProps: { span: 24 },
                    fieldProps: {
                      placeholder: '如已开通，必须输入通联商户号',
                    },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [];
          },
        },
        // MyFormItems.EnumRadio({
        //   key: 'pay_channel',
        //   title: '收款渠道',
        //   colProps: { span: 24 },
        //   valueEnum: CompanyReceiptAccountsPayChannelEnum,
        //   required: true,
        // }),
        // {
        //   name: ['pay_channel'],
        //   valueType: 'dependency',
        //   columns: ({ pay_channel }: any) => {
        //     return pay_channel ===
        //       CompanyReceiptAccountsPayChannelEnum.TongLian.value
        //       ? [
        //           {
        //             key: 'merchant_id',
        //             title: '商户ID',
        //             colProps: { span: 24 },
        //             formItemProps: { ...rulesHelper.text },
        //           },
        //         ]
        //       : [];
        //   },
        // },
        // {
        //   key: 'is_default',
        //   title: '是否设为默认账号',
        //   valueType: 'switch',
        //   colProps: { span: 24 },
        // },
      ]}
    />
  );
}
