import base64

from PyQt6 import QtCore , QtWidgets , QtGui
from PyQt6.QtCore import QByteArray
from PyQt6.QtGui import QIcon , QPixmap
from PyQt6.QtWidgets import QCheckBox


class CheckableComboBox(QtWidgets.QComboBox):
    """支持多选的自定义ComboBox"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setEditable(True)
        self.lineEdit().setReadOnly(True)
        self.lineEdit().setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
        self.view().pressed.connect(self.handle_item_pressed)
        self.setModel(QtGui.QStandardItemModel())
        self.update_text()  # 初始化显示

    def handle_item_pressed(self, index):
        item = self.model().item(index.row())
        item.setCheckState(
            QtCore.Qt.CheckState.Unchecked if item.checkState() == QtCore.Qt.CheckState.Checked else
            QtCore.Qt.CheckState.Checked
        )
        self.update_text()  # 更新文本
        self.showPopup()  # 保持下拉框打开

    def update_text(self):
        selected_texts = []
        for i in range(self.model().rowCount()):
            item = self.model().item(i)
            if item.checkState() == QtCore.Qt.CheckState.Checked:
                selected_texts.append(item.text())
        if not selected_texts:
            self.setEditText("请选择")
        else:
            self.setEditText(", ".join(selected_texts))

        self.update()  # 强制刷新界面

    def addItem(self, text, data=None):
        item = QtGui.QStandardItem(text)
        item.setData(data)
        item.setFlags(QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsUserCheckable)
        item.setCheckState(QtCore.Qt.CheckState.Unchecked)
        self.model().appendRow(item)
        self.update_text()  # 添加项后更新显示

    def addItems(self, texts):
        for text in texts:
            self.addItem(text)
        self.update_text()  # 添加多项后更新显示

    def get_selected_items(self):
        return [
            self.model().item(i).text()
            for i in range(self.model().rowCount())
            if self.model().item(i).checkState() == QtCore.Qt.CheckState.Checked
        ]


class Ui_workloadinfos(object):
    def setupUi(self, workloadinfos):
        icon_base64 = """
        [encoded data, 360532 characters]

        """

        # 从Base64创建图标
        icon_data = base64.b64decode(icon_base64)
        pixmap = QPixmap()
        pixmap.loadFromData(QByteArray(icon_data))
        icon = QIcon(pixmap)
        workloadinfos.setObjectName("workloadinfos")
        workloadinfos.resize(1295, 794)
        workloadinfos.setWindowIcon(icon)
        self.gridLayout = QtWidgets.QGridLayout(workloadinfos)

        # 顶部控制区域
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setContentsMargins(-1, 10, -1, -1)

        self.label = QtWidgets.QLabel("人员列表：")
        self.label.setMaximumSize(QtCore.QSize(50, 16777215))
        self.horizontalLayout_5.addWidget(self.label)

        self.comboBox_peoplelist = CheckableComboBox(workloadinfos)
        self.comboBox_peoplelist.setMaximumSize(QtCore.QSize(100, 16777215))
        self.horizontalLayout_5.addWidget(self.comboBox_peoplelist)

        self.autoupdate_check = QCheckBox("自动刷新", workloadinfos)
        self.autoupdate_check.setMaximumSize(QtCore.QSize(70, 16777215))
        self.horizontalLayout_5.addWidget(self.autoupdate_check)

        self.refresh = QtWidgets.QPushButton("刷新")
        self.refresh.setMaximumSize(QtCore.QSize(35, 16777215))
        self.horizontalLayout_5.addWidget(self.refresh)

        self.clear = QtWidgets.QPushButton("清空")
        self.clear.setMaximumSize(QtCore.QSize(35, 16777215))
        self.horizontalLayout_5.addWidget(self.clear)

        self.selectall = QtWidgets.QPushButton("全选")
        self.selectall.setMaximumSize(QtCore.QSize(35, 16777215))
        self.horizontalLayout_5.addWidget(self.selectall)

        self.load_pushbutton = QtWidgets.QPushButton("导入")
        self.load_pushbutton.setMaximumSize(QtCore.QSize(35, 16777215))
        self.horizontalLayout_5.addWidget(self.load_pushbutton)
        # 搜索
        self.label_8 = QtWidgets.QLabel("")
        self.label_8.setMaximumSize(QtCore.QSize(35, 16777215))
        self.horizontalLayout_5.addWidget(self.label_8)

        self.searchinfo = QtWidgets.QLineEdit()
        self.searchinfo.setMaximumSize(QtCore.QSize(100, 16777215))
        self.searchinfo.setPlaceholderText("输入搜索内容,回车")
        self.horizontalLayout_5.addWidget(self.searchinfo)

        self.label_3 = QtWidgets.QLabel()
        self.horizontalLayout_5.addWidget(self.label_3)

        # 修正区域
        self.label_4 = QtWidgets.QLabel("修正内容")
        self.label_4.setMaximumSize(QtCore.QSize(50, 16777215))
        self.horizontalLayout_5.addWidget(self.label_4)

        self.changeinfo = QtWidgets.QLineEdit()
        self.changeinfo.setMaximumSize(QtCore.QSize(100, 16777215))
        self.horizontalLayout_5.addWidget(self.changeinfo)

        self.label_5 = QtWidgets.QLabel("人员：")
        self.label_5.setMaximumSize(QtCore.QSize(30, 16777215))
        self.horizontalLayout_5.addWidget(self.label_5)

        self.changepeople = QtWidgets.QLineEdit()
        self.changepeople.setMaximumSize(QtCore.QSize(80, 16777215))
        self.changepeople.setPlaceholderText("带字母输入")
        self.horizontalLayout_5.addWidget(self.changepeople)

        self.label_6 = QtWidgets.QLabel("工时：")
        self.label_6.setMaximumSize(QtCore.QSize(30, 16777215))
        self.horizontalLayout_5.addWidget(self.label_6)

        self.changevalue = QtWidgets.QLineEdit()
        self.changevalue.setMaximumSize(QtCore.QSize(30, 16777215))
        self.horizontalLayout_5.addWidget(self.changevalue)

        self.label_7 = QtWidgets.QLabel("标准工时")
        self.label_7.setMaximumSize(QtCore.QSize(55, 16777215))
        self.horizontalLayout_5.addWidget(self.label_7)

        self.changeselect = QtWidgets.QComboBox()
        self.changeselect.setMaximumSize(QtCore.QSize(80, 16777215))
        self.horizontalLayout_5.addWidget(self.changeselect)

        self.changeupdate = QtWidgets.QPushButton("提交修正")
        self.changeupdate.setMaximumSize(QtCore.QSize(80, 16777215))
        self.horizontalLayout_5.addWidget(self.changeupdate)

        self.gridLayout.addLayout(self.horizontalLayout_5, 0, 1, 2, 1)

        # 表格区域
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.tableWidgetWorkload = QtWidgets.QTableWidget(parent=workloadinfos)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding,QtWidgets.QSizePolicy.Policy.Expanding)
        self.tableWidgetWorkload.setSizePolicy(sizePolicy)
        self.tableWidgetWorkload.setObjectName("tableWidgetWorkload")
        self.tableWidgetWorkload.setColumnCount(0)
        self.tableWidgetWorkload.setRowCount(0)
        self.tableWidgetWorkload.setSortingEnabled(True)  # 启用排序
        self.tableWidgetWorkload.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
        self.tableWidgetWorkload.setAlternatingRowColors(True)
        self.verticalLayout.addWidget(self.tableWidgetWorkload)

        self.gridLayout.addLayout(self.verticalLayout, 2, 1, 1, 1)

        self.retranslateUi(workloadinfos)
        QtCore.QMetaObject.connectSlotsByName(workloadinfos)

    def retranslateUi(self, workloadinfos):
        _translate = QtCore.QCoreApplication.translate
        workloadinfos.setWindowTitle(_translate("workloadinfos", "排班辅助"))
        self.label.setText(_translate("workloadinfos", "人员列表："))
        self.refresh.setText(_translate("workloadinfos", "刷新"))
        self.clear.setText(_translate("workloadinfos", "清空"))
        self.label_4.setText(_translate("workloadinfos", "修正内容"))
        self.label_5.setText(_translate("workloadinfos", "人员："))
        self.label_6.setText(_translate("workloadinfos", "工时："))
        self.label_7.setText(_translate("workloadinfos", "工时库"))
        self.changeupdate.setText(_translate("workloadinfos", "提交修正"))
