# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'window.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPalette,QPixmap,QColor
import sys
from get_stations import *
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from query_request import *
from PyQt5.QtGui import *
import time


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(960, 786)
        MainWindow.setMinimumSize(QtCore.QSize(960, 786))
        MainWindow.setMaximumSize(QtCore.QSize(960, 786))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label_title_img = QtWidgets.QLabel(self.centralwidget)
        self.label_title_img.setGeometry(QtCore.QRect(0, 0, 960, 141))
        self.label_title_img.setObjectName("label_title_img")
        title_img = QPixmap('img/bg1.png')
        self.label_title_img.setPixmap(title_img)
        self.widget_query = QtWidgets.QWidget(self.centralwidget)
        self.widget_query.setGeometry(QtCore.QRect(0, 141, 960, 80))
        self.widget_query.setObjectName("widget_query")
        self.widget_query.setAutoFillBackground(True)
        palette = QPalette()
        palette.setBrush(QPalette.Background,QtGui.QBrush(QtGui.QPixmap('img/bg2.png')))
        self.widget_query.setPalette(palette)
        self.label = QtWidgets.QLabel(self.widget_query)
        self.label.setGeometry(QtCore.QRect(30, 30, 54, 15))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.widget_query)
        self.label_2.setGeometry(QtCore.QRect(270, 30, 54, 15))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.widget_query)
        self.label_3.setGeometry(QtCore.QRect(500, 30, 54, 15))
        self.label_3.setObjectName("label_3")
        self.pushButton = QtWidgets.QPushButton(self.widget_query)
        self.pushButton.setGeometry(QtCore.QRect(750, 25, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.widget_query)
        self.textEdit.setGeometry(QtCore.QRect(90, 20, 104, 31))
        self.textEdit.setObjectName("textEdit")
        self.textEdit_2 = QtWidgets.QTextEdit(self.widget_query)
        self.textEdit_2.setGeometry(QtCore.QRect(330, 20, 104, 31))
        self.textEdit_2.setObjectName("textEdit_2")
        self.textEdit_3 = QtWidgets.QTextEdit(self.widget_query)
        self.textEdit_3.setGeometry(QtCore.QRect(560, 20, 104, 31))
        self.textEdit_3.setObjectName("textEdit_3")
        self.widget_checkBox = QtWidgets.QWidget(self.centralwidget)
        self.widget_checkBox.setGeometry(QtCore.QRect(0, 221, 960, 35))
        self.widget_checkBox.setObjectName("widget_checkBox")
        self.widget_checkBox.setAutoFillBackground(True)
        palette = QPalette()
        palette.setBrush(QPalette.Background,QtGui.QBrush(QtGui.QPixmap('img/bg3.png')))
        self.widget_checkBox.setPalette(palette)
        self.label_4 = QtWidgets.QLabel(self.widget_checkBox)
        self.label_4.setGeometry(QtCore.QRect(20, 9, 72, 15))
        self.label_4.setObjectName("label_4")
        self.checkBox = QtWidgets.QCheckBox(self.widget_checkBox)
        self.checkBox.setGeometry(QtCore.QRect(100, 9, 91, 19))
        self.checkBox.setObjectName("checkBox")
        self.checkBox_2 = QtWidgets.QCheckBox(self.widget_checkBox)
        self.checkBox_2.setGeometry(QtCore.QRect(290, 9, 70, 19))
        self.checkBox_2.setObjectName("checkBox_2")
        self.checkBox_3 = QtWidgets.QCheckBox(self.widget_checkBox)
        self.checkBox_3.setGeometry(QtCore.QRect(480, 9, 70, 19))
        self.checkBox_3.setObjectName("checkBox_3")
        self.checkBox_4 = QtWidgets.QCheckBox(self.widget_checkBox)
        self.checkBox_4.setGeometry(QtCore.QRect(640, 9, 70, 19))
        self.checkBox_4.setObjectName("checkBox_4")
        self.checkBox_5 = QtWidgets.QCheckBox(self.widget_checkBox)
        self.checkBox_5.setGeometry(QtCore.QRect(790, 9, 70, 19))
        self.checkBox_5.setObjectName("checkBox_5")
        self.label_train_img = QtWidgets.QLabel(self.centralwidget)
        self.label_train_img.setGeometry(QtCore.QRect(0, 256, 960, 62))
        self.label_train_img.setObjectName("label_train_img")
        train_img = QPixmap('img/bg4.png')
        self.label_train_img.setPixmap(train_img)
        self.tableView = QtWidgets.QTableView(self.centralwidget)
        self.tableView.setGeometry(QtCore.QRect(0, 320, 960, 461))
        self.tableView.setObjectName("tableView")
        self.model = QStandardItemModel();
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.tableView.horizontalHeader().setVisible(False)
        self.tableView.verticalHeader().setVisible(False)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.tableView.setFont(font)
        self.tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "车票查询"))
        self.label.setText(_translate("MainWindow", "出发地："))
        self.label_2.setText(_translate("MainWindow", "目的地："))
        self.label_3.setText(_translate("MainWindow", "出发日："))
        self.pushButton.setText(_translate("MainWindow", "查询"))
        self.label_4.setText(_translate("MainWindow", "车次类型："))
        self.checkBox.setText(_translate("MainWindow", "GC-高铁"))
        self.checkBox_2.setText(_translate("MainWindow", "D-动车"))
        self.checkBox_3.setText(_translate("MainWindow", "Z-直达"))
        self.checkBox_4.setText(_translate("MainWindow", "T-特快"))
        self.checkBox_5.setText(_translate("MainWindow", "K-快速"))

        self.textEdit_3.setText(get_time())
        self.pushButton.clicked.connect(self.on_click)
        self.checkBox.stateChanged.connect(self.change_G)
        self.checkBox_2.stateChanged.connect(self.change_D)
        self.checkBox_3.stateChanged.connect(self.change_Z)
        self.checkBox_4.stateChanged.connect(self.change_T)
        self.checkBox_5.stateChanged.connect(self.change_K)

def get_time():
    now = int(time.time())
    timeStruct = time.localtime(now)
    strTime = time.strftime("%Y-%m-%d",timeStruct)
    return strTime

def is_valid_date(str):
    try:
        time.strptime(str,"%Y-%m-%d")
        return True
    except:
        return False

# 将所有车次分类复选框取消勾选
def checkBox_default(self):
    self.checkBox.setChecked(False)
    self.checkBox_2.setChecked(False)
    self.checkBox_3.setChecked(False)
    self.checkBox_4.setChecked(False)
    self.checkBox_5.setChecked(False)

def change_G(self,state):
    if state == QtCore.Qt.Checked:
        g_vehicle()
        self.displayTable(len(type_data),16,type_data)
    else:
        r_g_vehicle()
        self.displayTable(len(type_data),16,type_data)

 # 动车复选框事件处理
def change_D(self, state):
        # 选中将动车信息添加到最后要显示的数据当中
    if state == QtCore.Qt.Checked:
            # 获取动车信息
        d_vehicle()
            # 通过表格显示该车型数据
        self.displayTable(len(type_data), 16, type_data)

    else:
            # 取消选中状态将移除该数据
        r_d_vehicle()
        self.displayTable(len(type_data), 16, type_data)

    # 直达复选框事件处理
def change_Z(self, state):
        # 选中将直达车信息添加到最后要显示的数据当中
    if state == QtCore.Qt.Checked:
            # 获取直达车信息
        z_vehicle()
        self.displayTable(len(type_data), 16, type_data)
    else:
            # 取消选中状态将移除该数据
        r_z_vehicle()
        self.displayTable(len(type_data), 16, type_data)

    # 特快复选框事件处理
def change_T(self, state):
        # 选中将特快车信息添加到最后要显示的数据当中
    if state == QtCore.Qt.Checked:
            # 获取特快车信息
        t_vehicle()
        self.displayTable(len(type_data), 16, type_data)
    else:
            # 取消选中状态将移除该数据
        r_t_vehicle()
        self.displayTable(len(type_data), 16, type_data)

    # 快速复选框事件处理
def change_K(self, state):
        # 选中将快车信息添加到最后要显示的数据当中
    if state == QtCore.Qt.Checked:
            # 获取快速车信息
        k_vehicle()
        self.displayTable(len(type_data), 16, type_data)

    else:
            # 取消选中状态将移除该数据
        r_k_vehicle()
        self.displayTable(len(type_data), 16, type_data)

    # 显示消息提示框，参数title为提示框标题文字，message为提示信息
def messageDialog(self, title, message):
    msg_box = QMessageBox(QMessageBox.Warning, title, message)
    msg_box.exec_()

    # 显示车次信息的表格
    # train参数为共有多少趟列车，该参数作为表格的行。
    # info参数为每趟列车的具体信息，例如有座、无座卧铺等。该参数作为表格的列
def displayTable(self, train, info, data):
    self.model.clear()
    for row in range(train):
        for column in range(info):
                # 添加表格内容
            item = QStandardItem(data[row][column])
                # 向表格存储模式中添加表格具体信息
            self.model.setItem(row, column, item)
        # 设置表格存储数据的模式
    self.tableView.setModel(self.model)

def on_click(self):
    get_from = self.textEdit.toPlainText()
    get_to = self.textEdit_2.toPlainText()
    get_date = self.textEdit_3.toPlainText()
    if isStations() == True:
        stations = eval(read())
        if get_from != "" and get_to != "" and get_date != "":
            if get_from in stations and get_to in stations and is_valid_date(get_date):
                inputYearDay = time.strptime(get_date,"%Y-%m-%d").tm_yday
                yearToday = time.localtime(time.time()).tm_yday
                timeDifference = inputYearDay - yearToday
                if timeDifference >= 0 and timeDifference <= 28:
                    from_station = stations[get_from]
                    to_station = stations[get_to]
                    data = query(get_date,from_station,to_station)
                    self.checkBox_default()
                    if len(data) != 0:
                        self.displayTable(len(data),16,data)
                    else:
                        self.messageDialog('警告','没有返回的网络数据！')
                else:
                    self.messageDialog('警告','超出查询日期的范围内，不可查询昨天的车票信息，以及29天以后的车票信息！')
            else:
                self.messageDialog('警告','输入的站名不存在，或日期格式不正确！')
        else:
            self.messageDialog('警告','请填写车站名称！')
    else:
        self.messageDialog('警告','未下载车站查询文件！')

def show_MainWindow():
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    if isStations() == False:
        getStation()
        show_MainWindow()
    else:
        show_MainWindow()
