import { MyColumns, MyPageContainer, MyProTableProps } from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AttendanceRecordsCheckinTypeEnum,
  AttendanceRecordsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Image } from 'antd';
import { useState } from 'react';

export default function Index({ title = '打卡记录' }) {
  const [getParams, setParams] = useState({});

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="attendance_records"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Attendance.AttendanceRecords.List,
            (e) => {
              setParams(e);
            },
          )
        }
        headerTitle={title}
        toolBarRender={() => [
          <MyExport
            key="export"
            item={getParams}
            download={Apis.Attendance.AttendanceRecords}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          {
            title: '员工',
            dataIndex: ['company_employee', 'name'],
            search: false,
          },
          {
            title: '打卡时间',
            dataIndex: 'checkin_time',
            valueType: 'dateRange',
            hidden: true,
          },
          MyColumns.EnumTag({
            title: '打卡类型',
            dataIndex: 'checkin_type',
            valueEnum: AttendanceRecordsCheckinTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '打卡结果',
            dataIndex: 'status',
            valueEnum: AttendanceRecordsStatusEnum,
          }),
          {
            title: '打卡时间',
            dataIndex: 'checkin_time',
            search: false,
            render: (_, item: any) => {
              return item?.status === 'CheckIn'
                ? item?.shift_periods?.work_start_time
                : item?.shift_periods?.work_end_time;
            },
          },
          {
            title: '员工打卡时间',
            dataIndex: 'checkin_time',
            search: false,
          },

          {
            title: '打卡拍照',
            dataIndex: 'checkin_time',
            search: false,
            render: (_, item: any) => {
              return (
                <Image.PreviewGroup
                  preview={{
                    onChange: (current, prev) =>
                      console.log(
                        `current index: ${current}, prev index: ${prev}`,
                      ),
                  }}
                >
                  {item?.photo?.map((res: any, index?: number) => {
                    return (
                      <Image key={`item_${index}`} height={30} src={res?.url} />
                    );
                  })}
                </Image.PreviewGroup>
              );
            },
          },
          // MyColumns.CreatedAt(),
        ]}
      />
    </MyPageContainer>
  );
}
