from datetime import datetime

from common.sql_lite import get_global_db_connection
from db.batch_msg import batch_msg_get_list, batch_msg_status, batch_msg_type
from ui.batch_task.ui_batch_task_detail import WinGUIBatchTaskDetail


class WinBatchTaskDetail(WinGUIBatchTaskDetail):
    def __init__(self):
        super().__init__()
        self.label_list = ["全部"]
        self.label_map = {
            "全部": 0
        }
        self.contacts = []


def open_batch_task_detail_win(task_id: int, task_info: tuple):
    # 创建窗口对象
    win_batch_task_detail = WinBatchTaskDetail()

    win_batch_task_detail.tk_label_frame_content.tk_text_content.insert(1.0, task_info[4])
    win_batch_task_detail.tk_label_frame_content.tk_text_content.config(state='disabled')

    connection = get_global_db_connection()
    cursor = connection.cursor()
    try:
        results = batch_msg_get_list(cursor, task_id)
    finally:
        cursor.close()

    for result in results:
        updated_at = datetime.fromtimestamp(result["updated_at"]).strftime("%Y-%m-%d %H:%M:%S") if result["updated_at"] is not None else ""
        status = batch_msg_status[result["status"]]
        type = batch_msg_type[result["type"]]
        win_batch_task_detail.tk_label_frame_contact.tk_table_contact_list.insert('', "end", values=(result["wxid"], type, result["nickname"], updated_at, status))

    win_batch_task_detail.mainloop()
