﻿from PySide6.QtWidgets import QDialog, QFormLayout, QLineEdit, QPushButton, QHBoxLayout

from pypos.core.base_view import BaseView
from pypos.modules.auth.controllers.change_password_controller import ChangePasswordController


class ChangePasswordDialog(BaseView, QDialog):
    def __init__(self, user_info, parent=None):
        super().__init__(parent)
        self.user_info = user_info or {}
        self.controller = ChangePasswordController(self, self.user_info)
        self.setWindowTitle("Ubah Password")

        layout = QFormLayout(self)

        self.old_password = QLineEdit()
        self.old_password.setEchoMode(QLineEdit.Password)
        self.new_password = QLineEdit()
        self.new_password.setEchoMode(QLineEdit.Password)
        self.confirm_password = QLineEdit()
        self.confirm_password.setEchoMode(QLineEdit.Password)

        layout.addRow("Password Lama:", self.old_password)
        layout.addRow("Password Baru:", self.new_password)
        layout.addRow("Konfirmasi Password:", self.confirm_password)

        btn_layout = QHBoxLayout()
        self.btn_save = QPushButton("Simpan")
        self.btn_cancel = QPushButton("Batal")
        self.btn_save.clicked.connect(self._handle_save)
        self.btn_cancel.clicked.connect(self.reject)
        btn_layout.addWidget(self.btn_save)
        btn_layout.addWidget(self.btn_cancel)
        layout.addRow(btn_layout)

    def _handle_save(self):
        ok = self.controller.process_change_password(
            old_password=self.old_password.text(),
            new_password=self.new_password.text(),
            confirm_password=self.confirm_password.text(),
        )
        if ok:
            self.accept()
