import tkinter as tk
from tkinter import ttk
from tkinter.font import Font


class AlarmConfigUI:
    def __init__(self, root):
        self.root = root
        self.root.title("报警规则配置")
        self.root.geometry("800x600")

        # 自定义字体
        self.title_font = Font(family="微软雅黑", size=12, weight="bold")
        self.text_font = Font(family="微软雅黑", size=10)

        # 创建顶部导航
        self.create_top_tabs()

        # 创建主内容区域
        self.create_main_content()

        # 创建响应措施区域
        self.create_response_section()

    def create_top_tabs(self):
        # 使用Frame模拟选项卡
        tab_frame = tk.Frame(self.root, bg="#2c3e50")
        tab_frame.pack(fill=tk.X, padx=5, pady=5)

        # 预设规则标签
        self.preset_tab = tk.Label(tab_frame,
                                   text="预设规则",
                                   font=self.title_font,
                                   bg="#3498db",
                                   fg="white",
                                   padx=20,
                                   pady=5)
        self.preset_tab.pack(side=tk.LEFT)

        # 自定义规则标签
        self.custom_tab = tk.Label(tab_frame,
                                   text="自定义规则",
                                   font=self.title_font,
                                   bg="#2980b9",
                                   fg="white",
                                   padx=20,
                                   pady=5)
        self.custom_tab.pack(side=tk.LEFT)

    def create_main_content(self):
        main_frame = tk.Frame(self.root)
        main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # 表格标题
        header_frame = tk.Frame(main_frame)
        header_frame.pack(fill=tk.X)

        columns = ["异常类型", "报警条件", "报警级别", "状态"]
        for col, width in zip(columns, [150, 300, 100, 80]):
            label = tk.Label(header_frame,
                             text=col,
                             font=self.title_font,
                             bg="#ecf0f1",
                             width=width // 10,
                             pady=5)
            label.pack(side=tk.LEFT, padx=1)

        # 表格内容
        self.tree = ttk.Treeview(main_frame,
                                 columns=columns,
                                 show="headings",
                                 selectmode="browse")

        # 配置列宽
        for col, width in zip(columns, [150, 300, 100, 80]):
            self.tree.column(col, width=width, anchor="w")
            self.tree.heading(col, text=col)

        # 添加示例数据
        data = [
            ("火灾隐患", "火焰识别+高温区域检测", "一级", "●"),
            ("违规操作", "未穿防静电服+操作区域", "二级", "●"),
            ("设备异常", "油枪持续漏油>5秒", "三级", "●"),
            ("危险行为", "吸烟+加油区停留", "二级", "●")
        ]
        for item in data:
            self.tree.insert("", "end", values=item, tags=("green",))

        self.tree.tag_configure("green", foreground="#2ecc71")
        self.tree.pack(fill=tk.BOTH, expand=True)

        # 添加规则按钮
        add_btn = tk.Button(main_frame,
                            text="+ 添加规则",
                            font=self.title_font,
                            bg="#e67e22",
                            fg="white",
                            padx=20)
        add_btn.pack(pady=10)

    def create_response_section(self):
        response_frame = tk.Frame(self.root, bg="#f1f2f6")
        response_frame.pack(fill=tk.BOTH, padx=10, pady=10)

        title_label = tk.Label(response_frame,
                               text="报警响应措施",
                               font=self.title_font,
                               bg="#34495e",
                               fg="white",
                               pady=5)
        title_label.pack(fill=tk.X)

        # 响应措施内容
        measures = [
            "一级警报：立即切断加油机电源，触发火灾报警系统",
            "二级警报：现场声光报警，通知管理员",
            "三级警报：后台记录，日常检查提醒"
        ]
        for measure in measures:
            label = tk.Label(response_frame,
                             text=measure,
                             font=self.text_font,
                             bg="#f1f2f6",
                             anchor="w")
            label.pack(fill=tk.X, padx=10, pady=5)


if __name__ == "__main__":
    root = tk.Tk()
    app = AlarmConfigUI(root)
    root.mainloop()