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 { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import TaskCreate from './modals/TaskCreate';
import TaskShow from './modals/TaskShow';

export default function Index({ title = '账单任务' }) {
  const navigate = useNavigate();
  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'house_charge_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.HouseCharage.HouseChargeTasks.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: 'type',
            valueEnum: HouseChargeTasksTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '任务状态',
            dataIndex: 'status',
            valueEnum: HouseChargeTasksStatusEnum,
          }),
          {
            title: '收费标准',
            dataIndex: ['house_charge_standard', 'name'],
            search: false,
            render: (_, record) => (
              <MyButtons.View
                title={record?.house_charge_standard?.name || '-'}
                type="link"
                onClick={() => {
                  navigate(
                    `/charge/standard/show/${record.house_charge_standards_id}`,
                  );
                }}
              />
            ),
          },
          {
            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
                      ? '重新执行'
                      : '执行'
                  }
                  type="link"
                  color="primary"
                  isConfirm
                  description="是否重新执行此任务？"
                  onConfirm={() =>
                    Apis.HouseCharage.HouseChargeTasks.ExecuteTask({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharage.HouseChargeTasks.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
