import {
  MyButtons,
  MyColumns,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from './modals/Create';
import Update from './modals/Update';

export default function Index({
  title = '打卡配置',
  noPermission,
}: {
  title?: string;
  noPermission?: boolean;
}) {
  return (
    <ProTable
      {...MyProTableProps.props}
      // search={false}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Attendance.AttendanceConfigs.List,
        )
      }
      headerTitle="打卡参数配置"
      toolBarRender={(action) =>
        noPermission
          ? [<Create key="Create" reload={action?.reload} title={title} />]
          : [
              <MyToolBarActions
                key="toolbar"
                actions={{
                  add: (
                    <Create
                      key="Create"
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                }}
              />,
            ]
      }
      search={false}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '可打卡范围(米内)',
          dataIndex: 'check_in_range',
          search: false,
        },
        {
          title: '关联项目',
          dataIndex: ['asset_project', 'name'],
          search: false,
        },
        MyColumns.Boolean({
          dataIndex: 'require_photo',
          title: '是否要求拍照打卡',
          search: false,
        }),
        // MyColumns.Boolean({
        //   dataIndex: 'allow_out_range_checkin',
        //   title: '是否允许范围外打卡',
        //   search: false,
        // }),
        // MyColumns.IsEnabled({
        //   onRestore: Apis.Attendance.AttendanceConfigs.Enable,
        //   onSoftDelete: Apis.Attendance.AttendanceConfigs.Enable,
        //   search: false,
        // }),
        // MyColumns.CreatedAt(),
        MyColumns.UpdatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <MyTableActions
                actions={{
                  update: (
                    <Update item={item} reload={action?.reload} title={title} />
                  ),
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.Attendance.AttendanceConfigs.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
