import {
  MyButtons,
  MyColumns,
  MyModalFormProps,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AttendanceRecordsCheckinTypeEnum,
  AttendanceRecordsStatusEnum,
} from '@/gen/Enums';
import { BetaSchemaForm, ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Form, Image, Space } from 'antd';
import { useState } from 'react';

export default function Index({ title = '打卡记录' }) {
  const [getParams, setParams] = useState({});
  const navigate = useNavigate();
  const [form] = Form.useForm();

  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={() => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              viewTrack: (
                <BetaSchemaForm
                  {...MyModalFormProps.props}
                  key={Date.now()}
                  title="员工轨迹查询"
                  trigger={
                    <MyButtons.Default
                      title="员工轨迹"
                      type="primary"
                      size="middle"
                    />
                  }
                  width="500px"
                  form={form}
                  onOpenChange={(open: any) => {
                    if (open) {
                      form.resetFields();
                    }
                  }}
                  onFinish={async (values: any) => {
                    navigate(
                      `/attendance/employee_tracks/${values.company_employees_id}`,
                    );
                    return true;
                  }}
                  columns={[
                    Selects?.Employees({
                      title: '请选择员工',
                      key: 'company_employees_id',
                      required: true,
                      colProps: { span: 24 },
                    }),
                  ]}
                />
              ),
              export: (
                <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(),
          MyColumns.Option({
            render: (_, item: any, index) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    view: (
                      <MyButtons.Default
                        title="查看轨迹"
                        type="primary"
                        onClick={() =>
                          navigate(`/attendance/employee_tracks/${item.id}`)
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
