167 lines
5.8 KiB
Python
167 lines
5.8 KiB
Python
import os
|
|
from functools import partial
|
|
from PyQt5.QtWidgets import (
|
|
QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem,
|
|
QPushButton, QHBoxLayout, QDialog, QLabel, QLineEdit, QComboBox, QMessageBox,
|
|
QMenu, QAction, QSizePolicy
|
|
)
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QFont
|
|
from PyQt5.QtWidgets import QHeaderView
|
|
from database.models import Account
|
|
from .forms.account_form import AccountForm
|
|
from config import PROFILES_DIR
|
|
|
|
# 👇 import cửa sổ login FB
|
|
from gui.handle.login_fb import LoginFB # chỉnh path này theo project của bạn
|
|
|
|
PAGE_SIZE = 10
|
|
|
|
class AccountTab(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.current_page = 0
|
|
layout = QVBoxLayout()
|
|
|
|
# --- Top bar ---
|
|
top_layout = QHBoxLayout()
|
|
self.add_btn = QPushButton("Add Account")
|
|
self.add_btn.clicked.connect(self.add_account)
|
|
top_layout.addWidget(self.add_btn)
|
|
|
|
# 🆕 Action menu
|
|
self.options_btn = QPushButton("Action")
|
|
self.options_btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
|
self.options_btn.setMinimumWidth(80)
|
|
self.options_btn.setMaximumWidth(120)
|
|
top_layout.addWidget(self.options_btn)
|
|
layout.addLayout(top_layout)
|
|
|
|
# Table
|
|
self.table = QTableWidget()
|
|
self.table.verticalHeader().setDefaultSectionSize(28) # row gọn
|
|
self.table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
|
layout.addWidget(self.table)
|
|
|
|
# Pagination
|
|
pag_layout = QHBoxLayout()
|
|
self.prev_btn = QPushButton("Previous")
|
|
self.prev_btn.setMinimumWidth(100)
|
|
self.prev_btn.clicked.connect(self.prev_page)
|
|
pag_layout.addWidget(self.prev_btn)
|
|
|
|
self.next_btn = QPushButton("Next")
|
|
self.next_btn.setMinimumWidth(100)
|
|
self.next_btn.clicked.connect(self.next_page)
|
|
pag_layout.addWidget(self.next_btn)
|
|
layout.addLayout(pag_layout)
|
|
|
|
self.setLayout(layout)
|
|
self.update_options_menu()
|
|
self.load_data()
|
|
|
|
def load_data(self):
|
|
accounts = Account.all()
|
|
start = self.current_page * PAGE_SIZE
|
|
end = start + PAGE_SIZE
|
|
page_items = accounts[start:end]
|
|
|
|
self.table.setRowCount(len(page_items))
|
|
self.table.setColumnCount(6)
|
|
self.table.setHorizontalHeaderLabels([
|
|
"ID", "Email", "Status", "Profile Exists", "Login At", "Actions"
|
|
])
|
|
|
|
for i, acc in enumerate(page_items):
|
|
acc_dict = {k: acc[k] for k in acc.keys()}
|
|
|
|
self.table.setItem(i, 0, QTableWidgetItem(str(acc_dict["id"])))
|
|
self.table.setItem(i, 1, QTableWidgetItem(acc_dict["email"]))
|
|
self.table.setItem(i, 2, QTableWidgetItem("Active" if acc_dict["is_active"] == 1 else "Inactive"))
|
|
|
|
# ✅ Check profile folder
|
|
folder_name = acc_dict["email"]
|
|
profile_path = os.path.join(PROFILES_DIR, folder_name)
|
|
profile_status = "True" if os.path.isdir(profile_path) else "False"
|
|
self.table.setItem(i, 3, QTableWidgetItem(profile_status))
|
|
|
|
# 🆕 Login At
|
|
login_at_value = acc_dict.get("login_at") or "-"
|
|
self.table.setItem(i, 4, QTableWidgetItem(str(login_at_value)))
|
|
|
|
# Actions
|
|
btn_menu = QPushButton("Actions")
|
|
menu = QMenu()
|
|
|
|
# 🆕 Login action
|
|
action_login = QAction("Login", btn_menu)
|
|
action_login.triggered.connect(lambda _, a=acc_dict: self.open_login_window(a))
|
|
menu.addAction(action_login)
|
|
|
|
# Edit
|
|
action_edit = QAction("Edit", btn_menu)
|
|
action_edit.triggered.connect(lambda _, a=acc_dict: self.edit_account(a))
|
|
menu.addAction(action_edit)
|
|
|
|
# Delete
|
|
action_delete = QAction("Delete", btn_menu)
|
|
action_delete.triggered.connect(lambda _, a=acc_dict: self.delete_account(a))
|
|
menu.addAction(action_delete)
|
|
|
|
btn_menu.setMenu(menu)
|
|
self.table.setCellWidget(i, 5, btn_menu)
|
|
|
|
# Column sizing
|
|
header = self.table.horizontalHeader()
|
|
header.setSectionResizeMode(0, QHeaderView.ResizeToContents)
|
|
header.setSectionResizeMode(1, QHeaderView.Stretch)
|
|
header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
|
|
header.setSectionResizeMode(3, QHeaderView.ResizeToContents)
|
|
header.setSectionResizeMode(4, QHeaderView.ResizeToContents)
|
|
header.setSectionResizeMode(5, QHeaderView.Fixed)
|
|
self.table.setColumnWidth(5, 100)
|
|
|
|
self.prev_btn.setEnabled(self.current_page > 0)
|
|
self.next_btn.setEnabled(end < len(accounts))
|
|
|
|
# 🆕 Action menu
|
|
def update_options_menu(self):
|
|
menu = QMenu()
|
|
action_reload = QAction("Reload", menu)
|
|
action_reload.triggered.connect(lambda: self.load_data())
|
|
menu.addAction(action_reload)
|
|
self.options_btn.setMenu(menu)
|
|
|
|
def add_account(self):
|
|
form = AccountForm(self)
|
|
if form.exec_():
|
|
self.current_page = 0
|
|
self.load_data()
|
|
|
|
def edit_account(self, account):
|
|
form = AccountForm(self, account)
|
|
if form.exec_():
|
|
self.load_data()
|
|
|
|
def delete_account(self, account):
|
|
confirm = QMessageBox.question(
|
|
self, "Confirm", f"Delete account {account['email']}?",
|
|
QMessageBox.Yes | QMessageBox.No
|
|
)
|
|
if confirm == QMessageBox.Yes:
|
|
Account.delete(account["id"])
|
|
self.load_data()
|
|
|
|
def next_page(self):
|
|
self.current_page += 1
|
|
self.load_data()
|
|
|
|
def prev_page(self):
|
|
self.current_page -= 1
|
|
self.load_data()
|
|
|
|
# 🆕 Mở cửa sổ login FB
|
|
def open_login_window(self, account):
|
|
self.login_window = LoginFB(account) # truyền account vào nếu class LoginFB có nhận
|
|
self.login_window.show()
|