<?php
/**
 * User profile page top-bar for pages inside user module
 *
 * Variables:
 * `$uid`: int, user id
 * `$name`: string, user name
 * `$owner`: bool, if current visitor is the user himself
 * `$extra`: extra information to display on user topbar
 */

// Set uid if not set on controller
if (!isset($uid)) {
    $uid = Pi::user()->getId();
}

// Set name if not set on controller
if (!isset($name)) {
    $name = 'profile';
}

// Get user base info
if (!isset($user) || empty($user)) {
    $user = Pi::api('user', 'user')->get(
        $uid,
        ['name', 'gender', 'birthdate'],
        true,
        true
    );
}

// Check user
if (!$user) {
    Pi::service('url')->redirect(Pi::user()->getUrl('profile'));
    return;
}

// Check if the current visitor is owner of the user
if (!isset($owner)) {
    $owner = !_get('uid') && $uid ? true : false;
}

// Load nav
if ($owner) {
    $nav = Pi::api('nav', 'user')->getList($name);
} else {
    $nav = Pi::api('nav', 'user')->getList($name, $uid);

    $requestId = $uid == Pi::user()->getId() ? 0 : null;
    $level     = Pi::api('privacy', 'user')->getLevel($uid, $requestId);
    $user      = Pi::api('privacy', 'user')->filterProfile(
        $uid,
        $level,
        $user,
        'user'
    );
}

// Get module
$module = Pi::service('module')->current();

// Load quicklinks
$quicklink = Pi::api('quicklink', 'user')->getList();

// Check show nav
$showNav = isset($showNav) ? $showNav : 1;

// Check show setting
$showSetting = isset($showSetting) ? $showSetting : 1;
?>
<div class="clearfix row bg-light p-3">
    <div class="col-12 col-md-2 text-center mb-3">
        <div class="<?php if (!$owner) { ?>border-radius<?php } ?>">
            <?php echo Pi::avatar()->get($uid, 'xlarge', [
                'alt'   => _escape($user['name']),
                'class' => 'img-fluid rounded',
            ]); ?>
        </div>
        <?php if ($owner) { ?>
            <a class="user-avatar-upload"
               href="<?php echo $this->url('user', ['controller' => 'avatar', 'action' => 'index']); ?>">
                <?php _e('Change avatar'); ?>
            </a>
        <?php } ?>
    </div>
    <div class="col-12 col-md-6 mb-3">
        <h1 class="mb-3"><?php echo _escape($user['name']); ?></h1>
        <?php if (!empty($user['time_activated'])) { ?>
            <div class="mb-3">
                <?php _e('Member since'); ?>
                <?php echo _date($user['time_activated']); ?>
            </div>
        <?php } ?>
        <?php if (!empty($user['gender'])) { ?>
            <div class="mb-3">
                <?php _e('Gender:'); ?>
                <?php echo _escape($user['gender']); ?>
            </div>
        <?php } ?>
        <?php if (!empty($user['birthdate'])) { ?>
            <div class="mb-3">
                <?php _e('Birthdate:'); ?>
                <?php echo _escape($user['birthdate']); ?>
            </div>
        <?php } ?>
        <?php if (!empty($user['city'])) { ?>
            <div class="mb-3">
                <?php echo _escape($user['city']); ?>
                <?php if (!empty($user['country'])) { ?>
                    (<?php echo _escape($user['country']); ?>)
                <?php } ?>
            </div>
        <?php } ?>
    </div>
    <div class="col-12 col-md-3">
        <?php
        if (!empty($extra)) {
            echo '<span class="user-extra">' . $extra . '</span>';
        }
        ?>
        <?php if ($owner) { ?>
            <?php if ($module == 'user' && isset($userConfig) && !$userConfig['disable_front']) { ?>
                <a class="btn btn-secondary"
                   href="<?php echo $this->url('user', ['controller' => 'profile', 'action' => 'view', 'uid' => $uid]); ?>">
                    <i class="far fa-eye"></i>
                    <?php _e('View my profile'); ?>
                </a>
            <?php } ?>
            <?php if ($showSetting) { ?>
                <a class="btn btn-secondary"
                   href="<?php echo $this->url('user', ['controller' => 'account', 'action' => 'index']); ?>">
                    <i class="fas fa-cog"></i>
                    <?php _e('Settings'); ?>
                </a>
            <?php } ?>
            <?php if ($quicklink) { ?>
                <div class="dropdown inline">
                    <button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">
                        <i class="fas fa-magic"></i>
                        <?php _e('Quicklinks'); ?>

                    </button>
                    <div class="dropdown-menu">
                        <?php foreach ($quicklink as $item) { ?>
                            <a class="dropdown-item" href="<?php echo $item['link']; ?>" title="<?php echo $item['title']; ?>"
                               target="_blank">
                                <?php
                                if ($item['icon']) {
                                    echo sprintf('<i class="fa %s"></i>', $item['icon']);
                                }
                                ?>
                                <?php echo $item['title']; ?>
                            </a>
                        <?php } ?>
                    </div>
                </div>
            <?php } ?>
        <?php } else { ?>
            <?php if (Pi::user()->getId() == $uid && $module == 'user') { ?>
                <a class="btn btn-secondary"
                   href="<?php echo $this->url('user', ['controller' => 'profile', 'action' => 'index']); ?>">
                    <i class="fas fa-edit"></i>
                    <?php _e('Manage my profile'); ?>
                </a>
            <?php } ?>
            <?php if (Pi::user()->getId() != $uid && Pi::service('module')->isActive('message')) { ?>

                <?php if (!Pi::user()->getId()) { ?>
                    <button class="btn btn-secondary"
                            data-redirect="<?php echo $this->url('default', ['module' => 'message', 'controller' => 'index', 'action' => 'send', 'uid' => $uid]); ?>"
                            href="<?php echo (Pi::user()->config('enable_modal')) ? '#' : $this->url('default', ['module' => 'message', 'controller' => 'index', 'action' => 'send', 'uid' => $uid]); ?>"
                            <?php if (Pi::user()->config('enable_modal')) : ?>data-action="login" data-toggle="modal"
                            data-target="#loginRegisterModal"<?php endif; ?>
                    >
                        <i class="fas fa-envelope"></i>
                        <?php _e('Send message'); ?>
                    </button>
                <?php } else { ?>
                    <a class="btn btn-secondary"
                       href="<?php echo $this->url('default', ['module' => 'message', 'controller' => 'index', 'action' => 'send', 'uid' => $uid]); ?>">
                        <i class="fas fa-envelope"></i>
                        <?php _e('Send message'); ?>
                    </a>
                <?php } ?>
            <?php } else { ?>
                <a class="btn btn-secondary disabled">
                    <i class="fas fa-envelope"></i>
                    <?php _e('Send message'); ?>
                </a>
            <?php } ?>
        <?php } ?>
    </div>
</div>
<?php if ($showNav) { ?>
    <ul class="nav nav-tabs">
        <?php
        $maxItem = 7;
        $items   = $nav['items'];
        $length  = count($items);
        $index   = 0;
        for ($i = 0; $i < $length; $i++) {
            if ($items[$i]['name'] == $nav['cur']) {
                $index = $i;
                break;
            }
        }
        $isHighlight = $length > $maxItem && $index > $maxItem - 1 ? true : false;
        if ($isHighlight) {
            for ($i = 0; $i < $maxItem - 1; $i++) {
                $list[] = $items[$i];
            }
            $list[] = $items[$index];
            for ($i = $maxItem - 1; $i < $length; $i++) {
                if ($i != $index) {
                    $list[] = $items[$i];
                }
            }
            $index = $maxItem - 1;
        } else {
            $list = $items;
        }

        /* Cur nav item order < $maxItem - 1 */
        for ($i = 0; $i < $maxItem; $i++) {
            if ($i >= $length) {
                break;
            }
            $item = $list[$i];
            ?>
            <li class="<?php echo $i == $index ? 'active' : ''; ?> nav-item">
                <a class="nav-link" href="<?php echo $item['url']; ?>">
                    <?php
                    if ($item['icon']) {
                        echo '<i class="fa ' . $item['icon'] . '"></i> ';
                    }
                    echo _escape($item['title']);
                    if (isset($item['count'])) {
                        echo ' (' . $item['count'] . ')';
                    }
                    ?>
                </a>
            </li>
        <?php }
        if ($length > $maxItem) {
            ?>
            <li class="dropdown">
                <a data-toggle="dropdown" href="#">
                    <?php _e('More'); ?>

                </a>
                <div class="dropdown-menu">
                    <?php for ($i = $maxItem; $i < $length; $i++) {
                        $item = $list[$i]; ?>
                        <a class="dropdown-item" href="<?php echo $item['url']; ?>">
                            <?php
                            if ($item['icon']) {
                                echo '<i class="fa ' . $item['icon'] . '"></i> ';
                            }
                            echo _escape($item['title']);
                            ?>
                        </a>
                    <?php } ?>
                </div>
            </li>
        <?php } ?>
    </ul>
<?php } ?>
