import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseBillsBillStatusEnum, HouseBillsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import BillCreate from './modals/BillCreate';
import BIllInfo from './modals/BIllInfo';
import BillRefund from './modals/BillRefund';
import BillUpdate from './modals/BillUpdate';

export default function Index({ title = '账单明细' }) {
  const navigate = useNavigate();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_bills"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle={title}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Bill.HouseBills.List)
        }
        toolBarRender={(action) => [
          // <MyImportModal
          //   key="ImportHouse"
          //   title="账单导入"
          //   type="default"
          //   size="middle"
          //   templateApi={Apis.Bill.HouseBills.DownloadTemplate}
          //   importApi={Apis.Bill.HouseBills.Import}
          //   reload={action?.reload}
          // />,
          <BillCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '房屋',
            dataIndex: ['asset_house', 'full_name'],
            search: {
              transform: (value) => {
                return { full_name: value };
              },
            },
            render: (_, res) => {
              return res.asset_houses_id ? (
                <MyButtons.View
                  title={res?.asset_house?.full_name || '-'}
                  type="link"
                  onClick={() => {
                    navigate(`/bills/summary/show/${res.asset_houses_id}`);
                  }}
                />
              ) : (
                '车位｜ ' + res.asset_car_port.full_name
              );
            },
          },
          {
            title: '账单月份',
            render: (_, record) => {
              return `${record.year}-${String(record.month).padStart(2, '0')}`;
            },
            search: false,
          },

          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: HouseBillsTypeEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'bill_status',
            valueEnum: HouseBillsBillStatusEnum,
            search: {
              transform: (value) => {
                return { bill_status: [value] };
              },
            },
          }),
          {
            title: '账单金额',
            dataIndex: 'amount',
            search: false,
          },
          {
            title: '优惠金额',
            dataIndex: 'discount_amount',
            search: false,
          },
          {
            title: '滞纳金',
            dataIndex: 'late_fee',
            search: false,
          },
          {
            title: '应收金额',
            dataIndex: 'total_payable_amount',
            search: false,
          },
          {
            title: '已收金额',
            dataIndex: 'total_paid_amount',
            search: false,
          },

          {
            title: '计费周期',
            dataIndex: 'start_date',
            render: (_, record) => {
              return `${record.start_date} 至 ${record.end_date}`;
            },
            search: false,
          },

          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BIllInfo
                  item={{ ...item, type: 'primary' }}
                  reload={action?.reload}
                  title="查看"
                />
                <BillUpdate item={item} reload={action?.reload} title={title} />
                <BillRefund
                  item={{
                    ...item,
                    // 跟据发起的页面传递的type来判断退款类型
                    // type: RefundsTypeEnum.HouseOrder.value,
                    // // 审批模板的类型
                    approval_type: 'Refund',
                    total_paid_amount: item.total_paid_amount,
                  }}
                  reload={action?.reload}
                  title={title}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Bill.HouseBills.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
