import sys

from PyQt5.QtCore import QSize, QTimer
from PyQt5.QtGui import QFont, QBrush, QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem
from ui.ui import Ui_MainWindow
import db_operation


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;")
        self.image_label.setPixmap(QPixmap('log.jpg'))
        self.image_label.setScaledContents(True)
        self.image_label.setMaximumHeight(140)
        self.image_label.setMaximumWidth(600)

        self.timer = QTimer()
        self.timer.timeout.connect(self.Switch)
        self.timer.start(200)

        self.pick_command_dict = {1: [], 2: [], 3: []}

    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(80, 80, 80))
            show_str = dict["car_number"] + "   排队中，请稍等片刻"
        elif (dict["statu"] == 1):  # 工作
            item.setForeground(QColor('blue'))
            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 Switch(self):

        pick_command_dict = {1:[],2:[],3:[]}
        for key in pick_command_dict:
            pick_command_dict[key] = db_query.query_command_in_unit_and_type(key,2)

        for key in self.pick_command_dict.keys():
            if self.pick_command_dict[key] != pick_command_dict[key]:
                self.pick_command_dict[key] = pick_command_dict[key]
                if key == 1:
                    self.A_listWidget.clear()
                    for dict in self.pick_command_dict[key]:
                        self.A_listWidget.addItem(self.getListWidgetItem(dict))
                elif key == 2:
                    self.B_listWidget.clear()
                    for dict in self.pick_command_dict[key]:
                        self.B_listWidget.addItem(self.getListWidgetItem(dict))
                elif key == 3:
                    self.C_listWidget.clear()
                    for dict in self.pick_command_dict[key]:
                        self.C_listWidget.addItem(self.getListWidgetItem(dict))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    db_query = db_operation.DBOperation()
    db_query.start()
    window.showMaximized()
    sys.exit(app.exec_())
