
import datetime

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QWidget , QMessageBox , QTableWidgetItem

from UI.checkLogs import Ui_checkLogs
from .LogerinTxt import app_logger
from Functions.apiFunction import api_fc


class checkLogs(Ui_checkLogs, QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.ipinfo = None
        self.endtime = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
        self.starttime = (datetime.date.today()).strftime("%Y-%m-%d %H:%M:%S")
        self.dateTimeEdit.setDateTime(datetime.datetime.strptime(self.starttime, "%Y-%m-%d %H:%M:%S"))
        self.dateTimeEdit_2.setDateTime(datetime.datetime.strptime(self.endtime, "%Y-%m-%d %H:%M:%S"))
        self.lineEdit.textChanged.connect(self.changed)
        self.peopleLabel = ["序号", "航班编号", "警告信息", "信息状态", "提示信息", "产生时间", "处理人", "处理时间", "变更字段", "显示对象", "提示内容"]
        self.tableWidgetFlight.setColumnCount(len(self.peopleLabel))
        self.tableWidgetFlight.verticalHeader().setVisible(False)
        self.tableWidgetFlight.setHorizontalHeaderLabels(self.peopleLabel)
        self.pushButton.clicked.connect(self.btn)

    def start(self):
        self.show()
        try:
            # ip = "http://" + str(self.ipinfo) + "/static/checkLogs/" + self.starttime + "/" + self.endtime
            # self.res = requests.get(url=ip, timeout=30).json()
            self.res = api_fc.request_post('/static/checkLogs',start=self.starttime,end=self.endtime)["data"]
        except Exception as e:
            # print('登录服务器失败')
            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
            app_logger.log_error(e)
            self.res = {}
        col = len(self.peopleLabel)
        diplaylist = self.res
        self.tableWidgetFlight.setRowCount(len(diplaylist))
        rownum = 0
        for row in diplaylist:
            for colNum in range(0, col):
                if colNum == 0:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(rownum)))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                else:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(row[colNum])))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignLeft)
            rownum += 1
        self.tableWidgetFlight.resizeColumnsToContents()

    def changed(self):
        res = []
        for i in self.res:
            if str(self.lineEdit.text()) in str(i):
                res.append(i)
        col = len(self.peopleLabel)
        diplaylist = res
        self.tableWidgetFlight.setRowCount(len(diplaylist))
        rownum = 0
        for row in diplaylist:
            for colNum in range(0, col):
                if colNum == 0:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(rownum)))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                else:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(row[colNum])))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignLeft)
            rownum += 1
        self.tableWidgetFlight.resizeColumnsToContents()

    def btn(self):
        self.starttime = self.dateTimeEdit.text()
        self.endtime = self.dateTimeEdit_2.text()
        try:
            # ip = "http://" + str(self.ipinfo) + "/static/checkLogs/" + self.starttime + "/" + self.endtime
            # self.res = requests.get(url=ip, timeout=30).json()
            self.res = api_fc.request_post('/static/checkLogs',start=self.starttime,end=self.endtime)["data"]
        except Exception as e :
            # print('登录服务器失败')
            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
            app_logger.log_error(e)
            self.res = {}
        col = len(self.peopleLabel)
        diplaylist = self.res
        self.tableWidgetFlight.setRowCount(len(diplaylist))
        rownum = 0
        for row in diplaylist:
            for colNum in range(0, col):
                if colNum == 0:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(rownum)))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                else:
                    self.tableWidgetFlight.setItem(rownum, colNum, QTableWidgetItem(str(row[colNum])))
                    self.tableWidgetFlight.item(rownum, colNum).setTextAlignment(Qt.AlignmentFlag.AlignLeft)
            rownum += 1
        self.tableWidgetFlight.resizeColumnsToContents()