import sqlite3
import tkinter as tk
from tkinter import ttk

# DB Setup
conn = sqlite3.connect("auftraege.db")
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS auftraege (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    kunde TEXT,
    beschreibung TEXT,
    datum TEXT
)
""")
conn.commit()

# Funktionen
def add_auftrag():
    kunde = entry_kunde.get()
    beschreibung = entry_beschreibung.get()
    datum = entry_datum.get()

    cursor.execute("INSERT INTO auftraege (kunde, beschreibung, datum) VALUES (?, ?, ?)",
                   (kunde, beschreibung, datum))
    conn.commit()
    load_data()

def load_data():
    for row in tree.get_children():
        tree.delete(row)

    cursor.execute("SELECT * FROM auftraege")
    for row in cursor.fetchall():
        tree.insert("", "end", values=row)

# GUI
root = tk.Tk()
root.title("Auftragsverwaltung")

tk.Label(root, text="Kunde").grid(row=0, column=0)
entry_kunde = tk.Entry(root)
entry_kunde.grid(row=0, column=1)

tk.Label(root, text="Beschreibung").grid(row=1, column=0)
entry_beschreibung = tk.Entry(root)
entry_beschreibung.grid(row=1, column=1)

tk.Label(root, text="Datum").grid(row=2, column=0)
entry_datum = tk.Entry(root)
entry_datum.grid(row=2, column=1)

tk.Button(root, text="Speichern", command=add_auftrag).grid(row=3, column=0, columnspan=2)

tree = ttk.Treeview(root, columns=("ID", "Kunde", "Beschreibung", "Datum"), show="headings")
for col in ("ID", "Kunde", "Beschreibung", "Datum"):
    tree.heading(col, text=col)

tree.grid(row=4, column=0, columnspan=2)

load_data()

root.mainloop()
