import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseChargeTasksStatusEnum,
  HouseChargeTasksTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import ChargeTasksCreate from './modals/ChargeTasksCreate';

export default function Index({ title = '账单任务' }) {
  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'charge_tasks',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="bills"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.HouseCharage.HouseChargeTasks.List,
          )
        }
        toolBarRender={(action) => [
          <ChargeTasksCreate
            key="Create"
            reload={action?.reload}
            title={title}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          MyColumns.EnumTag({
            title: '任务状态',
            dataIndex: 'status',
            valueEnum: HouseChargeTasksStatusEnum,
          }),
          MyColumns.EnumTag({
            title: '创建类型',
            dataIndex: 'type',
            valueEnum: HouseChargeTasksTypeEnum,
          }),
          {
            title: '收费标准名称',
            dataIndex: 'charge_standard_name',
            search: false,
          },
          {
            title: '账单月份',
            render: (_, record) => {
              return `${record.year}-${String(record.month).padStart(2, '0')}`;
            },
          },
          {
            title: '计费开始日期',
            dataIndex: 'start_date',
            search: false,
          },
          {
            title: '计费结束日期',
            dataIndex: 'end_date',
            search: false,
          },
          {
            title: '项目名称',
            dataIndex: 'project_name',
            search: false,
          },
          {
            title: '机构名称',
            dataIndex: 'company_name',
            search: false,
          },
          MyColumns.SoftDelete({
            title: '启/禁用',
            onRestore: Apis.HouseCharage.HouseChargeTasks.Restore,
            onSoftDelete: Apis.HouseCharage.HouseChargeTasks.SoftDelete,
            search: false,
          }),
          MyColumns.UpdatedAt(),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharage.HouseChargeTasks.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
