import sys

from PyQt5.QtCore import QSize
from PyQt5.QtGui import QFont, QBrush, QColor
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem
from ui.ui import Ui_MainWindow
import db_query
# db参数
# db_ip = "192.168.1.233"
db_ip = "127.0.0.1"
db_port = 3306
db_name = "ct_project"
# db_user = "zx"
# db_password = "zx123456"
db_user = "root"
db_password = "123456"

class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.A_listWidget.setGridSize(QSize(335, 60))
        self.A_listWidget.setStyleSheet("border:5px solid #014F84;")
        self.B_listWidget.setGridSize(QSize(335, 60))
        self.B_listWidget.setStyleSheet("border:5px solid #014F84;")
        self.C_listWidget.setGridSize(QSize(335, 60))
        self.C_listWidget.setStyleSheet("border:5px solid #014F84;")

    def closeEvent(self, event):
        db_query.Close()
        event.accept()  # 接受关闭事件
    def getListWidgetItem(self,dict):
        item = QListWidgetItem()
        item.setFont(QFont('微软雅黑', 20, QFont.Bold))
        show_str = ""
        if (dict["statu"] == 0):  # 排队
            item.setForeground(QColor('red'))
            show_str = dict["car_number"] + "   排队中，请稍等片刻"
        elif (dict["statu"] == 1):  # 工作
            item.setForeground(QColor(255,215,0))
            show_str = dict["car_number"] + "   取车中，等待取车结束"
        elif (dict["statu"] == 2):  # 已完成
            item.setForeground(QColor('green'))
            show_str = dict["car_number"] + "   已完成，请到 %d 号出口取车" % (dict["export_id"])
        item.setText(show_str)
        return item
    def callback(self,command_queue,unit):
        command_queue.sort(reverse=True,key=lambda s: s["statu"])
        if unit == 1:
            self.A_listWidget.clear()
            for dict in command_queue:
                self.A_listWidget.addItem(self.getListWidgetItem(dict))
        elif unit == 2:
            self.B_listWidget.clear()
            for dict in command_queue:
                self.B_listWidget.addItem(self.getListWidgetItem(dict))
        elif unit == 3:
            self.C_listWidget.clear()
            for dict in command_queue:
                self.C_listWidget.addItem(self.getListWidgetItem(dict))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    db_query = db_query.DBQuery(db_ip, db_port, db_name, db_user, db_password)
    db_query.setCallback(window.callback)
    db_query.start()
    window.showMaximized()
    sys.exit(app.exec_())
