
import datetime

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

from UI.checkLoadSingal import Ui_checkLoadSingal
from .LogerinTxt import app_logger
from Functions.apiFunction import api_fc


class checkLoadSingal(Ui_checkLoadSingal, QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        try:
            self.setupUi(self)
            self.worknum = None
            self.name = None
            self.selectedtime = None
            self.ipinfo = None
        except Exception as e:
            app_logger.log_error(e)

    def start(self):
        try:
            # print(self.worknum)
            # print(self.name)
            # print(self.selectedtime)
            # print(self.ipinfo)
            self.label_3.setText(self.name)
            selectedtime_time = str(self.selectedtime) + " 00:00:00"
            now = datetime.datetime.strptime(selectedtime_time, "%Y%m%d %H:%M:%S")
            yesterday = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
            self.label_5.setText("%s/%s" % (str(yesterday)[0:10], str(now)[0:10]))
            # print(now)
            # print(yesterday)
            # ip = "http://" + str(self.ipinfo) + "/static/checkLoadSingal" + "/" + str(self.worknum) + "/" + str(
            #     yesterday) + "/" + str(now)
            try:
                # res = requests.get(url=ip, timeout=30).json()
                res = api_fc.request_post('/static/checkLoadSingal',
                                          worknum=str(self.worknum),
                                          time1=str(yesterday),
                                          time2=str(now))
                # print(res)
            except Exception as e:
                # print('登录服务器失败')
                app_logger.log_error('登录服务器失败, res获取失败')
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
                app_logger.log_error(e)

            self.show()
            peopleLabel = ["序号", "保障日期", "类型", "机号", "机位", "开始时间", "结束时间"]
            self.tableWidgetFlight.setColumnCount(len(peopleLabel))
            self.tableWidgetFlight.verticalHeader().setVisible(False)
            self.tableWidgetFlight.setHorizontalHeaderLabels(peopleLabel)
            col = len(peopleLabel)
            diplaylist = res
            self.tableWidgetFlight.setRowCount(len(diplaylist.keys()))
            for row in diplaylist.keys():
                for colNum in range(0, col):
                    if colNum == 0:
                        self.tableWidgetFlight.setItem(int(row), colNum, QTableWidgetItem(str(row)))
                        self.tableWidgetFlight.item(int(row), colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                    else:
                        self.tableWidgetFlight.setItem(int(row), colNum,QTableWidgetItem(diplaylist[row][peopleLabel[colNum]]))
                        self.tableWidgetFlight.item(int(row), colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tableWidgetFlight.resizeColumnsToContents()
        except Exception as e:
            app_logger.log_error(e)