import datetime

from PyQt6.QtCore import Qt , QSortFilterProxyModel
from PyQt6.QtWidgets import QCompleter , QWidget , QMessageBox

from UI.addFlight import Ui_FormAddFlight
from .LogerinTxt import app_logger
from Functions.apiFunction import api_fc


class addFlightWidget(Ui_FormAddFlight, QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        try:
            self.setupUi(self)
            nowDay = datetime.datetime.now()
            self.dateEdit.setDateTime(nowDay)
            self.dateTimeEdit.setDateTime(nowDay)
            self.dateTimeEdit_2.setDateTime(nowDay)
            self.pushButton.clicked.connect(self.btn)
            self.comboBox.addItems(['短停', '航前', '特后前', '航后'])
            self.comboBox.setEditable(True)
            self.comboBox.currentIndexChanged.connect(self.changed)
            self.filterModel = QSortFilterProxyModel(self.comboBox)
            self.filterModel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
            self.filterModel.setSourceModel(self.comboBox.model())
            self.completer = QCompleter(self.filterModel, self.comboBox)
            self.completer.setCompletionMode(QCompleter.CompletionMode.UnfilteredPopupCompletion)
            self.completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
            self.comboBox.setCompleter(self.completer)
            self.comboBox.lineEdit().textEdited.connect(self.filterModel.setFilterFixedString)
            self.completer.activated.connect(self.on_completer_activated)
            self.ipinfo = None
            self.user = None
        except Exception as e:
            app_logger.log_error(e)

    def on_completer_activated(self, text):
        try:
            if text:
                print(text)
                index = self.comboBox.findText(text)
                self.comboBox.setCurrentIndex(index)
        except Exception as e:
            app_logger.log_error(e)

    def changed(self):
        if self.comboBox.currentText() == "航前":
            self.label_8.setEnabled(False)
            self.dateTimeEdit.setEnabled(False)
            self.label_9.setEnabled(True)
            self.dateTimeEdit_2.setEnabled(True)
        elif self.comboBox.currentText() == "航后":
            self.label_9.setEnabled(False)
            self.dateTimeEdit_2.setEnabled(False)
            self.label_8.setEnabled(True)
            self.dateTimeEdit.setEnabled(True)
        else:
            self.label_8.setEnabled(True)
            self.dateTimeEdit.setEnabled(True)
            self.label_9.setEnabled(True)
            self.dateTimeEdit_2.setEnabled(True)

    def btn(self):
        if self.lineEditPWD.text() != "" and self.lineEditUser.text() != "" and self.lineEditPeople.text() != "" and self.lineEdit.text() != "" and self.lineEdit_2.text() != "":
            # ip = "http://" + str(self.ipinfo) + "/static/" + "manAddFlight/" + "%s/%s/%s/%s/%s/%s/%s/%s/%s/%s" % (
            #     self.comboBox.currentText(), self.lineEditPWD.text(), self.lineEditUser.text(),
            #     self.lineEditPeople.text(),
            #     self.lineEdit.text(), self.dateEdit.text(), self.lineEdit_2.text(), self.dateTimeEdit.text(),
            #     self.dateTimeEdit_2.text(), self.user)
            try:
                # res = requests.get(url=ip, timeout=30).json()
                res = api_fc.request_post('/static/manAddFlight',
                                          flighttype=self.comboBox.currentText(),
                                          acno=self.lineEditPWD.text(),
                                          eng=self.lineEditUser.text(),
                                          actype=self.lineEditPeople.text(),
                                          flightno=self.lineEdit.text(),
                                          flightdate=self.dateEdit.text(),
                                          bay=self.lineEdit_2.text(),
                                          eta=self.dateTimeEdit.text(),
                                          etd=self.dateTimeEdit_2.text(),
                                          user=self.user)
                if res["返回值"] == "ok":
                    self.close()
                    QMessageBox.warning(self, "提示", "航班添加成功！！！")
                else:
                    self.close()
                    QMessageBox.warning(self, "警告", "航班添加失败")
            except Exception as e:
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
                app_logger.log_error(e)

        else:
            QMessageBox.warning(self, "警告", "请完善航班信息后再提交！")