#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :bms_login.py
@时间    :2022/01/03 09:35:16
@作者    :None
@版本    :1.0
@说明    :登录逻辑
'''


from ui.login import UiBmsLoginPage
from ui.own.frame_theme import MyFrame
from utils.qt import QMessageBox, Qt, Signal, Slot
from utils.globalvar import SD


class Win_Login(UiBmsLoginPage, MyFrame):
    # 信号传递
    login_signal = Signal()
    cancel_signal = Signal()
    home_show_signal = Signal()
    language_signal = Signal(int)

    def __init__(self, parent=None):
        super(Win_Login, self).__init__(parent)
        self.setupUi(self)

    @Slot()
    def on_btn_login_clicked(self):
        self.login_signal.emit()

    @Slot()
    def on_edt_password_returnPressed(self):
        self.login_signal.emit()

    @Slot()
    def on_btn_cancel_clicked(self):
        self.cancel_signal.emit()

    @Slot(int)
    def on_language_currentIndexChanged(self, index):
        self.language_signal.emit(index)

    def on_success(self):
        SD.CCU_TYPE = self.cb_prj.currentIndex()
        self.can_type = self.cb_con_type.currentIndex()
        self._can_index = self.cb_index.currentText()
        self._can_channel = self.cb_channel.currentText()
        self._can_baudrate = self.cb_baudrate.currentText()

        SD.CAN_TYPE = self.can_type
        can_index = int(self._can_index)
        can_channel = int(self._can_channel)
        can_baudrate = self._can_baudrate

        SD.CAN_CONTROL.set_can_board(SD.CAN_TYPE, can_index, can_channel, can_baudrate)

        self.home_show_signal.emit()
        self.close()

    def on_error(self):
        QMessageBox.critical(self, "登录失败" if SD.SYSTEM_LANGUAGE == 0 else "Login Error", "帐号密码错误" if SD.SYSTEM_LANGUAGE == 0 else "UserName or Pwd Error！")
