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

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="attendance_shifts"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Attendance.AttendanceShifts.List,
          )
        }
        headerTitle={title}
        toolBarRender={() => [
          <MyButtons.Default
            key="Create"
            size="middle"
            type="primary"
            onClick={() => {
              navigate('/attendance/attendance_shifts/pages/create');
            }}
            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 };
            //   },
            // },
            search: false,
          },
          {
            title: '班次名称',
            dataIndex: 'name',
          },

          {
            title: '时段要求',
            dataIndex: 'attendance_shift_periods',
            search: false,
            render: (_, item: any) => {
              const periods = item?.attendance_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>
              );
            },
          },

          {
            title: '可打卡时间范围',
            render(_, record) {
              return `${record?.allow_checkin_start} - ${record?.allow_checkin_end}`;
            },
            search: false,
          },
          {
            title: '备注',
            dataIndex: 'remark',
            search: false,
          },
          MyColumns.Boolean({
            dataIndex: 'is_enabled',
            title: '启用',
          }),

          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Default
                  key="Update"
                  size="small"
                  type="primary"
                  onClick={() => {
                    navigate(
                      `/attendance/attendance_shifts/pages/update?id=${item.id}`,
                    );
                  }}
                  title="编辑"
                />

                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Attendance.AttendanceShifts.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
