import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { PatrolTasksCreateTypeEnum, PatrolTasksStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import Create from './modals/Create';
import LocationsManage from './modals/LocationsManage';
import PatrolOrderAssign from './modals/PatrolOrderAssign';
import PatrolOrderShow from './modals/PatrolOrderShow';
import RoutesManage from './modals/RoutesManage';

export default function Index({ title = '巡更任务' }) {
  return (
    <MyPageContainer title={title}>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Patrol.PatrolTasks.List)
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              locations: <LocationsManage key="LocationsManage" />,
              routes: <RoutesManage key="RoutesManage" />,
              create: (
                <Create key="Create" reload={action?.reload} title={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 };
              },
            },
          },
          {
            title: '路线名称',
            dataIndex: ['patrol_route', 'name'],
            search: {
              transform: (value) => {
                return { patrol_route_name: value };
              },
            },
          },

          {
            title: '任务时段',
            dataIndex: 'start_time',
            valueType: 'dateTime',
            render: (_, item: any) => {
              const formatStartTime = (timeStr: string) => {
                if (!timeStr) return '';
                const [date, time] = timeStr.split(' ');
                if (!time) return date;
                return `${date} ${time.split(':').slice(0, 2).join(':')}`;
              };

              const formatEndTime = (timeStr: string) => {
                if (!timeStr) return '';
                const [, time] = timeStr.split(' ');
                if (!time) return '';
                return time.split(':').slice(0, 2).join(':');
              };

              return `${formatStartTime(item.start_time)} - ${formatEndTime(
                item.end_time,
              )}`;
            },
            search: false,
          },
          {
            title: '任务时段',
            dataIndex: 'date_time',
            valueType: 'dateRange',
            hideInTable: true,
          },
          MyColumns.EnumTag({
            title: '生成方式',
            dataIndex: 'create_type',
            valueEnum: PatrolTasksCreateTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '任务状态',
            dataIndex: 'status',
            valueEnum: PatrolTasksStatusEnum,
            search: false,
          }),
          {
            title: '指派时间',
            dataIndex: 'assign_time',
            search: false,
          },
          {
            title: '关联工单',
            dataIndex: 'house_work_orders_id',
            search: false,
          },
          {
            title: '完成时间',
            dataIndex: 'complete_time',
            search: false,
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  view:
                    item.status !== 'Unassigned' ? (
                      <PatrolOrderShow
                        item={item}
                        reload={action?.reload}
                        title="查看"
                      />
                    ) : null,
                  assign:
                    item?.status === 'Unassigned' ? (
                      <PatrolOrderAssign
                        item={item}
                        reload={action?.reload}
                        title="指派"
                      />
                    ) : null,
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.Patrol.PatrolTasks.Delete({ id: item.id }).then(
                          () => action?.reload(),
                        )
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
