pets/visao/componentes/dialogo_editar_adocao.py

84 lines
3.7 KiB
Python

import tkinter as tk
from tkinter import ttk, messagebox
from datetime import datetime
class DialogoEditarAdocao(tk.Toplevel):
def __init__(self, parent, bd, adocao_atual):
super().__init__(parent)
self.bd = bd
self.adocao_atual = adocao_atual
self.result = None
self.title("Editar Adoção")
self.geometry("400x450")
self.configure(bg="#1e1e1e")
self.transient(parent)
self.grab_set()
# 1. Obter os IDs de todos os pets que já foram adotados
ids_pets_adotados = set()
for adocao in self.bd.adocoes.listar_todos():
for item in adocao.itens:
ids_pets_adotados.add(item.pet.id)
# 2. Obter os IDs dos pets que pertencem a ESTA adoção específica
ids_pets_desta_adocao = {item.pet.id for item in self.adocao_atual.itens}
# 3. A lista de pets para edição inclui os disponíveis E os que já estão nesta adoção
self.pessoas = self.bd.pessoas.listar_todos()
self.pets = [p for p in self.bd.pets.listar_todos() if p.id not in ids_pets_adotados or p.id in ids_pets_desta_adocao]
self._criar_widgets()
self._preencher_dados()
def _criar_widgets(self):
main_frame = tk.Frame(self, bg="#1e1e1e", padx=20, pady=20)
main_frame.pack(fill="both", expand=True)
tk.Label(main_frame, text="Adotante:", bg="#1e1e1e", fg="white").pack(anchor="w")
self.combo_pessoas = ttk.Combobox(main_frame, values=[p.nome for p in self.pessoas], state="readonly")
self.combo_pessoas.pack(fill="x", pady=(0, 10))
tk.Label(main_frame, text="Selecione os Pets (use Ctrl ou Shift para múltiplos):", bg="#1e1e1e", fg="white").pack(anchor="w")
pet_frame = tk.Frame(main_frame)
pet_frame.pack(fill="both", expand=True, pady=(0, 10))
self.listbox_pets = tk.Listbox(pet_frame, selectmode=tk.MULTIPLE, bg="#2c2c2c", fg="white", selectbackground="#1f6aa5")
for pet in self.pets:
self.listbox_pets.insert(tk.END, f"{pet.nome} ({pet.especie})")
scrollbar = ttk.Scrollbar(pet_frame, orient="vertical", command=self.listbox_pets.yview)
self.listbox_pets.configure(yscrollcommand=scrollbar.set)
self.listbox_pets.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
tk.Label(main_frame, text="Data da Adoção (DD-MM-AAAA):", bg="#1e1e1e", fg="white").pack(anchor="w")
self.entry_data = tk.Entry(main_frame, bg="#2c2c2c", fg="white", insertbackground="white")
self.entry_data.pack(fill="x", pady=(0, 20))
botoes_frame = tk.Frame(main_frame, bg="#1e1e1e")
botoes_frame.pack(fill="x")
btn_ok = tk.Button(botoes_frame, text="Salvar", command=self._on_ok, bg="#4CAF50", fg="white", relief="flat")
btn_ok.pack(side="right", padx=(10, 0))
btn_cancelar = tk.Button(botoes_frame, text="Cancelar", command=self.destroy, bg="#f44336", fg="white", relief="flat")
btn_cancelar.pack(side="right")
def _preencher_dados(self):
try:
idx_pessoa = [p.id for p in self.pessoas].index(self.adocao_atual.adotante.id)
self.combo_pessoas.current(idx_pessoa)
except (ValueError, AttributeError):
pass
ids_pets_adotados = {item.pet.id for item in self.adocao_atual.itens}
for i, pet in enumerate(self.pets):
if pet.id in ids_pets_adotados:
self.listbox_pets.selection_set(i)
if self.adocao_atual.itens:
# Corrigido: Pega a data do primeiro item como referência
self.entry_data.insert(0, self.adocao_atual.itens[0].data)