<?php

    /*!
     * ifsoft.co.uk v1.1
     *
     * http://ifsoft.com.ua, http://ifsoft.co.uk
     * qascript@ifsoft.co.uk
     *
     * Copyright 2012-2017 Demyanchuk Dmitry (https://vk.com/dmitry.demyanchuk)
     */

    if (!$auth->authorize(auth::getCurrentUserId(), auth::getAccessToken())) {

        header('Location: /');
    }

    $profile = new profile($dbo, auth::getCurrentUserId());

    if (isset($_GET['action'])) {

        $notifications = new notify($dbo);
        $notifications->setRequestFrom(auth::getCurrentUserId());

        $notifications_count = $notifications->getNewCount($profile->getLastNotifyView());

        echo $notifications_count;
        exit;
    }

    $blacklist = new blacklist($dbo);
    $blacklist->setRequestFrom(auth::getCurrentUserId());

    $items_all = $blacklist->myActiveItemsCount();
    $items_loaded = 0;

    if (!empty($_POST)) {

        $itemId = isset($_POST['itemId']) ? $_POST['itemId'] : '';
        $loaded = isset($_POST['loaded']) ? $_POST['loaded'] : '';

        $itemId = helper::clearInt($itemId);
        $loaded = helper::clearInt($loaded);

        $result = $blacklist->get($itemId);

        $items_loaded = count($result['items']);

        $result['items_loaded'] = $items_loaded + $loaded;
        $result['items_all'] = $items_all;

        if ($items_loaded != 0) {

            ob_start();

            foreach ($result['items'] as $key => $value) {

                draw($value, $LANG, $helper);
            }

            $result['html'] = ob_get_clean();

            if ($result['items_loaded'] < $items_all) {

                ob_start();

                ?>

                <header class="top-banner loading-banner">

                    <div class="prompt">
                        <button onclick="BlackList.more('<?php echo $result['itemId']; ?>'); return false;" class="button green loading-button"><?php echo $LANG['action-more']; ?></button>
                    </div>

                </header>

                <?php

                $result['banner'] = ob_get_clean();
            }
        }

        echo json_encode($result);
        exit;
    }

    $page_id = "blacklist";

    $css_files = array("main.css", "my.css");
    $page_title = $LANG['page-blacklist']." | ".APP_TITLE;

    include_once("../html/common/header.inc.php");

?>

<body class="job-listings">


    <?php
        include_once("../html/common/topbar.inc.php");
    ?>


    <div class="wrap content-page">

        <div class="main-column">

            <div class="main-content">

                <div class="job-listings-page content-list-page">

                    <div class="standard-page" style="padding-bottom: 0">

                    <h1><?php echo $LANG['page-blacklist']; ?></h1>

                        <div class="tab-container" style="border: 0">
                            <nav class="tabs">
                                <a href="/account/settings/profile"><span class="tab"><?php echo $LANG['page-profile-settings']; ?></span></a>
                                <a href="/account/settings/privacy"><span class="tab"><?php echo $LANG['label-privacy']; ?></span></a>
                                <a href="/account/settings/services"><span class="tab"><?php echo $LANG['label-services']; ?></span></a>
                                <a href="/account/settings/profile/password"><span class="tab"><?php echo $LANG['label-password']; ?></span></a>
                                <a href="/account/balance"><span class="tab"><?php echo $LANG['page-balance']; ?></span></a>
                                <a href="/account/settings/blacklist"><span class="tab active"><?php echo $LANG['label-blacklist']; ?></span></a>
                                <a href="/account/settings/profile/deactivation"><span class="tab"><?php echo $LANG['action-deactivation-profile']; ?></span></a>

                            </nav>
                        </div>
                    </div>

                    <?php

                    $result = $blacklist->get(0);

                    $items_loaded = count($result['items']);

                    if ($items_loaded != 0) {

                        ?>

                            <ul class="items-list content-list">

                                <?php

                                    foreach ($result['items'] as $key => $value) {

                                        draw($value, $LANG, $helper);
                                    }
                                ?>

                            </ul>

                        <?php

                    } else {

                        ?>

                        <header class="top-banner info-banner">

                            <div class="info">
                                <h1><?php echo $LANG['label-empty-list']; ?></h1>
                            </div>

                        </header>

                        <?php
                    }
                    ?>

                    <?php

                        if ($items_all > 20) {

                            ?>

                            <header class="top-banner loading-banner">

                                <div class="prompt">
                                    <button onclick="BlackList.more('<?php echo $result['itemId']; ?>'); return false;" class="button green loading-button"><?php echo $LANG['action-more']; ?></button>
                                </div>

                            </header>

                            <?php
                        }
                    ?>


                </div>

            </div>
        </div>

        <?php

            include_once("../html/common/sidebar.inc.php");
        ?>

    </div>

    <?php

        include_once("../html/common/footer.inc.php");
    ?>

    <script type="text/javascript" src="/js/jquery.tipsy.js"></script>

    <script type="text/javascript">

        var items_all = <?php echo $items_all; ?>;
            var items_loaded = <?php echo $items_loaded; ?>;

    </script>


</body
</html>

<?php

    function draw($item, $LANG, $helper)
    {

        $time = new language(NULL, $LANG['lang-code']);

        $profilePhotoUrl = "/img/profile_default_photo.png";

        if (strlen($item['blockedUserPhotoUrl']) != 0) {

            $profilePhotoUrl = $item['blockedUserPhotoUrl'];

        }

        ?>

                <li class="custom-list-item item" data-id="<?php echo $item['id']; ?>">

                    <a href="/<?php echo $item['blockedUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $item['blockedUserUsername']; ?>" class="custom-item-link"><?php echo  $item['blockedUserFullname']; ?></a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($item['createAt']); ?></span>
                        <span class="post-date"><a href="javascript:void(0);" onclick="BlackList.remove('<?php echo $item['id']; ?>', '<?php echo $item['blockedUserUsername']; ?>', '<?php echo auth::getAuthenticityToken(); ?>'); return false;"><?php echo $LANG['action-unblock']; ?></a></span>

                    </div

                </li>

        <?php
    }

?>