facebook-tool/gui/tabs/accounts/forms/account_form.py

86 lines
3.0 KiB
Python

from PyQt5.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QComboBox,
QPushButton, QMessageBox
)
from database.models.account import Account
class AccountForm(QDialog):
def __init__(self, parent=None, account=None):
super().__init__(parent)
self.setWindowTitle("Account Form")
self.account = account
self.setMinimumSize(400, 200)
layout = QVBoxLayout()
# --- Email ---
layout.addWidget(QLabel("Email"))
self.email_input = QLineEdit()
self.email_input.setMinimumWidth(250)
layout.addWidget(self.email_input)
# --- Password + toggle button ---
layout.addWidget(QLabel("Password"))
pw_layout = QHBoxLayout()
self.password_input = QLineEdit()
self.password_input.setEchoMode(QLineEdit.Password)
self.password_input.setMinimumWidth(200)
self.toggle_btn = QPushButton("Show")
self.toggle_btn.setCheckable(True)
self.toggle_btn.setMaximumWidth(80)
self.toggle_btn.clicked.connect(self.toggle_password)
pw_layout.addWidget(self.password_input)
pw_layout.addWidget(self.toggle_btn)
layout.addLayout(pw_layout)
# --- Status ---
layout.addWidget(QLabel("Status"))
self.active_input = QComboBox()
self.active_input.addItems(["Inactive", "Active"])
layout.addWidget(self.active_input)
# --- Buttons ---
btn_layout = QHBoxLayout()
self.save_btn = QPushButton("Save")
self.save_btn.setMinimumWidth(80)
self.save_btn.clicked.connect(self.save)
btn_layout.addWidget(self.save_btn)
self.cancel_btn = QPushButton("Cancel")
self.cancel_btn.setMinimumWidth(80)
self.cancel_btn.clicked.connect(self.close)
btn_layout.addWidget(self.cancel_btn)
layout.addLayout(btn_layout)
self.setLayout(layout)
# --- Nếu edit account ---
if account:
self.email_input.setText(account.get("email", ""))
self.password_input.setText(account.get("password", ""))
self.active_input.setCurrentText("Active" if account.get("is_active", 1) == 1 else "Inactive")
def toggle_password(self):
"""Ẩn/hiện password khi nhấn nút"""
if self.toggle_btn.isChecked():
self.password_input.setEchoMode(QLineEdit.Normal)
self.toggle_btn.setText("Hide")
else:
self.password_input.setEchoMode(QLineEdit.Password)
self.toggle_btn.setText("Show")
def save(self):
email = self.email_input.text()
password = self.password_input.text()
is_active = 1 if self.active_input.currentText() == "Active" else 0
try:
if self.account and "id" in self.account:
Account.update(self.account["id"], email, password, is_active)
else:
Account.create(email, password, is_active)
self.accept()
except Exception as e:
QMessageBox.warning(self, "Error", str(e))