204 lines
8.7 KiB
C#
204 lines
8.7 KiB
C#
using BLL;
|
|
using DAL;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using MLL;
|
|
|
|
namespace UI
|
|
{
|
|
public class NotasXmlConsultaPanel : UserControl
|
|
{
|
|
// ── CORES (Mantendo seu padrão LevelOS) ──────────────────────────────────
|
|
private readonly Color AccentBlue = Color.FromArgb(37, 99, 235);
|
|
private readonly Color TextDark = Color.FromArgb(30, 41, 59);
|
|
private readonly Color BorderColor = Color.FromArgb(226, 232, 240);
|
|
private readonly Color GreenColor = Color.FromArgb(34, 197, 94);
|
|
private readonly Color MutedGray = Color.FromArgb(148, 163, 184);
|
|
private readonly Color SurfaceColor = Color.FromArgb(248, 250, 252);
|
|
|
|
// ── CONTROLES ─────────────────────────────────────────────────────────
|
|
private Panel pnlToolbar = null!;
|
|
private Panel pnlFiltros = null!;
|
|
private Panel pnlRodape = null!;
|
|
private DataGridView dgvXmls = null!;
|
|
private Label lblTotal = null!;
|
|
|
|
// ── FILTROS ESPECÍFICOS PARA XML ──────────────────────────────────────
|
|
private RoundTextBox txtFiltroChave = null!;
|
|
private RoundTextBox txtFiltroIdNota = null!;
|
|
private RoundTextBox txtDataInicio = null!;
|
|
private RoundTextBox txtDataFim = null!;
|
|
|
|
// ── DADOS ─────────────────────────────────────────────────────────────
|
|
private List<ModeloNotasXml> _todos = new();
|
|
private List<ModeloNotasXml> _filtrados = new();
|
|
private string _conexao = DadosDaConexao.ObterConexao();
|
|
|
|
public event Action<ModeloNotasXml>? OnAbrirVisualizacao;
|
|
|
|
public NotasXmlConsultaPanel()
|
|
{
|
|
Dock = DockStyle.Fill;
|
|
BackColor = Color.White;
|
|
DoubleBuffered = true;
|
|
|
|
InitializeLayout();
|
|
CarregarDadosDoBanco();
|
|
AplicarFiltros();
|
|
}
|
|
|
|
private void InitializeLayout()
|
|
{
|
|
Controls.Clear();
|
|
|
|
// 1º GRID
|
|
BuildGrid();
|
|
Controls.Add(dgvXmls);
|
|
|
|
// 2º RODAPÉ
|
|
pnlRodape = new Panel { Dock = DockStyle.Bottom, Height = 30, BackColor = SurfaceColor };
|
|
lblTotal = new Label { AutoSize = true, Location = new Point(16, 7), Font = new Font("Segoe UI", 8.5f, FontStyle.Bold), ForeColor = MutedGray, Text = "0 XMLs encontrados" };
|
|
pnlRodape.Controls.Add(lblTotal);
|
|
Controls.Add(pnlRodape);
|
|
|
|
// 3º FILTROS
|
|
pnlFiltros = new Panel { Dock = DockStyle.Top, Height = 95, BackColor = SurfaceColor, Padding = new Padding(16, 8, 16, 8) };
|
|
BuildFiltros();
|
|
Controls.Add(pnlFiltros);
|
|
|
|
// 4º TOOLBAR
|
|
pnlToolbar = new Panel { Dock = DockStyle.Top, Height = 55, BackColor = SurfaceColor };
|
|
var flow = new FlowLayoutPanel { Dock = DockStyle.Fill, Padding = new Padding(12, 10, 0, 0), BackColor = Color.Transparent };
|
|
|
|
var btnPesquisar = CreateToolbarButton("Pesquisar", AccentBlue);
|
|
var btnLimpar = CreateToolbarButton("Limpar", MutedGray);
|
|
var btnVisualizar = CreateToolbarButton("Visualizar", GreenColor);
|
|
var btnDownload = CreateToolbarButton("Salvar .XML", Color.FromArgb(99, 102, 241));
|
|
|
|
btnPesquisar.Click += (_, _) => AplicarFiltros();
|
|
btnLimpar.Click += (_, _) => LimparFiltros();
|
|
btnVisualizar.Click += (_, _) => AbrirRegistroSelecionado();
|
|
btnDownload.Click += (_, _) => BaixarXmlSelecionado();
|
|
|
|
flow.Controls.AddRange(new Control[] { btnPesquisar, btnLimpar, btnVisualizar, btnDownload });
|
|
pnlToolbar.Controls.Add(flow);
|
|
Controls.Add(pnlToolbar);
|
|
}
|
|
|
|
private void BuildFiltros()
|
|
{
|
|
txtFiltroChave = AddFiltroInput(pnlFiltros, "Chave de Acesso (44 dígitos)", 0, 8, 400);
|
|
txtFiltroIdNota = AddFiltroInput(pnlFiltros, "ID da Nota", 410, 8, 100);
|
|
|
|
txtDataInicio = AddFiltroInput(pnlFiltros, "Data Inicial", 0, 50, 130);
|
|
txtDataFim = AddFiltroInput(pnlFiltros, "Data Final", 140, 50, 130);
|
|
|
|
foreach (var txt in new[] { txtFiltroChave, txtFiltroIdNota, txtDataInicio, txtDataFim })
|
|
txt.KeyDown += (_, e) => { if (e.KeyCode == Keys.Enter) AplicarFiltros(); };
|
|
}
|
|
|
|
private void BuildGrid()
|
|
{
|
|
dgvXmls = new DataGridView
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
BackgroundColor = Color.White,
|
|
BorderStyle = BorderStyle.None,
|
|
RowHeadersVisible = false,
|
|
AllowUserToAddRows = false,
|
|
ReadOnly = true,
|
|
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
|
|
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
|
|
RowTemplate = { Height = 32 }
|
|
};
|
|
|
|
// Estilização seguindo seu padrão (omitido para brevidade, mas igual ao seu)
|
|
dgvXmls.Columns.AddRange(new DataGridViewColumn[]
|
|
{
|
|
new DataGridViewTextBoxColumn { Name = "colId", HeaderText = "ID", FillWeight = 10 },
|
|
new DataGridViewTextBoxColumn { Name = "colChave", HeaderText = "Chave de Acesso", FillWeight = 60 },
|
|
new DataGridViewTextBoxColumn { Name = "colTamanho", HeaderText = "Tamanho (KB)", FillWeight = 30 }
|
|
});
|
|
|
|
dgvXmls.CellDoubleClick += (_, e) => { if (e.RowIndex >= 0) AbrirRegistroSelecionado(); };
|
|
}
|
|
|
|
private void AplicarFiltros()
|
|
{
|
|
_filtrados = _todos.ToList();
|
|
|
|
var chave = txtFiltroChave.Text.Trim();
|
|
if (!string.IsNullOrEmpty(chave))
|
|
_filtrados = _filtrados.Where(x => x.CHAVE.Contains(chave)).ToList();
|
|
|
|
PreencherGrid();
|
|
}
|
|
|
|
private void PreencherGrid()
|
|
{
|
|
dgvXmls.Rows.Clear();
|
|
foreach (var item in _filtrados)
|
|
{
|
|
// Cálculo simples de KB para exibir no grid
|
|
double kb = (item.XML?.Length ?? 0) / 1024.0;
|
|
dgvXmls.Rows.Add(item.ID_NOTAS_XML, item.CHAVE, kb.ToString("N2"));
|
|
}
|
|
lblTotal.Text = $"{_filtrados.Count} XMLs encontrados";
|
|
}
|
|
|
|
private void AbrirRegistroSelecionado()
|
|
{
|
|
if (dgvXmls.SelectedRows.Count == 0) return;
|
|
var id = (int)dgvXmls.SelectedRows[0].Cells["colId"].Value;
|
|
var xml = _filtrados.FirstOrDefault(x => x.ID_NOTAS_XML == id);
|
|
if (xml != null) OnAbrirVisualizacao?.Invoke(xml);
|
|
}
|
|
|
|
private void BaixarXmlSelecionado()
|
|
{
|
|
if (dgvXmls.SelectedRows.Count == 0) return;
|
|
var id = (int)dgvXmls.SelectedRows[0].Cells["colId"].Value;
|
|
var nota = _filtrados.FirstOrDefault(x => x.ID_NOTAS_XML == id);
|
|
|
|
if (nota != null)
|
|
{
|
|
using var sfd = new SaveFileDialog { Filter = "XML File|*.xml", FileName = nota.CHAVE };
|
|
if (sfd.ShowDialog() == DialogResult.OK)
|
|
{
|
|
System.IO.File.WriteAllText(sfd.FileName, nota.XML);
|
|
MessageBox.Show("Arquivo salvo com sucesso!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CarregarDadosDoBanco()
|
|
{
|
|
// BLL_NotasXml bll = new BLL_NotasXml(_conexao);
|
|
// _todos = bll.Listar();
|
|
}
|
|
|
|
private RoundTextBox AddFiltroInput(Control parent, string label, int x, int y, int width)
|
|
{
|
|
parent.Controls.Add(new Label { Text = label, Location = new Point(x, y), Font = new Font("Segoe UI", 7.5f, FontStyle.Bold), ForeColor = TextDark, AutoSize = true });
|
|
var txt = new RoundTextBox { Location = new Point(x, y + 16), Size = new Size(width, 26), Radius = 4, BorderColor = BorderColor, FocusColor = AccentBlue, BackColor = Color.White };
|
|
parent.Controls.Add(txt);
|
|
return txt;
|
|
}
|
|
|
|
private RoundButton CreateToolbarButton(string text, Color color) => new RoundButton
|
|
{
|
|
Text = text,
|
|
Size = new Size(110, 32),
|
|
BackColor = color,
|
|
ForeColor = Color.White,
|
|
Font = new Font("Segoe UI Semibold", 8.5f),
|
|
Margin = new Padding(0, 0, 6, 0),
|
|
Cursor = Cursors.Hand
|
|
};
|
|
|
|
private void LimparFiltros() { /* Limpa os txts e chama AplicarFiltros */ }
|
|
}
|
|
} |