import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseChargeTaskDetailsStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import MeterTaskCreate from '../modals/MeterTaskCreate';
import MeterTaskShow from '../modals/MeterTaskShow';

export default function Index(props: MyBetaModalFormProps) {
  const navigate = useNavigate();

  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, house_meters_id: props?.item?.id },
            sort,
            Apis.Meter.HouseMeterTaskDetails.List,
          )
        }
        headerTitle="仪表任务（公摊表，请在【仪表任务】的菜单中执行）"
        toolBarRender={(action) => [
          <MeterTaskCreate
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="仪表任务"
          />,
        ]}
        search={false}
        // options={false}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '仪表编号',
            dataIndex: ['meter_data', 'name'],
            search: false,
          },
          {
            title: '关联房屋',
            dataIndex: 'full_name',
            search: false,
            render: (_, record) => (
              <MyButtons.View
                title={record.full_name}
                type="link"
                onClick={() => {
                  navigate(`/bills/summary/show/${record.asset_houses_id}`);
                }}
              />
            ),
          },
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'status',
            valueEnum: HouseChargeTaskDetailsStatusEnum,
          }),

          {
            title: '收费标准',
            dataIndex: ['charge_standard', 'name'],
            render: (_, record) => {
              return `【${record.charge_standard.id}】${record.charge_standard.name}`;
            },
            search: false,
          },
          {
            title: '账单月份',
            render: (_, record) => {
              return `${record.year}-${String(record.month).padStart(2, '0')}`;
            },
            search: false,
          },
          {
            title: '完成时间',
            dataIndex: 'processed_time',
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MeterTaskShow
                  item={item}
                  title="查看"
                  reload={action?.reload}
                />
                <MyButtons.Default
                  title={
                    item.status ===
                      HouseChargeTaskDetailsStatusEnum.Success.value ||
                    item.status ===
                      HouseChargeTaskDetailsStatusEnum.Failed.value
                      ? '重新执行'
                      : '执行'
                  }
                  type="link"
                  color="primary"
                  isConfirm
                  description="是否重新执行此任务？"
                  onConfirm={() =>
                    Apis.Meter.HouseMeterTasks.ExecuteMeterTasks({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
