<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 - Zmiana hasła klienta
 */

$client_id = $_SESSION['client_id'];
$client_login = $_SESSION['client_login'] ?? '';

$min_length = (int)ibok_setting('password_min_length', 6);
if ($min_length < 1) $min_length = 6;

$msg = '';
$msg_type = 'success';

// Handle POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_validate()) {
    if (ibok_is_demo()) {
        $msg = __('demo.feature_blocked');
        $msg_type = 'warning';
    } elseif (!function_exists('db_is_mysql_mode') || !db_is_mysql_mode()) {
        $msg = __('zmiana_hasla.error_readonly');
        $msg_type = 'danger';
    } else {
        $current = $_POST['current_password'] ?? '';
        $new = $_POST['new_password'] ?? '';
        $confirm = $_POST['confirm_password'] ?? '';

        if (empty($current) || empty($new) || empty($confirm)) {
            $msg = __('zmiana_hasla.error_current');
            $msg_type = 'danger';
        } elseif (mb_strlen($new) < $min_length) {
            $msg = __('zmiana_hasla.error_min_length', ['min' => $min_length]);
            $msg_type = 'danger';
        } elseif ($new !== $confirm) {
            $msg = __('zmiana_hasla.error_mismatch');
            $msg_type = 'danger';
        } elseif ($new === $current) {
            $msg = __('zmiana_hasla.error_same');
            $msg_type = 'danger';
        } else {
            // Weryfikacja obecnego hasła
            $client = db_find_client_by_login($client_login);
            if (!$client || $current !== $client['haslo_encoded']) {
                $msg = __('zmiana_hasla.error_current');
                $msg_type = 'danger';
            } else {
                if (db_update_client_password($client_id, $new)) {
                    $msg = __('zmiana_hasla.success');
                    $msg_type = 'success';
                    ibok_log("Password changed for client ID: {$client_id} ({$client_login})", 'info');
                } else {
                    $msg = __('zmiana_hasla.error_save');
                    $msg_type = 'danger';
                }
            }
        }
    }
}
?>

<div class="fade-in">
    <h4 class="mb-4">
        <i class="bi bi-key me-2"></i> <?= __('zmiana_hasla.title') ?>
    </h4>

    <?= ibok_help_section('zmiana_hasla', __('help.show_help'), __('zmiana_hasla.help_content')) ?>

    <?php if ($msg): ?>
        <div class="alert alert-<?= $msg_type ?> alert-dismissible fade show">
            <i class="bi bi-<?= $msg_type === 'success' ? 'check-circle' : 'exclamation-triangle' ?> me-1"></i>
            <?= e($msg) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-lg-6">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-key me-1"></i> <?= __('zmiana_hasla.title') ?>
                </div>
                <div class="card-body">
                    <form method="post">
                        <?= csrf_field() ?>

                        <div class="mb-3">
                            <label for="current_password" class="form-label"><?= __('zmiana_hasla.current_password') ?></label>
                            <input type="password" class="form-control" id="current_password" name="current_password" required autocomplete="current-password">
                        </div>

                        <hr>

                        <div class="mb-3">
                            <label for="new_password" class="form-label"><?= __('zmiana_hasla.new_password') ?></label>
                            <input type="password" class="form-control" id="new_password" name="new_password" required minlength="<?= $min_length ?>" autocomplete="new-password">
                            <div class="form-text"><?= __('zmiana_hasla.error_min_length', ['min' => $min_length]) ?></div>
                        </div>

                        <div class="mb-4">
                            <label for="confirm_password" class="form-label"><?= __('zmiana_hasla.confirm_password') ?></label>
                            <input type="password" class="form-control" id="confirm_password" name="confirm_password" required minlength="<?= $min_length ?>" autocomplete="new-password">
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-1"></i> <?= __('zmiana_hasla.btn_change') ?>
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
