import { ModelType, getColumns, produceModel } from '@gadmin2n/react-common';
import { ListButton, Show } from '@refinedev/antd';
import {
  IResourceComponentsProps,
  useShow,
  useTranslate,
} from '@refinedev/core';
import React, { useState } from 'react';
import { getShowField } from '../../helpers';
import type { AgendaJob } from '../../generated/types/prisma.types'; //'.prisma/client/index';
import {
  agendaJobShowConfig as showConfig,
  agendaJobShowPrismaSelect as showPrismaSelect,
} from '../../generated/props/agendaJob/config';
import { agendaJobModel } from '../../generated/props/agendaJob/model';
import { modelsMap } from 'generated/models.index';
import { agentAttrs } from '../../components/agentPanel/agentAttributes';

export const AgendaJobShow: React.FC<IResourceComponentsProps> = () => {
  const resourceName = 'agendaJob';
  const t = useTranslate();

  const { queryResult } = useShow<AgendaJob>({
    meta: { select: showPrismaSelect },
  });
  const { data, isLoading } = queryResult;
  const record = data?.data;

  const [[fields, model]] = useState(() => {
    const model: ModelType = produceModel(agendaJobModel, {
      // status: { render: xxxxx }
    });

    // getColumns is a function that returns an array of fields
    const fields = getColumns('AgendaJob', showConfig.fields, {
      ...modelsMap,
      AgendaJob: { model, meta: modelsMap['AgendaJob'].meta },
    });

    return [fields, model] as const;
  });

  return (
    <Show
      isLoading={isLoading}
      headerButtons={
        <div
          style={{ display: 'inline-flex', gap: 8 }}
          {...agentAttrs({ type: 'show-header', resource: resourceName })}
        >
          <ListButton />
          {/* 该资源为只读：Edit / Clone / Delete / Refresh 均已移除 */}
        </div>
      }
      contentProps={{ styles: { body: { padding: 0 } } }}
    >
      <div
        style={{
          border: '1px solid #f0f0f0',
          borderRadius: 8,
          overflow: 'hidden',
        }}
      >
        {fields.map((field: any) => {
          return getShowField(field, resourceName, record, t, model, modelsMap);
        })}
      </div>
    </Show>
  );
};
