File size: 24,009 Bytes
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385f196
 
 
 
 
 
 
 
 
 
 
 
 
c88d3e9
385f196
 
 
 
 
 
 
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385f196
 
 
c88d3e9
 
 
 
 
385f196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c88d3e9
 
 
 
 
 
 
385f196
 
c88d3e9
 
385f196
 
 
 
c88d3e9
385f196
 
c88d3e9
 
 
385f196
c88d3e9
 
 
 
 
 
 
 
385f196
 
 
 
 
c88d3e9
 
385f196
 
 
 
c88d3e9
385f196
 
c88d3e9
 
 
385f196
c88d3e9
385f196
 
 
 
 
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac5782
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385f196
 
c88d3e9
385f196
 
 
 
 
 
 
c88d3e9
 
 
 
 
 
385f196
 
 
 
 
 
 
c88d3e9
385f196
 
 
c88d3e9
385f196
 
c88d3e9
 
385f196
 
c88d3e9
385f196
 
c88d3e9
385f196
 
 
 
 
c88d3e9
385f196
 
 
 
 
 
 
 
 
 
 
c88d3e9
 
385f196
 
 
 
 
c88d3e9
385f196
 
 
 
 
 
 
 
 
 
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385f196
 
 
 
 
c88d3e9
 
 
385f196
 
 
 
 
c88d3e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385f196
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import React, { useEffect, useState } from 'react'
import { api, downloadBlob } from '../api'
import DataTable from './DataTable'
import EquationFormatsPanel from './EquationFormatsPanel'
import LoadingOverlay from './LoadingOverlay'
import MapFrame from './MapFrame'
import PlotFigure from './PlotFigure'

const REPO_INNER_TABS = [
  { key: 'mapa', label: 'Mapa' },
  { key: 'dados_mercado', label: 'Dados de Mercado' },
  { key: 'metricas', label: 'Métricas' },
  { key: 'transformacoes', label: 'Transformações' },
  { key: 'resumo', label: 'Resumo' },
  { key: 'coeficientes', label: 'Coeficientes' },
  { key: 'obs_calc', label: 'Obs x Calc' },
  { key: 'graficos', label: 'Gráficos' },
]

function formatarFonte(fonte) {
  if (!fonte || typeof fonte !== 'object') return 'Fonte não informada'
  const provider = String(fonte.provider || '').toLowerCase()
  if (provider === 'hf_dataset') {
    const repo = String(fonte.repo_id || '').trim()
    const revision = String(fonte.revision || '').trim()
    const suffix = fonte.degraded ? ' (modo contingência)' : ''
    return `HF Dataset${repo ? ` (${repo})` : ''}${revision ? ` | revisão ${revision.slice(0, 8)}` : ''}${suffix}`
  }
  return 'Pasta local'
}

export default function RepositorioTab({ authUser, sessionId }) {
  const [modelos, setModelos] = useState([])
  const [fonte, setFonte] = useState(null)
  const [loading, setLoading] = useState(false)
  const [uploading, setUploading] = useState(false)
  const [deleting, setDeleting] = useState(false)
  const [error, setError] = useState('')
  const [status, setStatus] = useState('')
  const [arquivoUpload, setArquivoUpload] = useState(null)
  const [confirmDeleteId, setConfirmDeleteId] = useState('')
  const [confirmarSubstituicao, setConfirmarSubstituicao] = useState({ open: false, substituidos: [] })
  const [confirmarExclusaoModal, setConfirmarExclusaoModal] = useState({
    open: false,
    modeloId: '',
    nomeModelo: '',
    digitado: '',
  })

  const [modeloAbertoMeta, setModeloAbertoMeta] = useState(null)
  const [modeloAbertoLoading, setModeloAbertoLoading] = useState(false)
  const [modeloAbertoError, setModeloAbertoError] = useState('')
  const [modeloAbertoActiveTab, setModeloAbertoActiveTab] = useState('mapa')
  const [modeloAbertoDados, setModeloAbertoDados] = useState(null)
  const [modeloAbertoEstatisticas, setModeloAbertoEstatisticas] = useState(null)
  const [modeloAbertoEscalasHtml, setModeloAbertoEscalasHtml] = useState('')
  const [modeloAbertoDadosTransformados, setModeloAbertoDadosTransformados] = useState(null)
  const [modeloAbertoResumoHtml, setModeloAbertoResumoHtml] = useState('')
  const [modeloAbertoEquacoes, setModeloAbertoEquacoes] = useState(null)
  const [modeloAbertoCoeficientes, setModeloAbertoCoeficientes] = useState(null)
  const [modeloAbertoObsCalc, setModeloAbertoObsCalc] = useState(null)
  const [modeloAbertoMapaHtml, setModeloAbertoMapaHtml] = useState('')
  const [modeloAbertoMapaChoices, setModeloAbertoMapaChoices] = useState(['Visualização Padrão'])
  const [modeloAbertoMapaVar, setModeloAbertoMapaVar] = useState('Visualização Padrão')
  const [modeloAbertoPlotObsCalc, setModeloAbertoPlotObsCalc] = useState(null)
  const [modeloAbertoPlotResiduos, setModeloAbertoPlotResiduos] = useState(null)
  const [modeloAbertoPlotHistograma, setModeloAbertoPlotHistograma] = useState(null)
  const [modeloAbertoPlotCook, setModeloAbertoPlotCook] = useState(null)
  const [modeloAbertoPlotCorr, setModeloAbertoPlotCorr] = useState(null)

  const isAdmin = String(authUser?.perfil || '').toLowerCase() === 'admin'
  const totalModelos = modelos.length
  const modoModeloAberto = Boolean(modeloAbertoMeta)
  const nomesSubstituidos = Array.isArray(confirmarSubstituicao.substituidos)
    ? confirmarSubstituicao.substituidos.filter(Boolean)
    : []
  const exclusaoDigitadaCorreta = confirmarExclusaoModal.digitado === confirmarExclusaoModal.nomeModelo

  useEffect(() => {
    void carregarModelos()
  }, [])

  async function carregarModelos() {
    setLoading(true)
    setError('')
    try {
      const resp = await api.repositorioListar()
      setModelos(Array.isArray(resp?.modelos) ? resp.modelos : [])
      setFonte(resp?.fonte || null)
      setStatus('')
    } catch (err) {
      setError(err.message || 'Falha ao carregar repositório.')
      setModelos([])
      setFonte(null)
    } finally {
      setLoading(false)
    }
  }

  function parseSubstituidosErro(err) {
    if (!err || typeof err !== 'object') return []
    const detail = err.detail
    if (!detail || typeof detail !== 'object') return []
    const lista = detail.substituidos
    if (!Array.isArray(lista)) return []
    return lista.map((item) => String(item || '').trim()).filter(Boolean)
  }

  async function onUploadArquivo(confirmar = false) {
    if (!isAdmin || !arquivoUpload) return
    setUploading(true)
    setError('')
    setStatus('')
    try {
      const resp = await api.repositorioUpload([arquivoUpload], { confirmarSubstituicao: confirmar })
      setModelos(Array.isArray(resp?.modelos) ? resp.modelos : [])
      setFonte(resp?.fonte || null)
      setStatus(resp?.status || 'Modelo incluído no repositório.')
      setArquivoUpload(null)
      setConfirmarSubstituicao({ open: false, substituidos: [] })
    } catch (err) {
      const substituidos = parseSubstituidosErro(err)
      const erroDuplicado = Number(err?.status) === 409 && substituidos.length > 0
      if (!confirmar && erroDuplicado) {
        setConfirmarSubstituicao({ open: true, substituidos })
        setError('')
        return
      }
      setError(err.message || 'Falha ao incluir modelo no repositório.')
    } finally {
      setUploading(false)
    }
  }

  async function onExcluirModelo(modeloId) {
    if (!isAdmin || !modeloId) return
    setDeleting(true)
    setError('')
    setStatus('')
    try {
      const resp = await api.repositorioDelete([String(modeloId)])
      setModelos(Array.isArray(resp?.modelos) ? resp.modelos : [])
      setFonte(resp?.fonte || null)
      setStatus(resp?.status || 'Modelo removido do repositório.')
      setConfirmDeleteId('')
      setConfirmarExclusaoModal({ open: false, modeloId: '', nomeModelo: '', digitado: '' })
    } catch (err) {
      setError(err.message || 'Falha na exclusão do modelo.')
    } finally {
      setDeleting(false)
    }
  }

  function onAbrirModalExclusao(item) {
    const id = String(item?.id || '').trim()
    if (!id) return
    const nomeModelo = String(item?.nome_modelo || item?.arquivo || id).trim()
    setConfirmDeleteId('')
    setConfirmarExclusaoModal({
      open: true,
      modeloId: id,
      nomeModelo,
      digitado: '',
    })
  }

  function onCancelarModalExclusao() {
    if (deleting) return
    setConfirmarExclusaoModal({ open: false, modeloId: '', nomeModelo: '', digitado: '' })
  }

  async function onConfirmarExclusaoModal() {
    const modeloId = String(confirmarExclusaoModal.modeloId || '').trim()
    if (!modeloId) return
    if (confirmarExclusaoModal.digitado !== confirmarExclusaoModal.nomeModelo) return
    await onExcluirModelo(modeloId)
  }

  function preencherModeloAberto(resp) {
    setModeloAbertoDados(resp?.dados || null)
    setModeloAbertoEstatisticas(resp?.estatisticas || null)
    setModeloAbertoEscalasHtml(resp?.escalas_html || '')
    setModeloAbertoDadosTransformados(resp?.dados_transformados || null)
    setModeloAbertoResumoHtml(resp?.resumo_html || '')
    setModeloAbertoEquacoes(resp?.equacoes || null)
    setModeloAbertoCoeficientes(resp?.coeficientes || null)
    setModeloAbertoObsCalc(resp?.obs_calc || null)
    setModeloAbertoMapaHtml(resp?.mapa_html || '')
    setModeloAbertoMapaChoices(resp?.mapa_choices || ['Visualização Padrão'])
    setModeloAbertoMapaVar('Visualização Padrão')
    setModeloAbertoPlotObsCalc(resp?.grafico_obs_calc || null)
    setModeloAbertoPlotResiduos(resp?.grafico_residuos || null)
    setModeloAbertoPlotHistograma(resp?.grafico_histograma || null)
    setModeloAbertoPlotCook(resp?.grafico_cook || null)
    setModeloAbertoPlotCorr(resp?.grafico_correlacao || null)
  }

  async function onAbrirModelo(item) {
    if (!sessionId) {
      setError('Sessão indisponível no momento. Aguarde e tente novamente.')
      return
    }
    setModeloAbertoLoading(true)
    setModeloAbertoError('')
    try {
      await api.visualizacaoRepositorioCarregar(sessionId, String(item?.id || ''))
      const resp = await api.exibirVisualizacao(sessionId)
      preencherModeloAberto(resp)
      setModeloAbertoActiveTab('mapa')
      setModeloAbertoMeta({
        id: String(item?.id || ''),
        nome: item?.nome_modelo || item?.arquivo || String(item?.id || ''),
      })
    } catch (err) {
      setModeloAbertoError(err.message || 'Falha ao abrir modelo.')
    } finally {
      setModeloAbertoLoading(false)
    }
  }

  function onVoltarRepositorio() {
    setModeloAbertoMeta(null)
    setModeloAbertoError('')
    setModeloAbertoActiveTab('mapa')
  }

  async function onModeloAbertoMapChange(nextVar) {
    setModeloAbertoMapaVar(nextVar)
    if (!sessionId) return
    try {
      const resp = await api.updateVisualizacaoMap(sessionId, nextVar)
      setModeloAbertoMapaHtml(resp?.mapa_html || '')
    } catch (err) {
      setModeloAbertoError(err.message || 'Falha ao atualizar mapa do modelo.')
    }
  }

  async function onDownloadEquacaoModeloAberto(mode) {
    if (!sessionId || !mode) return
    setModeloAbertoLoading(true)
    try {
      const blob = await api.exportEquationViz(sessionId, mode)
      const sufixo = String(mode) === 'excel_sab' ? 'estilo_sab' : 'excel'
      downloadBlob(blob, `equacao_modelo_${sufixo}.xlsx`)
    } catch (err) {
      setModeloAbertoError(err.message || 'Falha ao exportar equação.')
    } finally {
      setModeloAbertoLoading(false)
    }
  }

  if (modoModeloAberto) {
    return (
      <div className="tab-content">
        <div className="pesquisa-opened-model-view">
          <div className="pesquisa-opened-model-head">
            <div className="pesquisa-opened-model-title-wrap">
              <h3>{modeloAbertoMeta?.nome || 'Modelo'}</h3>
              <p>Visualização do modelo do repositório</p>
            </div>
            <button type="button" className="model-source-back-btn model-source-back-btn-danger" onClick={onVoltarRepositorio} disabled={modeloAbertoLoading}>
              Voltar ao repositório
            </button>
          </div>

          <div className="inner-tabs" role="tablist" aria-label="Abas internas do modelo aberto no repositório">
            {REPO_INNER_TABS.map((tab) => (
              <button
                key={tab.key}
                type="button"
                className={modeloAbertoActiveTab === tab.key ? 'inner-tab-pill active' : 'inner-tab-pill'}
                onClick={() => setModeloAbertoActiveTab(tab.key)}
              >
                {tab.label}
              </button>
            ))}
          </div>

          <div className="inner-tab-panel">
            {modeloAbertoActiveTab === 'mapa' ? (
              <>
                <div className="row compact visualizacao-mapa-controls">
                  <label>Variável no mapa</label>
                  <select value={modeloAbertoMapaVar} onChange={(event) => void onModeloAbertoMapChange(event.target.value)}>
                    {modeloAbertoMapaChoices.map((choice) => (
                      <option key={`repo-modelo-aberto-mapa-${choice}`} value={choice}>{choice}</option>
                    ))}
                  </select>
                </div>
                <MapFrame html={modeloAbertoMapaHtml} />
              </>
            ) : null}

            {modeloAbertoActiveTab === 'dados_mercado' ? <DataTable table={modeloAbertoDados} maxHeight={620} /> : null}
            {modeloAbertoActiveTab === 'metricas' ? <DataTable table={modeloAbertoEstatisticas} maxHeight={620} /> : null}

            {modeloAbertoActiveTab === 'transformacoes' ? (
              <>
                <div dangerouslySetInnerHTML={{ __html: modeloAbertoEscalasHtml }} />
                <h4 className="visualizacao-table-title">Dados com variáveis transformadas</h4>
                <DataTable table={modeloAbertoDadosTransformados} />
              </>
            ) : null}

            {modeloAbertoActiveTab === 'resumo' ? (
              <>
                <div className="equation-formats-section">
                  <h4>Equações do Modelo</h4>
                  <EquationFormatsPanel
                    equacoes={modeloAbertoEquacoes}
                    onDownload={(mode) => void onDownloadEquacaoModeloAberto(mode)}
                    disabled={modeloAbertoLoading}
                  />
                </div>
                <div dangerouslySetInnerHTML={{ __html: modeloAbertoResumoHtml }} />
              </>
            ) : null}

            {modeloAbertoActiveTab === 'coeficientes' ? <DataTable table={modeloAbertoCoeficientes} maxHeight={620} /> : null}
            {modeloAbertoActiveTab === 'obs_calc' ? <DataTable table={modeloAbertoObsCalc} maxHeight={620} /> : null}

            {modeloAbertoActiveTab === 'graficos' ? (
              <>
                <div className="plot-grid-2-fixed">
                  <PlotFigure figure={modeloAbertoPlotObsCalc} title="Obs x Calc" />
                  <PlotFigure figure={modeloAbertoPlotResiduos} title="Resíduos" />
                  <PlotFigure figure={modeloAbertoPlotHistograma} title="Histograma" />
                  <PlotFigure figure={modeloAbertoPlotCook} title="Cook" forceHideLegend />
                </div>
                <div className="plot-full-width">
                  <PlotFigure figure={modeloAbertoPlotCorr} title="Correlação" className="plot-correlation-card" />
                </div>
              </>
            ) : null}
          </div>

          {modeloAbertoError ? <div className="error-line inline-error">{modeloAbertoError}</div> : null}
        </div>
        <LoadingOverlay show={modeloAbertoLoading} label="Carregando modelo..." />
      </div>
    )
  }

  return (
    <div className="tab-content">
      <div className="repositorio-standalone-panel">
        <div className="repo-toolbar">
          <div className="repo-summary">
            <div><strong>Total:</strong> {totalModelos}</div>
            <div><strong>Fonte:</strong> {formatarFonte(fonte)}</div>
            <div><strong>Perfil:</strong> {isAdmin ? 'Administrador' : 'Leitura'}</div>
          </div>
          <div className="repo-actions">
            <button
              type="button"
              className="repo-refresh-btn"
              onClick={() => void carregarModelos()}
              disabled={loading || uploading || deleting}
            >
              Atualizar lista
            </button>
          </div>
        </div>

        {isAdmin ? (
          <div className="repo-admin-controls">
            <div className="repo-upload-row repo-upload-row-single">
              <input
                type="file"
                accept=".dai"
                onChange={(event) => setArquivoUpload(event.target.files?.[0] || null)}
                disabled={uploading || deleting}
              />
              <button type="button" onClick={() => void onUploadArquivo()} disabled={uploading || deleting || !arquivoUpload}>
                Incluir modelo no repositório
              </button>
            </div>
            {arquivoUpload ? <div className="section1-empty-hint">Arquivo selecionado: {arquivoUpload.name}</div> : null}
          </div>
        ) : (
          <div className="section1-empty-hint">Perfil de leitura: inclusão e exclusão disponíveis apenas para administradores.</div>
        )}

        {status ? <div className="status-line">{status}</div> : null}
        {error ? <div className="error-line">{error}</div> : null}

        <div className="table-container repo-table-block">
          <table className="repo-table">
            <thead>
              <tr>
                <th>Modelo</th>
                <th>Tipo</th>
                <th>Finalidade</th>
                <th>Autor</th>
                <th>Período</th>
                <th>Dados</th>
                <th>APP</th>
                <th>Status</th>
                <th className="repo-col-open">Abrir</th>
                {isAdmin ? <th className="repo-col-delete">Excluir</th> : null}
              </tr>
            </thead>
            <tbody>
              {modelos.map((item) => {
                const key = String(item.id)
                const emConfirmacao = confirmDeleteId === key
                return (
                  <tr key={key}>
                    <td>{item.nome_modelo || item.arquivo || key}</td>
                    <td>{item.tipo_imovel || '-'}</td>
                    <td>{item.finalidade || '-'}</td>
                    <td>{item.autor || '-'}</td>
                    <td>{item.periodo_dados?.label || '-'}</td>
                    <td>{item.total_dados ?? '-'}</td>
                    <td>{item.tem_app ? 'Sim' : 'Não'}</td>
                    <td>{item.status || '-'}</td>
                    <td className="repo-col-open">
                      <button
                        type="button"
                        className="repo-open-btn"
                        onClick={() => void onAbrirModelo(item)}
                        title="Abrir modelo"
                        aria-label={`Abrir ${item.nome_modelo || item.arquivo || key}`}
                      >

                      </button>
                    </td>
                    {isAdmin ? (
                      <td className="repo-col-delete">
                        {!emConfirmacao ? (
                          <button
                            type="button"
                            className="repo-delete-icon-btn"
                            onClick={() => setConfirmDeleteId(key)}
                            disabled={deleting}
                            title="Excluir modelo"
                            aria-label={`Excluir ${item.nome_modelo || item.arquivo || key}`}
                          >
                            🗑
                          </button>
                        ) : (
                          <div className="repo-delete-inline-confirm">
                            <button
                              type="button"
                              className="btn-danger repo-delete-confirm-btn"
                              onClick={() => onAbrirModalExclusao(item)}
                              disabled={deleting}
                            >
                              Excluir
                            </button>
                            <button
                              type="button"
                              className="repo-delete-cancel-btn"
                              onClick={() => setConfirmDeleteId('')}
                              disabled={deleting}
                            >
                              Cancelar
                            </button>
                          </div>
                        )}
                      </td>
                    ) : null}
                  </tr>
                )
              })}
              {!modelos.length ? (
                <tr>
                  <td colSpan={isAdmin ? 10 : 9}>
                    {loading ? 'Carregando modelos...' : 'Nenhum modelo encontrado no repositório.'}
                  </td>
                </tr>
              ) : null}
            </tbody>
          </table>
        </div>
      </div>

      {confirmarSubstituicao.open ? (
        <div className="pesquisa-modal-backdrop">
          <div className="pesquisa-modal repo-confirm-modal">
            <div className="pesquisa-modal-head">
              <div>
                <h4>Confirmar substituição de modelo</h4>
                <p>Já existe modelo com o mesmo nome no repositório.</p>
              </div>
              <button
                type="button"
                className="pesquisa-modal-close"
                onClick={() => setConfirmarSubstituicao({ open: false, substituidos: [] })}
                disabled={uploading}
              >
                Fechar
              </button>
            </div>
            <div className="repo-confirm-modal-body">
              <div className="repo-confirm-text">
                Se você continuar, o modelo existente será substituído.
              </div>
              <ul className="repo-replace-list">
                {(nomesSubstituidos.length ? nomesSubstituidos : [String(arquivoUpload?.name || '')]).map((nome) => (
                  <li key={`substituir-${nome}`}>{nome}</li>
                ))}
              </ul>
              <div className="repo-confirm-actions">
                <button
                  type="button"
                  className="repo-delete-cancel-btn"
                  onClick={() => setConfirmarSubstituicao({ open: false, substituidos: [] })}
                  disabled={uploading}
                >
                  Cancelar
                </button>
                <button
                  type="button"
                  className="btn-danger"
                  onClick={() => void onUploadArquivo(true)}
                  disabled={uploading}
                >
                  {uploading ? 'Substituindo...' : 'Confirmar substituição'}
                </button>
              </div>
            </div>
          </div>
        </div>
      ) : null}

      {confirmarExclusaoModal.open ? (
        <div className="pesquisa-modal-backdrop">
          <div className="pesquisa-modal repo-confirm-modal">
            <div className="pesquisa-modal-head">
              <div>
                <h4>{confirmarExclusaoModal.nomeModelo || 'Confirmar exclusão'}</h4>
                <p>Digite o nome completo do modelo para confirmar a exclusão.</p>
              </div>
              <button
                type="button"
                className="pesquisa-modal-close"
                onClick={onCancelarModalExclusao}
                disabled={deleting}
              >
                Fechar
              </button>
            </div>
            <form
              className="repo-confirm-modal-body"
              onSubmit={(event) => {
                event.preventDefault()
                void onConfirmarExclusaoModal()
              }}
            >
              <label className="repo-delete-typing-field">
                Nome do modelo
                <input
                  type="text"
                  value={confirmarExclusaoModal.digitado}
                  onChange={(event) => setConfirmarExclusaoModal((prev) => ({ ...prev, digitado: event.target.value }))}
                  placeholder="Digite exatamente como no título"
                  autoComplete="off"
                  disabled={deleting}
                />
              </label>
              {confirmarExclusaoModal.digitado && !exclusaoDigitadaCorreta ? (
                <div className="repo-delete-typing-hint repo-delete-typing-hint-error">
                  O texto digitado não corresponde ao nome do modelo.
                </div>
              ) : (
                <div className="repo-delete-typing-hint">
                  A exclusão será concluída somente quando o nome for digitado exatamente.
                </div>
              )}
              <div className="repo-confirm-actions">
                <button type="button" className="repo-delete-cancel-btn" onClick={onCancelarModalExclusao} disabled={deleting}>
                  Cancelar
                </button>
                <button type="submit" className="btn-danger" disabled={deleting || !exclusaoDigitadaCorreta}>
                  {deleting ? 'Excluindo...' : 'Excluir modelo'}
                </button>
              </div>
            </form>
          </div>
        </div>
      ) : null}
    </div>
  )
}