import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Selects } from '@/components/Select';
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();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_charge_tasks"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="账单任务"
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.HouseCharge.HouseChargeTasks.List,
          )
        }
        toolBarRender={(action) => [
          <TaskCreate key="Create" reload={action?.reload} title="账单任务" />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          MyColumns.EnumTag({
            title: '创建类型',
            dataIndex: 'type',
            valueEnum: HouseChargeTasksTypeEnum,
            search: false,
          }),
          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}?type=${record?.house_charge_standard?.type}`,
                  );
                }}
              />
            ),
          },
          {
            title: '账单年份',
            valueType: 'dateYear',
            dataIndex: 'bill_year',
            hidden: true,
          },
          {
            title: '账单月份',
            valueType: 'dateMonth',
            dataIndex: 'bill_month',
            hidden: true,
            fieldProps: {
              format: 'MM',
            },
          },
          {
            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}>
                <MyButtons.Default
                  title={
                    item.status === HouseChargeTasksStatusEnum.Completed.value
                      ? '重新执行'
                      : '执行'
                  }
                  type="primary"
                  isConfirm
                  description="是否重新执行此任务？"
                  onConfirm={() =>
                    Apis.HouseCharge.HouseChargeTasks.ExecuteTask({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
                <TaskShow
                  item={item}
                  title="执行情况"
                  reload={action?.reload}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharge.HouseChargeTasks.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
