pets/visao/componentes/dialogo_nova_adocao.py

87 lines
3.6 KiB
Python

import tkinter as tk
from tkinter import ttk, messagebox
from datetime import datetime
class DialogoNovaAdocao(tk.Toplevel):
def __init__(self, parent, bd):
super().__init__(parent)
self.bd = bd
self.result = None
self.title("Registrar Nova 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. Filtrar a lista de pets para mostrar apenas os disponíveis
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]
self._criar_widgets()
def _criar_widgets(self):
main_frame = tk.Frame(self, bg="#1e1e1e", padx=20, pady=20)
main_frame.pack(fill="both", expand=True)
# Adotante
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))
# Pets
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")
# Data
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.insert(0, datetime.now().strftime("%d-%m-%Y"))
self.entry_data.pack(fill="x", pady=(0, 20))
# Botões
botoes_frame = tk.Frame(main_frame, bg="#1e1e1e")
botoes_frame.pack(fill="x")
btn_ok = tk.Button(botoes_frame, text="Registrar", 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 _on_ok(self):
idx_pessoa = self.combo_pessoas.current()
if idx_pessoa == -1:
messagebox.showerror("Erro", "Por favor, selecione um adotante.", parent=self)
return
indices_pets = self.listbox_pets.curselection()
if not indices_pets:
messagebox.showerror("Erro", "Por favor, selecione pelo menos um pet.", parent=self)
return
data = self.entry_data.get()
pessoa_selecionada = self.pessoas[idx_pessoa]
pets_selecionados = [self.pets[i] for i in indices_pets]
self.result = {"pessoa": pessoa_selecionada, "pets": pets_selecionados, "data": data}
self.destroy()