
from PyQt5.QtCore import QSize, QTimer, Qt
from PyQt5.QtGui import QFont, QBrush, QColor, QPixmap, QCursor
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem, QListWidget, QLabel, QMenu

from ui.ui import Ui_MainWindow



class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, db, display_number,pagination,task_type):
        super().__init__()
        self.setupUi(self)
        # 设置标题
        self.label.setText("碧海花园智能立体停车库流程信息")
        # 显示的任务类型
        self.task_type = task_type
        # 图片以及尺寸
        self.image_label.setPixmap(QPixmap('./image/log.jpg'))
        self.image_label.setScaledContents(True)
        self.image_label.setMaximumHeight(140)
        self.image_label.setMaximumWidth(600)
        # 数据库操作句柄
        self.db = db
        # 显示框数量，一般来说有几个单元显示几个
        self.display_number = display_number
        # 页数
        self.pagination = pagination
        self.list_widget_dict = {}
        self.label_dict = {}
        self.command_dict = {}
        # 创建listwidget与label 配置多少个单元生成多少对
        for i in range(0,self.display_number):
            A_ascii = ord('A')
            # 设置样式
            self.list_widget_dict[i] = QListWidget(self)
            self.list_widget_dict[i].setStyleSheet("border:5px solid #014F84;")
            self.list_widget_dict[i].setObjectName(('list_widget_%d'%(i+1)))

            self.process_horizontalLayout.addWidget(self.list_widget_dict[i])

            # 设置单元文本
            self.label_dict[i] = QLabel(self)
            self.label_dict[i].setText(str(chr(A_ascii+i))+"单元")
            self.label_dict[i].setAlignment(Qt.AlignCenter)
            self.label_dict[i].setFont(QFont("华文楷体", 60, QFont.Bold))

            self.command_dict[i] = []

            self.unit_horizontalLayout.addWidget(self.label_dict[i])


        self.timer = QTimer()
        self.timer.timeout.connect(self.Switch)
        self.timer.start(200)
    def closeEvent(self, event):
        event.accept()  # 接受关闭事件


    def get_listWidget_item(self, dict):
        item = QListWidgetItem()
        item.setFont(QFont('微软雅黑', 20, QFont.Bold))
        show_str = ""
        if dict['type'] == 1:#存车指令
            item.setBackground(QColor(185,240,240))
            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'] + "       存车已完成"
            elif (dict['statu'] == 3):  # 已完成
                item.setForeground(QColor('red'))
                show_str = dict['car_number'] + "      任务故障，请联系管理员！"
            show_str = "存       "+show_str

        elif dict['type'] == 2:#取车指令
            item.setBackground(QColor(250,240,200))
            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"])
            elif (dict['statu'] == 3):  # 已完成
                item.setForeground(QColor('red'))
                show_str = dict['car_number'] + "      任务故障，请联系管理员！"
            show_str = "取       "+show_str
        item.setText(show_str)
        return item

    def Switch(self):
        # 查询所有指令表
        for i in range(0,self.display_number):
            if self.task_type != 0:
                t_command_dict = self.db.query_command_all_in_unit_type_and_sort((i+1),self.task_type)
            else:
                t_command_dict = self.db.query_command_all_in_unit_and_sort((i+1))
            if self.command_dict[i] != t_command_dict:
                self.command_dict[i] = t_command_dict
                self.list_widget_dict[i].clear()
                for dict in self.command_dict[i]:
                    self.list_widget_dict[i].addItem(self.get_listWidget_item(dict))
