import {
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseBillsBillStatusEnum,
  HouseOrdersPaymentMethodEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useState } from 'react';
import SerialNumber from './modals/SerialNumber';
import SerialNumbers from './modals/SerialNumbers';
import Show from './modals/Show';

export default function Index({ title = '线上收款单' }) {
  const [getParams, setParams] = useState({});
  const [selectedRows, setSelectedRows] = useState<any[]>([]);
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="houseorders"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="收款单明细"
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            {
              ...params,
              payment_methods: [
                HouseOrdersPaymentMethodEnum.TongLian.value,
                HouseOrdersPaymentMethodEnum.WeChat.value,
                HouseOrdersPaymentMethodEnum.Alipay.value,
                HouseOrdersPaymentMethodEnum.CCB.value,
              ],
            },
            sort,
            Apis.HouseOrder.HouseOrders.List,
          );
        }}
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              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}
                />
              ),
              serialNumbers: (
                <SerialNumbers
                  key="serialNumbers"
                  items={selectedRows}
                  reload={action?.reload}
                  title="批量关联"
                />
              ),
            }}
          />,
        ]}
        columns={[
          // MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '收款单ID',
            dataIndex: 'id',
            search: false,
          },
          MyColumns.Boolean({
            dataIndex: 'is_serial_number',
            title: '关联流水',
            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: 'fee',
            search: false,
          },

          {
            title: '收款日期',
            dataIndex: 'paid_time',
            valueType: 'dateRange',
            hidden: true,
          },
          {
            title: '收款日期',
            dataIndex: 'paid_time',
            valueType: 'date',
            // 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) => (
              <MyTableActions
                key={index}
                actions={{
                  show: (
                    <Show item={item} reload={action?.reload} title="查看" />
                  ),
                  serialNumber: (
                    <SerialNumber
                      item={item}
                      reload={action?.reload}
                      title="关联流水"
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
