import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyFormItems,
  MyModalFormProps,
  MyProTableProps,
  renderTextHelper,
  rulesHelper,
} from '@/common';

import { Apis } from '@/gen/Apis';
import {
  HouseBillsTypeEnum,
  HouseOrdersAuditStatusEnum,
  HouseOrdersPaymentMethodEnum,
  HouseRegistersStatusEnum,
} from '@/gen/Enums';
import {
  BetaSchemaForm,
  ProCard,
  ProDescriptions,
  ProTable,
} from '@ant-design/pro-components';
import { Form, message, Space } from 'antd';
export default function Audit(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Bill.HouseBills.Update>
      {...MyModalFormProps.props}
      title={props.title}
      trigger={<MyButtons.Default title="审核" type="primary" />}
      // wrapperCol={{ span: 24 }}
      layout="horizontal"
      labelCol={{ span: 0 }}
      wrapperCol={{ span: 24 }}
      labelAlign="left"
      width="800px"
      key={new Date().getTime()}
      form={form}
      onFinish={async (values: any) =>
        Apis.HouseOrder.HouseOrders.Audit({
          id: props.item?.id ?? 0,
          accept_name: props.item?.receipt_account?.company_name || '',
          accept_bank: props.item?.receipt_account?.company_bank || '',
          accept_account: props.item?.receipt_account?.company_account || '',
          ...values,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          // title: '登记信息',
          dataIndex: 'info_display',
          valueType: 'text',
          renderFormItem: () => (
            <Space direction="vertical" style={{ width: '100%' }}>
              <ProCard size="small">
                <ProDescriptions bordered size="small" column={2}>
                  <ProDescriptions.Item label="房屋名称" span={2}>
                    <Space>
                      {props?.item?.house_order_items?.[0].asset_house
                        ?.full_name || '-'}
                      <renderTextHelper.Tag
                        Enums={HouseOrdersAuditStatusEnum}
                        value={props?.item?.audit_status}
                      />
                    </Space>
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="收款账号" span={2}>
                    {props?.item?.receipt_account?.company_name} ；
                    {props?.item?.receipt_account?.company_bank} ；
                    {props?.item?.receipt_account?.company_account}
                  </ProDescriptions.Item>
                  {/* <ProDescriptions.Item label="关联流水" span={2}>
                    {props?.item?.accept_serial_number || '未提供'}
                  </ProDescriptions.Item> */}
                  <ProDescriptions.Item label="收款金额">
                    {props?.item?.actual_paid_amount || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="录入人">
                    {props?.item?.creator || '-'}-
                    {props?.item?.creator_phone || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="录入时间">
                    {props?.item?.created_at || '-'}
                  </ProDescriptions.Item>
                </ProDescriptions>
              </ProCard>
              <ProCard title="" size="small">
                <ProTable
                  {...MyProTableProps.props}
                  search={false}
                  toolBarRender={false}
                  pagination={false}
                  dataSource={props?.item?.house_order_items}
                  rowKey={(record, index) => record?.id || index}
                  size="small"
                  columns={[
                    {
                      title: '关联账单',
                      render: (_, record) => {
                        return `${record.house_bill.year}-${String(
                          record.house_bill.month,
                        ).padStart(2, '0')}`;
                      },
                    },
                    MyColumns.EnumTag({
                      title: '类型',
                      dataIndex: ['house_bill', 'type'],
                      valueEnum: HouseBillsTypeEnum,
                    }),
                    {
                      title: '未收金额',
                      dataIndex: 'amount',
                      search: false,
                    },
                    {
                      title: '本次收取金额',
                      dataIndex: 'paid_amount',
                      search: false,
                    },
                  ]}
                />
              </ProCard>
            </Space>
          ),
          colProps: { span: 24 },
        },

        {
          valueType: 'group',
          title: '',
          columns: [
            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;
              },
              formItemProps: {
                ...rulesHelper.text,
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
              },
              initialValue: props?.item?.payment_method,
            }),
            {
              key: 'paid_time',
              title: '收款日期',
              valueType: 'date',
              colProps: { span: 24 },
              formItemProps: {
                ...rulesHelper.text,
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
              },
              initialValue: props?.item?.paid_time
                ? new Date(props?.item?.paid_time)
                : undefined,
            },
          ],
        },

        {
          valueType: 'group',
          // title: '审核操作',
          columns: [
            MyFormItems.EnumRadio({
              key: 'audit_status',
              title: '审核操作',
              colProps: { span: 24 },
              valueEnum: () => {
                let obj: any = JSON.parse(
                  JSON.stringify(HouseRegistersStatusEnum),
                );
                delete obj.Pending;
                return obj;
              },
              formItemProps: {
                ...rulesHelper.text,
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
              },
            }),

            {
              valueType: 'dependency',
              name: ['audit_status'],
              columns: ({ audit_status }) => {
                return audit_status ===
                  HouseOrdersAuditStatusEnum.Rejected.value
                  ? [
                      {
                        title: '驳回原因',
                        dataIndex: 'reason',
                        valueType: 'textarea',
                        formItemProps: {
                          ...rulesHelper.text,
                          labelCol: { span: 4 },
                          wrapperCol: { span: 20 },
                        },
                        colProps: { span: 24 },
                      },
                    ]
                  : [
                      {
                        key: 'accept_serial_number',
                        title: '关联流水',
                        valueType: 'text',
                        colProps: { span: 24 },
                        initialValue: props?.item?.accept_serial_number,
                        formItemProps: {
                          ...rulesHelper.text,
                          labelCol: { span: 4 },
                          wrapperCol: { span: 20 },
                        },
                        fieldProps: {
                          placeholder: '请输入银行流水',
                        },
                      },
                    ];
              },
            },
          ],
        },
      ]}
    />
  );
}
