import tkinter as tk
from tkinter import ttk, messagebox
import json
import os

# 强制把数据文件存在桌面，不会有权限问题
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
DATA_FILE = os.path.join(desktop, "plan019.json")

# 加载数据


def load_data():
    if not os.path.exists(DATA_FILE):
        with open(DATA_FILE, "w", encoding="utf-8") as f:
            json.dump({"todo": []}, f, ensure_ascii=False, indent=2)
    with open(DATA_FILE, "r", encoding="utf-8") as f:
        return json.load(f)

# 保存数据


def save_data(data):
    with open(DATA_FILE, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

# 主程序界面


class CalendarPlanApp:
    def __init__(self, win):
        self.win = win
        self.win.title("019.日历提醒与计划表")
        self.win.geometry("520x400")

        # 加载数据
        self.data = load_data()

        # 标题
        tk.Label(
            win, text="日历提醒与计划表", font=("微软雅黑", 16, "bold")
        ).pack(pady=12)

        # 输入区域
        tk.Label(win, text="日期 (YYYY-MM-DD)：",
                 font=("微软雅黑", 11)).place(x=30, y=80)
        self.entry_date = tk.Entry(win, width=22, font=("微软雅黑", 11))
        self.entry_date.place(x=180, y=80)

        tk.Label(win, text="待办事项：", font=("微软雅黑", 11)).place(x=30, y=120)
        self.entry_task = tk.Entry(win, width=35, font=("微软雅黑", 11))
        self.entry_task.place(x=180, y=120)

        # 按钮
        tk.Button(win, text="添加计划", width=12,
                  command=self.add_plan).place(x=50, y=170)
        tk.Button(win, text="查看全部", width=12,
                  command=self.show_all).place(x=180, y=170)
        tk.Button(win, text="清空输入", width=12,
                  command=self.clear_input).place(x=310, y=170)

    # 添加计划
    def add_plan(self):
        date = self.entry_date.get().strip()
        task = self.entry_task.get().strip()

        if not date or not task:
            messagebox.showwarning("提示", "日期和事项都不能为空！")
            return

        self.data["todo"].append({
            "date": date,
            "task": task,
            "done": False
        })
        save_data(self.data)
        messagebox.showinfo("成功", "计划已添加！")
        self.clear_input()

    # 查看所有计划
    def show_all(self):
        window = tk.Toplevel(self.win)
        window.title("所有计划")
        window.geometry("500x350")

        text = tk.Text(window, font=("微软雅黑", 11))
        text.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        if not self.data["todo"]:
            text.insert(tk.END, "暂无计划")
            return

        for i, item in enumerate(self.data["todo"], 1):
            status = "✅ 已完成" if item["done"] else "⏳ 未完成"
            text.insert(
                tk.END, f"{i}. {item['date']}  {item['task']}  {status}\n")

    # 清空输入框
    def clear_input(self):
        self.entry_date.delete(0, tk.END)
        self.entry_task.delete(0, tk.END)


if __name__ == "__main__":
    root = tk.Tk()
    app = CalendarPlanApp(root)
    root.mainloop()
