<?php
$title = $l = "Музыка";
$lBack = profile:: getLink($user);

$thisLink = "/audio";
if (empty($_SESSION['audioSession']))
    $_SESSION['audioSession'] = substr(session_id(), 0, -15) . time(); // сессия файлов для каждого юзера
include_once coreAudio . 'class.vkParserNew.php';
$v = new vkParserNew();
$link = '';

if (filter_has_var(INPUT_POST, 'q')) {
    $q = trim($_POST['q'], " \r\n\t");
    $q = strip_tags(stripcslashes(htmlspecialchars($_POST['q'])));
    if (!empty($q)) {
        $link .= 'q/' . urlencode($q);
    }
    header('Location: ' . $link);
    exit();
}
$args = array(
    '_qq' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
    '_pp' => FILTER_SANITIZE_NUMBER_INT
);
$get = filter_input_array(INPUT_GET, $args);
$dQQ = urldecode($get['_qq']);
$data = $v->fetch((empty($dQQ) === true ? '?act=popular' : '?act=search&q=' . $get['_qq']));

if (empty($dQQ) === false)
    $link .= '/q/' . $get['_qq'];
$title = 'Поиск музыки';
require(CORE . "head.php");
?>
    <link type="text/css" rel="stylesheet" media="only screen"
          href="/template/audio/css/main.css?v=<?= filemtime(ROOT . 'template/audio/css/main.css') ?>"/>
    <div class="app-block">
        <noscript>
            <style> .ai_play:hover .i_play {
                    opacity: 0;
                } </style>
        </noscript>
        <div class="pcont audioPage">
            <div class="audioPage__header">
                <div class="audioPage__tabs">
                    <ul class="tabs">
                        <div class="hp_block tabs_block">
                            <ul class="tabs_list">
                                <?php
                                if (empty($get['_qq']) === true) {
                                    ?>
                                    <li><a class="tab_item tab_item_cur" href="/audio/">Популярное</a></li><?
                                } else {
                                    ?>
                                    <li><a class="tab_item" href="/audio/">Популярное</a></li><?
                                    ?>
                                    <li><a class="tab_item tab_item_cur" href="/audio/q/<?= $get['_qq'] ?>">Поиск</a>
                                    </li><?
                                }
                                ?>
                                <li><a class="tab_item" href="/audio/radio">Радио</a></li>
                            </ul>
                        </div>
                    </ul>
                </div>
                <div class="audioPage__search">
                    <div class="hp_block">
                        <form action="/audio/" class="qsearch" method="post">
                            <table class="row_table">
                                <tbody>
                                <tr>
                                    <td class="row_table_main_column">
                                        <div class="qs_field_wrap">
                                            <div class="iwrap">
                                                <input type="text" class="textfield qs_textfield" name="q"
                                                       value="<?= $dQQ ?>" autocomplete="off" placeholder="Поиск">
                                            </div>
                                        </div>
                                    </td>
                                    <td class="row_table_last_column">
                                        <button type="submit" class="audio_search"><i class="audio_search"></i></button>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
            <div class="upanel">
                <div id="au_search_items" class="bl_cont">
                    <?
                    $limit = 20;
                    $itemData = array_slice($data['item'], $get['_pp'], $limit, true);
                    $dataCount = count($data['item']);
                    if (count($itemData) === 0)
                    {
                        ?>
                        <div class="service_msg_box">
                            <div class="service_msg service_msg_null">Нет ни одной аудиозаписи.</div>
                        </div>
                        <?
                    }
                    else
                    {
                    if($get['_pp'] >= $limit)
                    {
                        ?>
                        <div class="show_next_wrap">
                            <a class="show_next" href="/audio<?= (($get['_pp'] - $limit) == 0 ? '' : '/p/' . ($get['_pp'] - $limit)) . $link ?>">Показать предыдущие</a>
                        </div>
                        <?
                    }
                    ?>
                    <div class="audios_block audios_list _si_container"><?
                        foreach ($itemData AS $key => $item)
                        {
                        if (empty($dQQ) === false) {
                            $pattern = implode('|', explode(' ', $dQQ));
                            $item['title'] = preg_replace('~' . $pattern . '~i', '<em class="found">$0</em>', $item['title']);
                            $item['artist'] = preg_replace('~' . $pattern . '~i', '<em class="found">$0</em>', $item['artist']);
                        }
                        ?>
                        <div id="audio<?= $key ?>" data-id="<?= $key ?>" class="audioPlay audio_item ai_has_btn">
                            <div class="ai_info">
                                <div class="ai_play"
                                     onclick="audioplayer.playPause(event, '<?= $key ?>', true);"<?= (empty($item['coverServer']) === false ? ' style="background-image: url(\'/audio/get/' . $data['nameCache'] . '/cover/' . $item['cover'] . '\')' : '') ?>
                                ">
                                <i class="i_play"></i>
                            </div>
                            <div class="ai_download">
                                <a href="/audio/get/<?= $data['nameCache'] ?>/download/<?= $item['file'] ?>">
                                    <i class="i_download"></i>
                                </a>
                            </div>
                            <div class="ai_body">
                                <div class="ai_dur" data-dur="<?= $item['dur'] ?>"
                                     onclick="audioplayer.switchTimeFormat(this, event);"><?= $item['cDur'] ?></div>
                                <div class="ai_label">
                                    <span class="ai_title"><?= $item['title'] ?></span>
                                    <span class="dividers"> – </span>
                                    <span class="ai_artist"><?= $item['artist'] ?></span>
                                </div>
                                <input type="hidden"
                                       value="<?= 'http://' . $_SERVER['HTTP_HOST'] ?>/audio/get/<?= $data['nameCache'] ?>/music/<?= $item['file'] ?>">
                            </div>
                        </div>
                        <div class="ai_controls">
                            <table class="row_table">
                                <tbody>
                                <tr>
                                    <td class="aic_progress_wrap">
                                        <div class="aic_line" onmousedown="audioplayer.setPosition(this, event);"
                                             onclick="cancelEvent(event);">
                                            <div class="aic_ln aic_back_line slider_loading_bar"
                                                 onclick="cancelEvent(event);"></div>
                                            <div class="aic_ln aic_load_line" onclick="cancelEvent(event);"></div>
                                            <div class="aic_ln aic_pl_wrap" onclick="cancelEvent(event);">
                                                <div class="aic_ln aic_progress_line" onclick="cancelEvent(event);">
                                                    <div class="aic_slider" onclick="cancelEvent(event);"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="aic_volume_wrap">
                                        <div class="aic_line" onmousedown="audioplayer.setVolume(this, event);"
                                             onclick="cancelEvent(event);">
                                            <div class="aic_ln aic_back_line" onclick="cancelEvent(event);"></div>
                                            <div class="aic_ln aic_pl_wrap" onclick="cancelEvent(event);">
                                                <div class="aic_ln aic_progress_line" onclick="cancelEvent(event);">
                                                    <div class="aic_slider" onclick="cancelEvent(event);"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?
                }
                ?>
                </div>
                <?
                if($dataCount > $limit && $dataCount > ($get['_pp'] + $limit))
                {
                    ?>
                    <div class="show_more_wrap">
                        <a class="show_more" href="<?= '/audio/p/' . ($get['_pp'] + $limit) . $link ?>">Показать ещё аудиозаписи</a>
                    </div>
                    <?
                }
                }
                ?>
            </div>
        </div>
    </div>
<?
require(CORE . "foot.php");
?>