import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractBillsCostTypeEnum,
  HouseOrdersPaymentMethodEnum,
} from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function PayBills(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractBillPayments.Store>
      {...MyModalFormProps.props}
      title={props.title}
      trigger={
        <MyButtons.Default
          type="primary"
          disabled={
            props?.item?.payment_status === 'Paid' ||
            (props?.item?.audit_status !== 'Approved' &&
              props?.item?.cost_type === 'Expense')
          }
          title={props.title}
          size="small"
        />
      }
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      key={new Date().getTime()}
      width="650px"
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.setFieldsValue({
            ...props.item,
            remark: '',
            paid_time: undefined,
            cost_type: props.item?.cost_type,
            pay_certificate: [],
            accept_account_number: '',
            accept_account_name:
              props.item?.cost_type === ContractBillsCostTypeEnum.Income.value
                ? props.item?.payee
                : props.item?.payer,

            accept_serial_number: '',
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Contract.ContractBillPayments.Store({
          ...values,
          contract_bills_id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('提交成功，等待审核！');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'cost_type',
          title: '费用类型',
          valueEnum: ContractBillsCostTypeEnum,
          fieldProps: {
            disabled: true,
          },
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          name: ['cost_type'],
          valueType: 'dependency',
          columns: ({ cost_type }) => {
            return cost_type === ContractBillsCostTypeEnum.Income.value
              ? [
                  {
                    title: '收款金额',
                    key: 'amount',
                    colProps: { span: 24 },
                    fieldProps: {
                      suffix: '元',
                      disabled: true,
                      style: {
                        width: '100%',
                      },
                    },
                    formItemProps: { ...rulesHelper.number },
                  },
                  MyFormItems.EnumRadio({
                    key: 'payment_method',
                    title: '收款方式',
                    colProps: { span: 24 },
                    valueEnum: () => {
                      const obj: Record<string, any> = JSON.parse(
                        JSON.stringify(HouseOrdersPaymentMethodEnum),
                      );
                      delete obj.WeChat;
                      delete obj.Alipay;
                      delete obj.TongLian;
                      delete obj.Prepayment;
                      return obj;
                    },
                    required: true,
                  }),
                  {
                    title: '收款日期',
                    key: 'paid_time',
                    colProps: { span: 24 },
                    valueType: 'date',
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: {
                      style: {
                        width: '100%',
                      },
                    },
                  },
                  {
                    title: '收款账户',
                    key: 'accept_account_name',
                    colProps: { span: 24 },
                  },
                  {
                    title: '收款银行',
                    key: 'accept_bank_name',
                    colProps: { span: 24 },
                  },
                  {
                    title: '收款账号',
                    key: 'accept_account_number',
                    colProps: { span: 24 },
                  },
                  {
                    title: '收款流水号',
                    key: 'accept_serial_number',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                  MyFormItems.UploadImages({
                    key: 'pay_certificate',
                    title: '支付凭证',
                    uploadType: 'file',
                    colProps: { span: 24 },
                    max: 100,
                  }),
                ]
              : [
                  {
                    title: '支付金额',
                    key: 'amount',
                    colProps: { span: 24 },
                    fieldProps: {
                      suffix: '元',
                      disabled: true,
                      style: {
                        width: '100%',
                      },
                    },
                    formItemProps: { ...rulesHelper.number },
                  },
                  MyFormItems.EnumRadio({
                    key: 'payment_method',
                    title: '支付方式',
                    colProps: { span: 24 },
                    valueEnum: () => {
                      const obj: Record<string, any> = JSON.parse(
                        JSON.stringify(HouseOrdersPaymentMethodEnum),
                      );
                      delete obj.WeChat;
                      delete obj.Alipay;
                      delete obj.TongLian;
                      delete obj.Prepayment;
                      delete obj.POS;
                      delete obj.Cash;
                      return obj;
                    },
                    required: true,
                  }),
                  {
                    title: '支付日期',
                    key: 'paid_time',
                    colProps: { span: 24 },
                    valueType: 'date',
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: {
                      style: {
                        width: '100%',
                      },
                    },
                  },
                  {
                    title: '付款账户',
                    key: 'accept_account_name',
                    colProps: { span: 24 },
                  },
                  {
                    title: '付款银行',
                    key: 'accept_bank_name',
                    colProps: { span: 24 },
                  },
                  {
                    title: '付款账号',
                    key: 'accept_account_number',
                    colProps: { span: 24 },
                  },
                  {
                    title: '支付流水号',
                    key: 'accept_serial_number',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                  MyFormItems.UploadImages({
                    key: 'pay_certificate',
                    title: '支付凭证',
                    uploadType: 'file',
                    colProps: { span: 24 },
                    max: 100,
                  }),
                ];
          },
        },
      ]}
    />
  );
}
