#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
简化的维修系统登录界面
解决显示和输入问题
"""

from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QFormLayout, QGroupBox,
    QLineEdit, QPushButton, QLabel, QMessageBox
)
from PyQt5.QtCore import Qt, pyqtSignal


class SimpleMaintenanceLoginDialog(QDialog):
    """简化的维修系统登录对话框"""
    
    login_success = pyqtSignal()  # 登录成功信号
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUI()
    
    def setupUI(self):
        """设置用户界面"""
        self.setWindowTitle("维修系统 - 登录")
        self.resize(400, 250)
        self.setModal(True)
        
        # 设置简洁的样式
        self.setStyleSheet("""
            QDialog {
                background-color: #ffffff;
                font-family: "Microsoft YaHei", Arial, sans-serif;
            }
            QGroupBox {
                font-size: 14px;
                font-weight: bold;
                color: #2c3e50;
                border: 2px solid #bdc3c7;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px;
            }
            QLabel {
                color: #2c3e50;
                font-size: 14px;
            }
            QLineEdit {
                background-color: #ffffff;
                border: 2px solid #bdc3c7;
                border-radius: 3px;
                padding: 6px 8px;
                font-size: 14px;
                color: #2c3e50;
                min-height: 16px;
            }
            QLineEdit:focus {
                border-color: #3498db;
                background-color: #ffffff;
            }
            QPushButton {
                background-color: #3498db;
                color: white;
                border: none;
                padding: 8px 20px;
                border-radius: 3px;
                font-size: 14px;
                font-weight: bold;
                min-height: 16px;
            }
            QPushButton:hover {
                background-color: #2980b9;
            }
            QPushButton:pressed {
                background-color: #21618c;
            }
            QPushButton#cancel_btn {
                background-color: #95a5a6;
            }
            QPushButton#cancel_btn:hover {
                background-color: #7f8c8d;
            }
        """)
        
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setSpacing(20)
        main_layout.setContentsMargins(30, 30, 30, 30)
        
        # 标题
        title_label = QLabel("🔧 维修系统")
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("""
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
            margin: 10px 0px;
        """)
        main_layout.addWidget(title_label)
        
        # 登录表单组
        form_group = QGroupBox("身份验证")
        form_layout = QFormLayout(form_group)
        form_layout.setSpacing(15)
        form_layout.setContentsMargins(20, 20, 20, 20)
        
        # 用户名输入框
        self.username_edit = QLineEdit()
        self.username_edit.setPlaceholderText("请输入用户名")
        form_layout.addRow("用户名:", self.username_edit)
        
        # 密码输入框
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        self.password_edit.setPlaceholderText("请输入密码")
        form_layout.addRow("密码:", self.password_edit)
        
        main_layout.addWidget(form_group)
        
        # 提示信息
        hint_label = QLabel("💡 默认账号密码均为: admin")
        hint_label.setAlignment(Qt.AlignCenter)
        hint_label.setStyleSheet("""
            color: #7f8c8d;
            font-size: 12px;
            margin: 5px 0px;
        """)
        main_layout.addWidget(hint_label)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        button_layout.setSpacing(10)
        
        self.cancel_btn = QPushButton("取消")
        self.cancel_btn.setObjectName("cancel_btn")
        
        self.login_btn = QPushButton("登录")
        
        button_layout.addStretch()
        button_layout.addWidget(self.cancel_btn)
        button_layout.addWidget(self.login_btn)
        
        main_layout.addLayout(button_layout)
        
        # 连接信号
        self.login_btn.clicked.connect(self.handle_login)
        self.cancel_btn.clicked.connect(self.reject)
        self.password_edit.returnPressed.connect(self.handle_login)
        
        # 设置焦点和Tab顺序
        self.username_edit.setFocus()
        self.setTabOrder(self.username_edit, self.password_edit)
        self.setTabOrder(self.password_edit, self.login_btn)
        self.setTabOrder(self.login_btn, self.cancel_btn)
        
        print("✅ 简化登录界面创建完成")
        print(f"📝 窗口大小: {self.size().width()}x{self.size().height()}")
    
    def handle_login(self):
        """处理登录"""
        username = self.username_edit.text().strip()
        password = self.password_edit.text().strip()
        
        print(f"🔍 登录尝试: 用户名='{username}', 密码='{password}'")
        
        # 验证账号密码
        if username == "admin" and password == "admin":
            print("✅ 登录验证成功")
            self.login_success.emit()
            self.accept()
        else:
            print("❌ 登录验证失败")
            QMessageBox.warning(
                self, 
                "登录失败", 
                "用户名或密码错误！\n\n请使用:\n用户名: admin\n密码: admin"
            )
            self.password_edit.clear()
            self.password_edit.setFocus()


def test_simple_login():
    """测试简化登录界面"""
    import sys
    from PyQt5.QtWidgets import QApplication
    
    print("🧪 测试简化登录界面")
    print("=" * 30)
    
    app = QApplication(sys.argv)
    
    dialog = SimpleMaintenanceLoginDialog()
    
    print("💡 请测试以下功能:")
    print("1. 检查是否显示完整的登录界面")
    print("2. 在用户名框输入 'admin'")
    print("3. 在密码框输入 'admin'")
    print("4. 点击登录按钮")
    
    def on_success():
        print("🎉 登录成功！")
        QMessageBox.information(None, "成功", "登录测试成功！")
    
    dialog.login_success.connect(on_success)
    
    result = dialog.exec_()
    print(f"📊 测试结果: {'成功' if result == QDialog.Accepted else '取消'}")
    
    return result == QDialog.Accepted


if __name__ == "__main__":
    test_simple_login() 