import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseChargeTaskDetailsStatusEnum,
  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 = '仪表账单生成' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_charge_tasks_bill"
      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({ 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: 'bill_year',
            valueType: 'dateYear',
            hidden: true,
          },
          {
            title: '选择月份',
            dataIndex: 'bill_month',
            valueType: 'dateMonth',
            hidden: true,
            fieldProps: {
              format: 'MM',
            },
          },
          // {
          //   title: '收费标准',
          //   dataIndex: ['house_charge_standard', 'name'],
          //   search: false,
          // },
          {
            title: '账单月份',
            search: false,
            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 ===
                      HouseChargeTaskDetailsStatusEnum.Success.value ||
                    item.status ===
                      HouseChargeTaskDetailsStatusEnum.Failed.value
                      ? '重新执行'
                      : '执行'
                  }
                  type="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>
  );
}
