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

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_charge_tasks"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Meter.HouseMeterTasks.List)
        }
        toolBarRender={(action) => [
          <TaskCreate key="Create" reload={action?.reload} title="仪表任务" />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '机构',
            dataIndex: ['company', 'name'],
            search: false,
          },
          {
            title: '项目',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          MyColumns.EnumTag({
            title: '任务状态',
            dataIndex: 'status',
            valueEnum: HouseChargeTasksStatusEnum,
          }),
          MyColumns.EnumTag({
            title: '生成方式',
            dataIndex: 'generation_method',
            valueEnum: HouseMeterTasksGenerationMethodEnum,
          }),
          // {
          //   title: '收费标准',
          //   dataIndex: ['house_charge_standard', 'name'],
          //   search: false,
          // },
          {
            title: '账单月份',
            render: (_, record) => {
              return `${record.bill_year}-${String(record.bill_month).padStart(
                2,
                '0',
              )}`;
            },
          },
          {
            title: '计费周期',

            render: (_, record) => {
              return `${record.start_date}-${String(record.end_date).padStart(
                2,
                '0',
              )}`;
            },
            search: false,
          },
          {
            title: '账单数',
            dataIndex: 'task_count',
            search: false,
          },
          MyColumns.UpdatedAt(),
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <TaskShow item={item} title="查看" reload={action?.reload} />
                <MyButtons.Default
                  title={
                    item.status ===
                      HouseChargeTasksStatusEnum.Completed.value ||
                    item.status === HouseChargeTasksStatusEnum.Failed.value
                      ? '重新执行'
                      : '执行'
                  }
                  type="link"
                  color="primary"
                  isConfirm
                  description="是否重新执行此任务？"
                  onConfirm={() =>
                    Apis.Meter.HouseMeterTasks.ExecuteMeterTasks({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
                {/* <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Meter.HouseMeterTasks.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                /> */}
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
