import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import os
import shutil
def create_database():
    folder_name = entry.get()
    if folder_name:
        try:
            if os.path.exists(folder_name):
                messagebox.showwarning("警告", "資料夾已經存在")
            else:
                os.mkdir(folder_name)
                listbox.insert(tk.END, folder_name)  # 將資料夾名稱添加到Listbox中
                messagebox.showinfo("成功", f"已成功建立資料夾 {folder_name}")
                update_combobox_options()
        except Exception as e:
            messagebox.showerror("錯誤", f"無法建立資料夾: {e}")
    else:
        messagebox.showwarning("警告", "請輸入資料夾名稱")
def delete_database_window():
    selected_folder = listbox.get(tk.ACTIVE)  # 獲取選取的資料庫名稱
    if selected_folder:
        delete_window = tk.Toplevel(root)
        delete_window.title("刪除資料庫")
        
        label_delete_folder = tk.Label(delete_window, text="選擇要刪除的資料庫：")
        label_delete_folder.pack()
        
        delete_combobox = ttk.Combobox(delete_window, values=listbox.get(0, tk.END))
        delete_combobox.pack()
        
        button_confirm_delete = tk.Button(delete_window, text="確定刪除", command=lambda: delete_database(delete_combobox.get()))
        button_confirm_delete.pack()
        
    else:
        messagebox.showwarning("警告", "請選擇要刪除的資料庫")
def delete_database(folder_name):
    if folder_name:
        try:
            result = messagebox.askyesno("確認", f"確定要刪除資料庫 {folder_name} 嗎？")
            if result:
                folder_path = os.path.join(os.getcwd(), folder_name)
                shutil.rmtree(folder_path)  # 刪除資料夾
                listbox.delete(listbox.get(0, tk.END).index(folder_name))  # 從Listbox中刪除資料庫名稱
                messagebox.showinfo("成功", f"已成功刪除資料庫 {folder_name}")
                update_combobox_options()
        except Exception as e:
            messagebox.showerror("錯誤", f"無法刪除資料庫: {e}")
    else:
        messagebox.showwarning("警告", "請選擇要刪除的資料庫")
def search_database():
    
    search_content = entry_search.get()  # 獲取輸入的查詢內容
    if search_content:
        if search_content in listbox.get(0, tk.END):
            messagebox.showinfo("查詢結果", f"找到資料庫：{search_content}")
        else:
            messagebox.showinfo("查詢結果", "找不到符合條件的資料庫")
    else:
        messagebox.showwarning("警告", "請輸入查詢內容")
def create_table_window():
    selected_folder = combobox.get()  # 獲取所選的資料夾名稱
    if selected_folder:
        def add_entry():
            entry_column = tk.Entry(table_window)
            entry_column.pack()
            entry_columns.append(entry_column)
            
        table_window = tk.Toplevel(root)
        table_window.title("新增表格")
        
        
        label_table_name = tk.Label(table_window, text="輸入表格名稱：")
        label_table_name.pack()
        entry_table_name = tk.Entry(table_window)
        entry_table_name.pack()
        
        label_columns = tk.Label(table_window, text="輸入欄位名稱：")
        label_columns.pack()
        
        button_add_entry = tk.Button(table_window, text="新增輸入框", command=add_entry)
        button_add_entry.pack()
        
        button_create_table = tk.Button(table_window, text="建立表格", command=lambda: create_table(selected_folder, entry_table_name.get(), entry_columns))
        button_create_table.pack()
        entry_columns = []
        for i in range(4):
            entry_column = tk.Entry(table_window)
            entry_column.pack()
            entry_columns.append(entry_column)
def create_table(selected_folder, table_name, entry_columns):
    if selected_folder and table_name:
        try:
            file_path = os.path.join(selected_folder, table_name + ".csv")
            column_names = []
            for entry in entry_columns:
                column_name = entry.get()
                if column_name:
                    column_names.append(column_name)
            
            if os.path.exists(file_path):
                messagebox.showwarning("警告", "檔案已經存在")
            else:
                with open(file_path, 'w') as file:
                    file.write(",".join(column_names) + "\n")  # 寫入表格欄位名稱
                messagebox.showinfo("成功", f"已成功建立表格 {table_name}")
        except Exception as e:
            messagebox.showerror("錯誤", f"無法建立表格: {e}")
    else:
        messagebox.showwarning("警告", "請選擇資料夾並輸入表格名稱")
        
def update_combobox_options():
    folders = os.listdir()
    folder_options = [folder for folder in folders if os.path.isdir(folder)]
    combobox['values'] = folder_options

# 創建主視窗
root = tk.Tk()
root.title("資料庫建立工具")
root.geometry("600x600")  # 設定視窗大小

# 創建標籤和輸入框
label = tk.Label(root, text="輸入資料庫名稱：")
label.pack()
entry = tk.Entry(root)
entry.pack()

# 創建按鈕
button = tk.Button(root, text="建立資料庫", command=create_database)
button.pack()
button_delete_folder = tk.Button(root, text="刪除資料庫", command=delete_database_window)
button_delete_folder.pack()

search_frame = tk.Frame(root)
search_frame.pack()

label_search = tk.Label(search_frame, text="輸入查詢內容：")
label_search.pack(side=tk.LEFT)

entry_search = tk.Entry(search_frame)
entry_search.pack(side=tk.LEFT)

button_search = tk.Button(search_frame, text="查詢", command=search_database)
button_search.pack(side=tk.LEFT)

# 創建Listbox
listbox = tk.Listbox(root)
listbox.pack()

# 列出已存在的資料夾
folders = os.listdir()
for folder in folders:
    if os.path.isdir(folder):
        listbox.insert(tk.END, folder)
        
# 創建下拉式選單
combobox = ttk.Combobox(root)
combobox.pack()
update_combobox_options()

button_create_table = tk.Button(root, text="新增表格", command=create_table_window)
button_create_table.pack()
# 開始運行主迴圈
root.mainloop() 可以在create table的地方幫我加下拉選單可以選型別嗎 每一條entry_column都要有