452 lines
18 KiB
C#
452 lines
18 KiB
C#
using CPM;
|
|
using DAL;
|
|
using MLL;
|
|
using BLL;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using System.Windows.Forms;
|
|
|
|
namespace UI
|
|
{
|
|
public class ClientesEnderecosCadastroPanel : FormularioModelo
|
|
{
|
|
// ── CAMPOS DO FORMULÁRIO ──────────────────────────────────────────────
|
|
private LV_TEXTBOX1 txtId = null!;
|
|
private LV_TEXTBOX1 txtCodigo = null!;
|
|
|
|
// Cliente — busca
|
|
private LV_TEXTBOX1 txtCodCliente = null!;
|
|
private Button btnBuscarCliente = null!;
|
|
|
|
// Endereço
|
|
private LV_TEXTBOX1 txtCep = null!;
|
|
private Button btnBuscarCep = null!;
|
|
private LV_TEXTBOX1 txtLogradouro = null!;
|
|
private LV_TEXTBOX1 txtNumero = null!;
|
|
private LV_TEXTBOX1 txtComplem = null!;
|
|
private LV_TEXTBOX1 txtBairro = null!;
|
|
private LV_TEXTBOX1 txtCidade = null!;
|
|
private LV_TEXTBOX1 txtUf = null!;
|
|
private LV_TEXTBOX1 txtDataCadastro = null!;
|
|
|
|
// ── ESTADO ────────────────────────────────────────────────────────────
|
|
private enum ModoFormulario { Visualizacao, Novo, Edicao }
|
|
private ModoFormulario _modo = ModoFormulario.Visualizacao;
|
|
private int _idAtual = 0;
|
|
|
|
// ── BLL ───────────────────────────────────────────────────────────────
|
|
// private ClientesEnderecosBLL _bll = null!;
|
|
|
|
// ── CONSTRUTOR ────────────────────────────────────────────────────────
|
|
public ClientesEnderecosCadastroPanel()
|
|
{
|
|
Titulo = "Cadastro de Endereços de Clientes";
|
|
BuildForm();
|
|
AplicarModo(ModoFormulario.Visualizacao);
|
|
}
|
|
|
|
// ── CONSTRUÇÃO DOS CAMPOS ─────────────────────────────────────────────
|
|
private void BuildForm()
|
|
{
|
|
// ── SEÇÃO: IDENTIFICAÇÃO ──────────────────────────────────────────
|
|
content.Controls.Add(CreateSectionHeader("Identificação", 20));
|
|
|
|
txtId = AddInput(content, "ID", 20, 58, 80, 32, readOnly: true);
|
|
txtCodigo = AddInput(content, "Código", 120, 58, 160, 32);
|
|
txtCodigo.MaxLength = 50;
|
|
|
|
// ── SEÇÃO: CLIENTE ────────────────────────────────────────────────
|
|
content.Controls.Add(CreateSectionHeader("Cliente", 110));
|
|
|
|
var lblCodCliente = new Label
|
|
{
|
|
Text = "Cód. Cliente",
|
|
Location = new Point(20, 148),
|
|
Font = new Font("Segoe UI", 7.5f, FontStyle.Bold),
|
|
ForeColor = TextDark,
|
|
AutoSize = true
|
|
};
|
|
txtCodCliente = new LV_TEXTBOX1
|
|
{
|
|
Location = new Point(20, 164),
|
|
Size = new Size(200, 32),
|
|
BorderColor = BorderColor,
|
|
BorderFocusColor = AccentBlue,
|
|
ReadOnly = true,
|
|
BackColor = ReadOnlyBg
|
|
};
|
|
btnBuscarCliente = new Button
|
|
{
|
|
Text = "🔍 Buscar",
|
|
Location = new Point(228, 164),
|
|
Size = new Size(90, 32),
|
|
BackColor = AccentBlue,
|
|
ForeColor = Color.White,
|
|
Font = new Font("Segoe UI Semibold", 8.5f),
|
|
Cursor = Cursors.Hand,
|
|
FlatStyle = FlatStyle.Flat,
|
|
Enabled = false
|
|
};
|
|
btnBuscarCliente.FlatAppearance.BorderSize = 0;
|
|
btnBuscarCliente.Click += BtnBuscarCliente_Click;
|
|
content.Controls.Add(lblCodCliente);
|
|
content.Controls.Add(txtCodCliente);
|
|
content.Controls.Add(btnBuscarCliente);
|
|
|
|
// ── SEÇÃO: ENDEREÇO ───────────────────────────────────────────────
|
|
content.Controls.Add(CreateSectionHeader("Endereço", 218));
|
|
|
|
// CEP + botão buscar
|
|
var lblCep = new Label
|
|
{
|
|
Text = "CEP",
|
|
Location = new Point(20, 256),
|
|
Font = new Font("Segoe UI", 7.5f, FontStyle.Bold),
|
|
ForeColor = TextDark,
|
|
AutoSize = true
|
|
};
|
|
txtCep = new LV_TEXTBOX1
|
|
{
|
|
Location = new Point(20, 272),
|
|
Size = new Size(140, 32),
|
|
BorderColor = BorderColor,
|
|
BorderFocusColor = AccentBlue,
|
|
MaxLength = 9
|
|
};
|
|
btnBuscarCep = new Button
|
|
{
|
|
Text = "🔍 Buscar CEP",
|
|
Location = new Point(168, 272),
|
|
Size = new Size(120, 32),
|
|
BackColor = AccentBlue,
|
|
ForeColor = Color.White,
|
|
Font = new Font("Segoe UI Semibold", 8.5f),
|
|
Cursor = Cursors.Hand,
|
|
FlatStyle = FlatStyle.Flat,
|
|
Enabled = false
|
|
};
|
|
btnBuscarCep.FlatAppearance.BorderSize = 0;
|
|
btnBuscarCep.Click += BtnBuscarCep_Click;
|
|
content.Controls.Add(lblCep);
|
|
content.Controls.Add(txtCep);
|
|
content.Controls.Add(btnBuscarCep);
|
|
|
|
// Logradouro — preenchido via CEP mas editável para ajustes
|
|
txtLogradouro = AddInput(content, "Logradouro", 20, 334, 460, 32);
|
|
txtLogradouro.MaxLength = 200;
|
|
|
|
txtNumero = AddInput(content, "Número", 500, 334, 100, 32);
|
|
txtNumero.MaxLength = 20;
|
|
|
|
txtComplem = AddInput(content, "Complemento", 20, 396, 280, 32);
|
|
txtComplem.MaxLength = 100;
|
|
|
|
txtBairro = AddInput(content, "Bairro", 320, 396, 280, 32);
|
|
txtBairro.MaxLength = 100;
|
|
|
|
txtCidade = AddInput(content, "Cidade", 20, 458, 340, 32);
|
|
txtCidade.MaxLength = 100;
|
|
|
|
txtUf = AddInput(content, "UF", 380, 458, 60, 32);
|
|
txtUf.MaxLength = 2;
|
|
|
|
// ── SEÇÃO: AUDITORIA ──────────────────────────────────────────────
|
|
content.Controls.Add(CreateSectionHeader("Auditoria", 516));
|
|
|
|
txtDataCadastro = AddInput(content, "Data de Cadastro", 20, 554, 200, 32, readOnly: true);
|
|
|
|
content.Height = 620;
|
|
}
|
|
|
|
// ── CAMPOS PREENCHIDOS PELO CEP — editáveis mas com visual distinto ───
|
|
private LV_TEXTBOX1[] CamposEndereco() => new[]
|
|
{ txtLogradouro, txtBairro, txtCidade, txtUf };
|
|
|
|
// ── CAMPOS EDITÁVEIS NORMAIS ──────────────────────────────────────────
|
|
private LV_TEXTBOX1[] CamposEditaveis() => new[]
|
|
{ txtCodigo, txtCep, txtLogradouro, txtNumero, txtComplem, txtBairro, txtCidade, txtUf };
|
|
|
|
// ── MODOS DO FORMULÁRIO ───────────────────────────────────────────────
|
|
private void AplicarModo(ModoFormulario modo)
|
|
{
|
|
_modo = modo;
|
|
|
|
bool editando = modo == ModoFormulario.Novo || modo == ModoFormulario.Edicao;
|
|
|
|
foreach (var txt in CamposEditaveis())
|
|
{
|
|
txt.ReadOnly = !editando;
|
|
txt.BackColor = editando ? Color.White : ReadOnlyBg;
|
|
txt.BorderColor = editando ? BorderColor : ReadOnlyBorder;
|
|
}
|
|
|
|
btnBuscarCliente.Enabled = editando;
|
|
btnBuscarCep.Enabled = editando;
|
|
|
|
btnNovo.Enabled = !editando;
|
|
btnAlterar.Enabled = !editando && _idAtual > 0;
|
|
btnExcluir.Enabled = !editando && _idAtual > 0;
|
|
btnLocalizar.Enabled = !editando;
|
|
btnSalvar.Enabled = editando;
|
|
btnCancelar.Enabled = editando;
|
|
|
|
if (modo == ModoFormulario.Novo) LimparCampos();
|
|
}
|
|
|
|
// ── BUSCA DE CLIENTE ──────────────────────────────────────────────────
|
|
private void BtnBuscarCliente_Click(object? sender, EventArgs e)
|
|
{
|
|
// TODO: abrir formulário de busca de clientes
|
|
// var frm = new FormularioBuscarCliente();
|
|
// if (frm.ShowDialog() == DialogResult.OK)
|
|
// txtCodCliente.Text = frm.ClienteSelecionado.CODIGO;
|
|
|
|
MessageBox.Show("Implemente o formulário de busca de clientes.",
|
|
"Buscar Cliente", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
// ── BUSCA DE CEP (ViaCEP) ─────────────────────────────────────────────
|
|
private async void BtnBuscarCep_Click(object? sender, EventArgs e)
|
|
{
|
|
string cep = txtCep.Text.Trim().Replace("-", "").Replace(".", "");
|
|
|
|
if (cep.Length != 8)
|
|
{
|
|
MessageBox.Show("Informe um CEP válido com 8 dígitos.", "CEP inválido",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtCep.Focus();
|
|
return;
|
|
}
|
|
|
|
btnBuscarCep.Enabled = false;
|
|
btnBuscarCep.Text = "Buscando...";
|
|
|
|
try
|
|
{
|
|
using var http = new HttpClient();
|
|
var json = await http.GetStringAsync($"https://viacep.com.br/ws/{cep}/json/");
|
|
using var doc = JsonDocument.Parse(json);
|
|
var root = doc.RootElement;
|
|
|
|
if (root.TryGetProperty("erro", out _))
|
|
{
|
|
MessageBox.Show("CEP não encontrado.", "CEP inválido",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
txtLogradouro.Text = root.GetProperty("logradouro").GetString() ?? string.Empty;
|
|
txtBairro.Text = root.GetProperty("bairro").GetString() ?? string.Empty;
|
|
txtCidade.Text = root.GetProperty("localidade").GetString() ?? string.Empty;
|
|
txtUf.Text = root.GetProperty("uf").GetString() ?? string.Empty;
|
|
|
|
// Foca no número após preencher o endereço
|
|
txtNumero.Focus();
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("Não foi possível buscar o CEP. Verifique sua conexão.",
|
|
"Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
btnBuscarCep.Enabled = true;
|
|
btnBuscarCep.Text = "🔍 Buscar CEP";
|
|
}
|
|
}
|
|
|
|
// ── POPULAR / LIMPAR ──────────────────────────────────────────────────
|
|
private void PopularCampos(ModeloClientesEnderecos m)
|
|
{
|
|
_idAtual = m.ID_COD_CLIENTE_END;
|
|
|
|
txtId.Text = m.ID_COD_CLIENTE_END.ToString();
|
|
txtCodigo.Text = m.CODIGO;
|
|
txtCodCliente.Text = m.COD_CLIENTE;
|
|
txtCep.Text = m.CEP;
|
|
txtLogradouro.Text = m.LOGRADOURO;
|
|
txtNumero.Text = m.NUMERO;
|
|
txtComplem.Text = m.COMPLEM;
|
|
txtBairro.Text = m.BAIRRO;
|
|
txtCidade.Text = m.CIDADE;
|
|
txtUf.Text = m.UF;
|
|
txtDataCadastro.Text = m.DATA_CADASTRO;
|
|
}
|
|
|
|
private void LimparCampos()
|
|
{
|
|
_idAtual = 0;
|
|
|
|
txtId.Text = string.Empty;
|
|
txtCodigo.Text = string.Empty;
|
|
txtCodCliente.Text = string.Empty;
|
|
txtCep.Text = string.Empty;
|
|
txtLogradouro.Text = string.Empty;
|
|
txtNumero.Text = string.Empty;
|
|
txtComplem.Text = string.Empty;
|
|
txtBairro.Text = string.Empty;
|
|
txtCidade.Text = string.Empty;
|
|
txtUf.Text = string.Empty;
|
|
txtDataCadastro.Text = string.Empty;
|
|
}
|
|
|
|
// ── VALIDAÇÃO ─────────────────────────────────────────────────────────
|
|
private bool Validar()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(txtCodCliente.Text))
|
|
{
|
|
MessageBox.Show("Selecione um cliente.", "Validação",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
btnBuscarCliente.Focus();
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(txtCep.Text))
|
|
{
|
|
MessageBox.Show("Informe o CEP.", "Validação",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtCep.Focus();
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(txtLogradouro.Text))
|
|
{
|
|
MessageBox.Show("Informe o logradouro.", "Validação",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtLogradouro.Focus();
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(txtCidade.Text))
|
|
{
|
|
MessageBox.Show("Informe a cidade.", "Validação",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtCidade.Focus();
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(txtUf.Text))
|
|
{
|
|
MessageBox.Show("Informe a UF.", "Validação",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtUf.Focus();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// ── MONTAR MODELO ─────────────────────────────────────────────────────
|
|
private ModeloClientesEnderecos MontarModelo() => new ModeloClientesEnderecos(
|
|
_idAtual,
|
|
txtCodigo.Text.Trim(),
|
|
txtCodCliente.Text.Trim(),
|
|
txtLogradouro.Text.Trim(),
|
|
txtNumero.Text.Trim(),
|
|
txtComplem.Text.Trim(),
|
|
txtBairro.Text.Trim(),
|
|
txtCidade.Text.Trim(),
|
|
txtUf.Text.Trim().ToUpper(),
|
|
txtCep.Text.Trim(),
|
|
txtDataCadastro.Text.Trim()
|
|
);
|
|
|
|
// ── EVENTOS ABSTRATOS ─────────────────────────────────────────────────
|
|
protected override void OnNovo()
|
|
{
|
|
AplicarModo(ModoFormulario.Novo);
|
|
btnBuscarCliente.Focus();
|
|
}
|
|
|
|
protected override void OnAlterar()
|
|
{
|
|
if (_idAtual == 0) return;
|
|
AplicarModo(ModoFormulario.Edicao);
|
|
txtCep.Focus();
|
|
}
|
|
|
|
protected override void OnExcluir()
|
|
{
|
|
if (_idAtual == 0) return;
|
|
|
|
var confirm = MessageBox.Show(
|
|
$"Deseja realmente excluir o endereço \"{txtLogradouro.Text}, {txtNumero.Text}\"?",
|
|
"Confirmar exclusão",
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Warning);
|
|
|
|
if (confirm != DialogResult.Yes) return;
|
|
|
|
try
|
|
{
|
|
// TODO: _bll.Excluir(_idAtual);
|
|
MessageBox.Show("Endereço excluído com sucesso.", "Sucesso",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
LimparCampos();
|
|
AplicarModo(ModoFormulario.Visualizacao);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Erro ao excluir:\n{ex.Message}", "Erro",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
protected override void OnLocalizar()
|
|
{
|
|
// TODO: abrir formulário de busca de endereços
|
|
// var frm = new FormularioBuscarClientesEnderecos();
|
|
// if (frm.ShowDialog() == DialogResult.OK)
|
|
// {
|
|
// PopularCampos(frm.EnderecoSelecionado);
|
|
// AplicarModo(ModoFormulario.Visualizacao);
|
|
// }
|
|
|
|
MessageBox.Show("Implemente o formulário de busca de endereços.",
|
|
"Localizar", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
protected override void OnSalvar()
|
|
{
|
|
if (!Validar()) return;
|
|
|
|
var modelo = MontarModelo();
|
|
|
|
try
|
|
{
|
|
if (_modo == ModoFormulario.Novo)
|
|
{
|
|
// TODO: modelo.DATA_CADASTRO = DateTime.Now.ToString("dd/MM/yyyy");
|
|
// _bll.Criar(modelo);
|
|
MessageBox.Show("Endereço cadastrado com sucesso!", "Sucesso",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
// TODO: _bll.Atualizar(modelo);
|
|
MessageBox.Show("Endereço atualizado com sucesso!", "Sucesso",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
AplicarModo(ModoFormulario.Visualizacao);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Erro ao salvar:\n{ex.Message}", "Erro",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
protected override void OnCancelar()
|
|
{
|
|
if (_idAtual > 0)
|
|
{
|
|
// TODO: recarregar do banco se necessário
|
|
}
|
|
LimparCampos();
|
|
AplicarModo(ModoFormulario.Visualizacao);
|
|
}
|
|
}
|
|
} |