<?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 - Informacje klienta (taryfa, urządzenia, FAQ)
 */

$client_id = $_SESSION['client_id'];
$clients = vbk_read_klienci();
$client = $clients[$client_id] ?? null;
$info = vbk_read_info($client_id);
$infoadmin = vbk_read_infoadmin($client_id);
$devices = vbk_read_mac($client_id);
$faq = read_admin_text('pomoc_faq.txt');

if (!$client) {
    echo '<div class="alert alert-danger">' . __('common.error') . '</div>';
    return;
}
?>

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

    <?= ibok_help_section('info', __('help.show_help'), __('help.info_section_content')) ?>

    <div class="row g-4">
        <!-- Taryfa -->
        <div class="col-lg-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="bi bi-speedometer me-1"></i> <?= __('info.tariff') ?><?= ibok_tooltip(__('help.tooltip_tariff_info')) ?>
                </div>
                <div class="card-body p-0">
                    <?php if (ibok_feature('hide_speed')): ?>
                    <p class="text-muted text-center py-3 mb-0"><?= __('info.speed_hidden') ?></p>
                    <?php else: ?>
                    <?php
                    // Fallback: prędkości z urządzeń gdy brak w karcie usera
                    $info_dl = $client['dl_dzien'];
                    $info_ul = $client['ul_dzien'];
                    $info_dl_noc = $client['dl_noc'];
                    $info_ul_noc = $client['ul_noc'];
                    if ((empty($info_dl) || $info_dl === '0') && ibok_feature('speed_from_devices')) {
                        $dev_speed = get_max_device_speed($devices);
                        if (!empty($dev_speed['dl_dzien'])) {
                            $info_dl = $dev_speed['dl_dzien'];
                            $info_ul = $dev_speed['ul_dzien'];
                            $info_dl_noc = $dev_speed['dl_noc'];
                            $info_ul_noc = $dev_speed['ul_noc'];
                        }
                    }
                    ?>
                    <table class="table table-sm tariff-table mb-0">
                        <tr>
                            <td><i class="bi bi-arrow-down text-success me-1"></i> <?= __('info.download_day') ?></td>
                            <td><?= e($info_dl) ?></td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-arrow-up text-primary me-1"></i> <?= __('info.upload_day') ?></td>
                            <td><?= e($info_ul) ?></td>
                        </tr>
                        <?php if (ibok_has_night_speed($info_dl, $info_ul, $info_dl_noc, $info_ul_noc)): ?>
                        <tr>
                            <td><i class="bi bi-arrow-down text-success me-1"></i> <?= __('info.download_night') ?></td>
                            <td><?= e($info_dl_noc) ?></td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-arrow-up text-primary me-1"></i> <?= __('info.upload_night') ?></td>
                            <td><?= e($info_ul_noc) ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Dane kontaktowe klienta -->
            <?php
            $has_contact = !empty($infoadmin['tel1']) || !empty($infoadmin['tel2']) || !empty($infoadmin['email']) || !empty($infoadmin['nip']);
            if ($has_contact):
            ?>
            <div class="card mb-4">
                <div class="card-header">
                    <i class="bi bi-person-vcard me-1"></i> <?= __('info.your_data') ?><?= ibok_tooltip(__('help.tooltip_client_data')) ?>
                </div>
                <div class="card-body p-0">
                    <table class="table table-sm mb-0">
                        <?php if (!empty($infoadmin['adres_pelny'])): ?>
                        <tr><td class="fw-bold"><i class="bi bi-geo-alt me-1"></i> <?= __('contact.address') ?></td><td><?= e($infoadmin['adres_pelny']) ?></td></tr>
                        <?php endif; ?>
                        <?php if (!empty($infoadmin['adres_instalacji'])): ?>
                        <tr><td class="fw-bold"><i class="bi bi-house me-1"></i> <?= __('contact.installation_address') ?></td><td><?= e($infoadmin['adres_instalacji']) ?></td></tr>
                        <?php endif; ?>
                        <?php if (!empty($infoadmin['tel1'])): ?>
                        <tr><td class="fw-bold"><i class="bi bi-telephone me-1"></i> <?= __('contact.phone') ?></td><td><?= e($infoadmin['tel1']) ?></td></tr>
                        <?php endif; ?>
                        <?php if (!empty($infoadmin['tel2'])): ?>
                        <tr><td class="fw-bold"><i class="bi bi-telephone me-1"></i> <?= __('contact.phone') ?> 2</td><td><?= e($infoadmin['tel2']) ?></td></tr>
                        <?php endif; ?>
                        <?php if (!empty($infoadmin['email'])): ?>
                        <tr><td class="fw-bold"><i class="bi bi-envelope me-1"></i> <?= __('contact.email') ?></td><td><?= e($infoadmin['email']) ?></td></tr>
                        <?php endif; ?>
                        <?php if (!empty($infoadmin['nip'])): ?>
                        <tr><td class="fw-bold">NIP</td><td><?= e($infoadmin['nip']) ?></td></tr>
                        <?php endif; ?>
                    </table>
                </div>
                <div class="card-footer d-flex justify-content-between align-items-center">
                    <small class="text-muted"><?= __('info.data_check_note') ?></small>
                    <a href="index.php?page=kontakt&subject=data_update" class="btn btn-sm btn-outline-warning">
                        <i class="bi bi-exclamation-triangle me-1"></i> <?= __('info.report_outdated') ?>
                    </a>
                </div>
            </div>
            <?php endif; ?>

            <!-- Notatki -->
            <?php if (!empty($info['notatka1']) || !empty($info['notatka2'])): ?>
            <div class="card mb-4">
                <div class="card-header">
                    <i class="bi bi-sticky me-1"></i> <?= __('info.notes') ?>
                </div>
                <div class="card-body">
                    <?php if (!empty($info['notatka1'])): ?>
                        <p><?= e($info['notatka1']) ?></p>
                    <?php endif; ?>
                    <?php if (!empty($info['notatka2'])): ?>
                        <p><?= e($info['notatka2']) ?></p>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Urządzenia -->
        <div class="col-lg-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="bi bi-router me-1"></i> <?= __('info.devices') ?>
                    <span class="badge bg-secondary ms-1"><?= count($devices) ?></span>
                </div>
                <div class="card-body">
                    <?php if (empty($devices)): ?>
                        <p class="text-muted text-center"><?= __('info.no_devices') ?></p>
                    <?php else: ?>
                        <?php foreach ($devices as $dev): ?>
                        <div class="border rounded p-3 mb-2">
                            <div class="d-flex justify-content-between align-items-start">
                                <div>
                                    <strong><?= e($dev['rodzaj']) ?></strong>
                                    <?php if (!empty($dev['typ'])): ?>
                                        <span class="text-muted ms-1">(<?= e($dev['typ']) ?>)</span>
                                    <?php endif; ?>
                                </div>
                                <?php if (!empty($dev['wylaczony']) && $dev['wylaczony'] !== '0'): ?>
                                    <span class="badge bg-secondary">OFF</span>
                                <?php endif; ?>
                            </div>
                            <div class="small mt-1">
                                <?php if (!empty($dev['ip']) && !ibok_feature('hide_device_ip')): ?>
                                    <span class="me-3"><i class="bi bi-hdd-network me-1"></i> <?= e($dev['ip']) ?></span>
                                <?php endif; ?>
                                <?php if (!empty($dev['mac'])): ?>
                                    <span class="me-3"><i class="bi bi-ethernet me-1"></i> <?= e($dev['mac']) ?></span>
                                <?php endif; ?>
                            </div>
                            <?php if (!empty($dev['punkt_podl'])): ?>
                                <div class="small text-muted mt-1">
                                    <i class="bi bi-geo-alt me-1"></i> <?= e($dev['punkt_podl']) ?>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($dev['taryfa'])): ?>
                                <div class="small mt-1">
                                    <i class="bi bi-speedometer2 me-1 text-info"></i> <strong><?= e($dev['taryfa']) ?></strong>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($dev['data_podl']) && $dev['data_podl'] !== '0000-00-00'): ?>
                                <div class="small text-muted mt-1">
                                    <i class="bi bi-calendar me-1"></i> <?= __('info.device_date') ?>: <?= format_date($dev['data_podl']) ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Historia logowań -->
    <?php
    $login_history_file = IBOK_LOGS_DIR . '/login_history_' . preg_replace('/[^a-zA-Z0-9_-]/', '', $client_id) . '.json';
    $login_history = [];
    if (!ibok_is_demo() && file_exists($login_history_file)) {
        $login_history = @json_decode(file_get_contents($login_history_file), true) ?: [];
    }
    if (!empty($login_history)):
    ?>
    <div class="card mb-4">
        <div class="card-header">
            <i class="bi bi-clock-history me-1"></i> <?= __('info.login_history') ?>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
            <table class="table table-sm table-striped mb-0">
                <thead class="table-light">
                    <tr>
                        <th><?= __('common.date') ?></th>
                        <th>IP</th>
                        <th><?= __('info.browser') ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($login_history as $entry): ?>
                    <tr>
                        <td><?= e($entry['time'] ?? '') ?></td>
                        <td><code><?= e($entry['ip'] ?? '') ?></code></td>
                        <td class="small"><?= e(mb_substr($entry['user_agent'] ?? '', 0, 80)) ?><?= mb_strlen($entry['user_agent'] ?? '') > 80 ? '...' : '' ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- FAQ -->
    <?php if (!empty($faq)): ?>
    <div class="card">
        <div class="card-header">
            <i class="bi bi-question-circle me-1"></i> <?= __('info.faq') ?>
        </div>
        <div class="card-body">
            <?= $faq ?>
        </div>
    </div>
    <?php endif; ?>
</div>
