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

from UI.searchpeople_get import Ui_searchpeople_get
from .LogerinTxt import app_logger


class searchpeople_get(Ui_searchpeople_get, QWidget):
    searchpeoplelist_signal = QtCore.pyqtSignal(list)

    def __init__(self, parent=None):
        super().__init__(parent)
        try:
            self.setupUi(self)
            self.pushButton.clicked.connect(self.btn)
            self.searchpeoplelist = None
            self.ipinfo = None
            self.Allpeoplelist = {}
            self.selectedtime = None
            self.selectedbc = None
        except Exception as e:
            app_logger.log_error(e)

    def btn(self):
        try:
            searchpeoplelist = []
            for row in range(self.tableWidgetFlight.rowCount()):
                item = self.tableWidgetFlight.item(row, 2)
                if item is not None and item.checkState() == Qt.CheckState.Checked:
                    name = self.tableWidgetFlight.item(row, 1).text()
                    searchpeoplelist.append(name)
            # print(searchpeoplelist)
            self.searchpeoplelist = searchpeoplelist
            self.searchpeoplelist_signal.emit(self.searchpeoplelist)
            self.close()
        except Exception as e:
            app_logger.log_error(e)

    def start(self):
        try:
            self.show()
            peopleLabel = ["序号", "姓名", "选择"]
            diplaylist = self.Allpeoplelist
            self.tableWidgetFlight.setColumnCount(len(peopleLabel))
            self.tableWidgetFlight.setHorizontalHeaderLabels(peopleLabel)
            self.tableWidgetFlight.verticalHeader().setVisible(False)
            self.tableWidgetFlight.setRowCount(len(diplaylist.keys()))

            for row in diplaylist.keys():
                for colNum in range(0, 3):
                    if colNum == 0:
                        self.tableWidgetFlight.setItem(int(row), colNum, QTableWidgetItem(str(row)))
                        self.tableWidgetFlight.item(int(row), colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                    elif colNum == 1:
                        self.tableWidgetFlight.setItem(int(row), colNum,QTableWidgetItem(diplaylist[row][peopleLabel[colNum]]))
                        self.tableWidgetFlight.item(int(row), colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
                    elif colNum == 2:
                        checkbox = QTableWidgetItem()
                        checkbox.setCheckState(Qt.CheckState.Unchecked)
                        self.tableWidgetFlight.setItem(int(row), colNum, checkbox)
                        self.tableWidgetFlight.item(int(row), colNum).setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tableWidgetFlight.resizeColumnsToContents()
        except Exception as e:
            app_logger.log_error(e)