import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { AttendanceSchedulesStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space, Tooltip } from 'antd';
import Update from './modals/Update';

export default function Index({ title = '排班管理' }) {
  const navigate = useNavigate();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="attendance_schedules"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, status: AttendanceSchedulesStatusEnum.Active.value },
            sort,
            Apis.Attendance.AttendanceSchedules.List,
          )
        }
        headerTitle="排班信息"
        toolBarRender={() => [
          <MyButtons.Default
            key="Create"
            size="middle"
            type="primary"
            onClick={() => {
              navigate('/attendance/attendance_schedules/pages/create');
            }}
            title="批量排班 / 批量调整"
          />,
        ]}
        columns={[
          // MyColumns.ID({
          //   search: false,
          // }),

          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '排班日期',
            dataIndex: 'schedule_date',
            valueType: 'date',
          },
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            // search: {
            //   transform: (value) => {
            //     return { project_name: value };
            //   },
            // },
            search: false,
          },
          {
            title: '员工',
            dataIndex: ['company_employee', 'name'],
            search: {
              transform: (value) => {
                return { employee_name: value };
              },
            },
          },
          {
            title: '班次',
            dataIndex: ['attendance_shift', 'name'],
            search: false,
          },
          {
            title: '时段要求',
            dataIndex: 'shift_periods',
            search: false,
            render: (_, item: any) => {
              const periods = item?.shift_periods || [];
              const periodTexts = periods.map((res: any) => {
                return `时段${
                  res?.period_order
                }: ${res?.work_start_time?.substring(
                  0,
                  5,
                )}-${res?.work_end_time?.substring(0, 5)}`;
              });
              const allPeriodsText = periodTexts.join(' ');

              return (
                <Tooltip
                  title={
                    periodTexts.map((text: string, index: number) => (
                      <div key={index}>{text}</div>
                    )) || ''
                  }
                >
                  <div
                    style={{
                      whiteSpace: 'nowrap',
                      overflow: 'hidden',
                      textOverflow: 'ellipsis',
                      maxWidth: '150px',
                    }}
                  >
                    {allPeriodsText}
                  </div>
                </Tooltip>
              );
            },
          },

          // MyColumns.EnumTag({
          //   title: '状态',
          //   dataIndex: 'status',
          //   valueEnum: AttendanceSchedulesStatusEnum,
          // }),
          {
            title: '排班人',
            dataIndex: ['created_employee', 'name'],
            search: false,
          },
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Default
                  disabled={item?.status === 'Cancelled'}
                  isConfirm
                  title="取消班次"
                  danger
                  description="是否确定取消?"
                  onConfirm={() =>
                    Apis.Attendance.AttendanceSchedules.Cancel({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
