import {
  MyButtons,
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseBillsBillStatusEnum,
  HouseOrdersAuditStatusEnum,
  HouseOrdersPaymentMethodEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';
import Audit from './modals/Audit';
import SerialNumber from './modals/SerialNumber';
import Show from './modals/Show';

export default function Index({ title = '收款单' }) {
  const [getParams, setParams] = useState({});
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions({
      import: (
        <MyImportModal
          key="import"
          title="导入流水"
          type="default"
          size="middle"
          templateApi={Apis.HouseOrder.HouseOrders.DownloadSerialNumberTemplate}
          importApi={Apis.HouseOrder.HouseOrders.ImportSerialNumber}
          reload={action?.reload}
        />
      ),
      export: (
        <MyExport
          key="export"
          item={getParams}
          download={Apis.HouseOrder.HouseOrders}
        />
      ),
    });
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions({
      show: <Show item={item} reload={action?.reload} title="查看" />,
      serialNumber: (
        <SerialNumber item={item} reload={action?.reload} title="关联流水" />
      ),
      audit: (
        <>
          {item.audit_status === HouseOrdersAuditStatusEnum.Pending.value && (
            <Audit item={item} reload={action?.reload} title="审核" />
          )}
        </>
      ),
      delete: (
        <MyButtons.Delete
          onConfirm={() =>
            Apis.HouseOrder.HouseOrders.Delete({ id: item.id }).then(() =>
              action?.reload(),
            )
          }
        />
      ),
    });
  };
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="houseorders"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="收款单明细"
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            params,
            sort,
            Apis.HouseOrder.HouseOrders.List,
          );
        }}
        toolBarRender={(action: any) => [toolBarRender(action)]}
        columns={[
          // MyColumns.ID({ search: false }),
          {
            title: '收款单ID',
            dataIndex: 'id',
            search: false,
          },
          MyColumns.Boolean({
            dataIndex: 'is_serial_number',
            title: '关联流水',
            hidden: true,
          }),
          MyColumns.EnumTag({
            title: '审核状态',
            dataIndex: 'audit_status',
            valueEnum: HouseOrdersAuditStatusEnum,
          }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          {
            title: '房屋',
            dataIndex: ['asset_house', 'full_name'],
            render: (_, record) =>
              record.house_order_items?.[0]?.asset_house?.full_name || '-',
            search: {
              transform: (value) => {
                return { full_name: value };
              },
            },
          },
          MyColumns.EnumTag({
            title: '收款方式',
            dataIndex: 'payment_method',
            valueEnum: HouseOrdersPaymentMethodEnum,
          }),
          MyColumns.EnumTag({
            title: '支付状态',
            dataIndex: 'order_status',
            valueEnum: HouseBillsBillStatusEnum,
            search: false,
          }),

          {
            title: '收款金额',
            dataIndex: 'actual_paid_amount',
            search: false,
          },
          {
            title: '收款时间',
            dataIndex: 'paid_time',
            render: (_, record) => {
              return record?.paid_time?.substring(0, 10);
            },
            search: false,
          },
          {
            title: '收款账户',
            dataIndex: 'accept_name',
            // search: false,
          },
          {
            title: '收款银行',
            dataIndex: 'accept_bank',
            // search: false,
          },
          {
            title: '收款账号',
            dataIndex: 'accept_account',
            // search: false,
          },
          {
            title: '银行流水',
            dataIndex: 'accept_serial_number',
            // search: false,
          },
          {
            title: '系统关联编号',
            dataIndex: 'payment_no',
            search: false,
          },
          // {
          //   title: '录入人',
          //   dataIndex: 'creator',
          //   search: false,
          // },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <>{tableRender(item, action)}</>
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
