import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  renderTextHelper,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BillsStatusEnum, HouseOrdersPaymentMethodEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { request } from '@umijs/max';
import { Image as AntImage, Descriptions, Form, message } from 'antd';
import * as React from 'react';

const AuditStatusEnum = {
  Approved: { text: '通过', value: 'Approved' },
  Rejected: { text: '驳回', value: 'Rejected' },
};

export default function AuditPayment(
  props: MyBetaModalFormProps,
): React.ReactElement {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Bill.Bills.AuditPayment>
      {...MyModalFormProps.props}
      title="审核线下收款"
      trigger={
        <MyButtons.Default
          title="审核收款"
          size="small"
          disabled={props.item?.status !== BillsStatusEnum.ToBeConfirmed.value}
        />
      }
      width="600px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: boolean): void => {
        if (open) {
          form.resetFields();
        }
      }}
      onFinish={async (values: any) => {
        if (values.status === 'Approved' && values.serial_number) {
          await request('company/bill/bills/update_serial_number', {
            data: {
              id: props.item?.id ?? 0,
              serial_number: values.serial_number,
            },
          });
        }
        return Apis.Bill.Bills.AuditPayment({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('审核成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '收款信息',
          },
        },
        {
          colProps: { span: 24 },
          renderFormItem: () => (
            <Descriptions
              column={2}
              size="small"
              bordered
              style={{ marginBottom: '12px' }}
            >
              <Descriptions.Item label="账单ID">
                {props.item?.id || '-'}
              </Descriptions.Item>
              <Descriptions.Item label="关联项目">
                {props.item?.asset_project?.name || '-'}
              </Descriptions.Item>
              <Descriptions.Item label="收款金额">
                ¥ {props.item?.bill_payment?.amount || 0} 元
              </Descriptions.Item>
              <Descriptions.Item label="收款方式">
                {renderTextHelper.Tag({
                  Enums: HouseOrdersPaymentMethodEnum,
                  value: props.item?.bill_payment?.payment_method,
                }) || '-'}
              </Descriptions.Item>
              <Descriptions.Item label="收款日期">
                {props.item?.bill_payment?.paid_time?.slice(0, 10) || '-'}
              </Descriptions.Item>
              <Descriptions.Item label="备注">
                {props.item?.bill_payment?.remark || '-'}
              </Descriptions.Item>
              {props.item?.bill_payment?.pay_certificate?.length > 0 && (
                <Descriptions.Item label="收款凭证" span={2}>
                  <AntImage.PreviewGroup>
                    {props.item?.bill_payment?.pay_certificate?.map(
                      (img: any) => (
                        <AntImage
                          key={img.uid}
                          height={30}
                          src={img.url}
                          style={{ marginRight: 12 }}
                        />
                      ),
                    )}
                  </AntImage.PreviewGroup>
                </Descriptions.Item>
              )}
            </Descriptions>
          ),
        },
        MyFormItems.EnumRadio({
          key: 'status',
          title: '审核意见',
          valueEnum: AuditStatusEnum,
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['status'],
          columns: ({ status }) => {
            if (status === 'Approved') {
              return [
                {
                  title: '流水号',
                  valueType: 'text',
                  key: 'serial_number',
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '审核备注',
                  valueType: 'textarea',
                  key: 'rejected_reason',
                },
              ];
            }
            return [
              {
                title: '驳回理由',
                valueType: 'textarea',
                key: 'rejected_reason',
                formItemProps: { ...rulesHelper.text },
              },
            ];
          },
        },
      ]}
    />
  );
}
