<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$tpinStatus = $tpinStatus ?? ['has_tpin' => false, 'required' => false, 'locked' => false, 'set_at' => null];
?>

<div class="row g-4">
    <div class="col-lg-4">
        <div class="stat-card text-center">
            <?php if (! empty($user->avatar_path)): ?>
                <img src="<?= base_url($user->avatar_path) ?>" class="rounded-circle mb-3" width="96" height="96" style="object-fit:cover;" alt="">
            <?php else: ?>
                <span class="avatar-circle mb-3" style="width:96px;height:96px;font-size:2rem;">
                    <?= esc(strtoupper(substr($user->first_name, 0, 1))) ?>
                </span>
            <?php endif; ?>

            <h1 class="h5 mb-0"><?= esc($user->getFullName()) ?></h1>
            <div class="text-muted small mb-2">@<?= esc($user->username) ?></div>

            <?php if ($isOnline): ?>
                <span class="badge text-bg-success mb-3"><i class="fa-solid fa-circle me-1" style="font-size:.5rem;"></i>Online</span>
            <?php else: ?>
                <span class="badge text-bg-secondary mb-3">Offline</span>
            <?php endif; ?>

            <form action="<?= base_url('users/' . $user->id . '/avatar') ?>" method="post" enctype="multipart/form-data" class="mt-2">
                <?= csrf_field() ?>
                <input type="file" name="avatar" accept="image/jpeg,image/png,image/webp" class="form-control form-control-sm mb-2">
                <button type="submit" class="btn btn-sm btn-outline-primary w-100">Upload Photo</button>
            </form>
        </div>
    </div>

    <div class="col-lg-8">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h2 class="h6 mb-0">Profile Details</h2>
                <?php if (empty($isOwnProfile)): ?>
                    <a href="<?= base_url('users/' . $user->id . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
                        <i class="fa-solid fa-pen me-1"></i>Edit
                    </a>
                <?php else: ?>
                    <div class="d-flex gap-2">
                        <a href="<?= base_url('profile/tpin') ?>" class="btn btn-sm btn-outline-primary">
                            <i class="fa-solid fa-shield-halved me-1"></i>Manage TPIN
                        </a>
                        <a href="<?= base_url('password/change') ?>" class="btn btn-sm btn-outline-secondary">
                            <i class="fa-solid fa-lock me-1"></i>Change Password
                        </a>
                    </div>
                <?php endif; ?>
            </div>

            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Email</dt>
                <dd class="col-sm-8"><?= esc($user->email) ?></dd>

                <dt class="col-sm-4 text-muted">Phone</dt>
                <dd class="col-sm-8"><?= esc($user->phone ?: '—') ?></dd>

                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8">
                    <?php $badge = ['active' => 'success', 'inactive' => 'secondary', 'locked' => 'danger'][$user->status] ?? 'secondary'; ?>
                    <span class="badge text-bg-<?= $badge ?>"><?= esc(ucfirst($user->status)) ?></span>
                </dd>

                <dt class="col-sm-4 text-muted">Role</dt>
                <dd class="col-sm-8">
                    <?php if (! empty($userRole)): ?>
                        <?= esc($userRole['name']) ?>
                        <?php if (! empty($isCustomRole)): ?>
                            <span class="badge text-bg-info ms-1">Custom access</span>
                        <?php endif; ?>
                        <span class="text-muted">(<?= (int) ($permCount ?? 0) ?> permissions)</span>
                    <?php else: ?>
                        <span class="text-muted">No role assigned</span>
                    <?php endif; ?>
                </dd>

                <dt class="col-sm-4 text-muted">Last Login</dt>
                <dd class="col-sm-8"><?= esc($user->last_login_at ?? 'Never') ?> <?= $user->last_login_ip ? '(' . esc($user->last_login_ip) . ')' : '' ?></dd>

                <dt class="col-sm-4 text-muted">Account Created</dt>
                <dd class="col-sm-8"><?= esc($user->created_at) ?></dd>

                <dt class="col-sm-4 text-muted">Must Change Password</dt>
                <dd class="col-sm-8"><?= $user->must_change_password ? 'Yes' : 'No' ?></dd>

                <dt class="col-sm-4 text-muted">TPIN</dt>
                <dd class="col-sm-8">
                    <?php if (! empty($tpinStatus['has_tpin'])): ?>
                        <span class="badge text-bg-success">Set</span>
                    <?php else: ?>
                        <span class="badge text-bg-secondary">Not set</span>
                    <?php endif; ?>
                    <?php if (! empty($tpinStatus['required'])): ?>
                        <span class="badge text-bg-warning text-dark ms-1">Required</span>
                    <?php endif; ?>
                    <?php if (! empty($tpinStatus['locked'])): ?>
                        <span class="badge text-bg-danger ms-1">Locked</span>
                    <?php endif; ?>
                </dd>
            </dl>
        </div>

        <?php if (empty($isOwnProfile)): ?>
        <div class="stat-card mt-3">
            <h2 class="h6 mb-3">Administrative Actions</h2>
            <div class="d-flex flex-wrap gap-2">
                <form action="<?= base_url('users/' . $user->id . '/reset-password') ?>" method="post" onsubmit="return confirm('Reset this user\'s password?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-warning"><i class="fa-solid fa-key me-1"></i>Reset Password</button>
                </form>

                <?php if ($user->status === 'active'): ?>
                    <form action="<?= base_url('users/' . $user->id . '/deactivate') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-ban me-1"></i>Deactivate</button>
                    </form>
                <?php else: ?>
                    <form action="<?= base_url('users/' . $user->id . '/activate') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline-success"><i class="fa-solid fa-check me-1"></i>Activate</button>
                    </form>
                <?php endif; ?>

                <form action="<?= base_url('users/' . $user->id . '/delete') ?>" method="post" onsubmit="return confirm('Move this user to trash?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fa-solid fa-trash me-1"></i>Delete</button>
                </form>
            </div>
        </div>

        <div class="stat-card mt-3">
            <h2 class="h6 mb-3">TPIN Assignment</h2>
            <p class="text-muted small mb-3">
                When enabled, this user must confirm with their TPIN on actions that use the TPIN module.
                The user sets the PIN themselves from their profile.
            </p>
            <div class="d-flex flex-wrap gap-2 align-items-center">
                <form action="<?= base_url('users/' . $user->id . '/tpin-required') ?>" method="post" class="d-flex align-items-center gap-2">
                    <?= csrf_field() ?>
                    <?php if (! empty($tpinStatus['required'])): ?>
                        <input type="hidden" name="tpin_required" value="0">
                        <button type="submit" class="btn btn-sm btn-outline-secondary">
                            <i class="fa-solid fa-toggle-on me-1"></i>Disable TPIN Requirement
                        </button>
                    <?php else: ?>
                        <input type="hidden" name="tpin_required" value="1">
                        <button type="submit" class="btn btn-sm btn-outline-primary">
                            <i class="fa-solid fa-toggle-off me-1"></i>Enable TPIN Requirement
                        </button>
                    <?php endif; ?>
                </form>

                <?php if (! empty($tpinStatus['has_tpin'])): ?>
                    <form action="<?= base_url('users/' . $user->id . '/tpin-clear') ?>" method="post" onsubmit="return confirm('Clear this user\'s TPIN? They will need to set a new one.');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline-danger">
                            <i class="fa-solid fa-eraser me-1"></i>Clear User TPIN
                        </button>
                    </form>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<?= $this->endSection() ?>
