<?php

include_once('../../sys/inc/core.php');
include_once('inc/configs.php');
only_reg();
$set['title'] = lang('Сменить обложку');
include_once H . 'sys/inc/thead.php';

aut();

switch (@$_GET['action']):
    case 'select':
        if (!isset($_GET['cover_id']) || !mysql_result(mysql_query("SELECT COUNT(*) FROM `profileCoversList` WHERE `id` = '" . intval(@$_GET['cover_id']) . "'"), 0)) {
            echo "<div class='list-group-item'>\n";
            echo "Обложка не найдена<br />\n";
            echo "</div>\n";
            echo "<div class='list-group-item'>\n";
            echo "<a href='?'>Назад</a><br />\n";
            echo '</div>';
            include_once(FOOT);
        }
        $cover = mysql_fetch_array(mysql_query("SELECT * FROM `profileCoversList` WHERE `id` = '" . intval($_GET['cover_id']) . "'"));
        $category = mysql_fetch_array(mysql_query("SELECT * FROM `profileCoversCategories` WHERE `id` = '$cover[id_category]'"));
        if (isset($_POST['submit'])) {
            if ($user['money'] < $cover['price'])
                $err[] = 'Вам не хватает ' . $sklon_balance_array[2] . ' что бы купить эту обложку';
            elseif ($user['profileCover'] == $cover['id'])
                $err[] = 'Эта обложка уже установлена';
            else {
                mysql_query("UPDATE `user` SET `profileCover` = '$cover[id]', `money` = '" . ($user['money'] - $cover['price']) . "' WHERE `id` = '$user[id]'");
                mysql_query("UPDATE `user` SET `profileCover` = '$cover[id]' WHERE `id` = '$user[id]'");
                header("Location: /info.php");
                exit();
            }
        }
        err();
        echo "<form method='POST' class='list-group-item' action=''>\n";
        echo "<div>\n";
        echo "<img src='/style/covers/{$cover['id']}.jpg' alt='*' style='width: 100%' />\n";
        echo "</div>\n";
        echo "<div>\n";
        echo "<b>Цена:</b> " . sklon_text($cover['price'], $sklon_balance_array) . "<br />\n";
        echo "<b>У Вас:</b> " . sklon_text($user['money'], $sklon_balance_array) . "<br />\n";
        echo "</div>\n";
        echo "<input type='submit' name='submit' value='Заменить' /><br />\n";
        echo "</form>\n";
        echo "<div class='list-group-item'>\n";
        echo "<a href='?action=category&category_id=$category[id]'>Назад</a><br />\n";
        echo "</div>\n";
        break;
    case 'category':
        if (!isset($_GET['category_id']) || !mysql_result(mysql_query("SELECT COUNT(*) FROM `profileCoversCategories` WHERE `id` = '" . intval(@$_GET['category_id']) . "'"), 0)) {
            echo "<div class='list-group-item'>\n";
            echo "Категория не найдена<br />\n";
            echo "</div>\n";
            echo "<div class='list-group-item'>\n";
            echo "<a href='?'>Назад</a><br />\n";
            echo '</div>';
            include_once(FOOT);
        }
        $category = mysql_fetch_array(mysql_query("SELECT * FROM `profileCoversCategories` WHERE `id` = '" . intval($_GET['category_id']) . "'"));
        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `profileCoversList` WHERE `id_category` = '$category[id]'"), 0);
        if (!$k_post) {
            $doc->NoResult();
        }
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];
        $q = mysql_query("SELECT * FROM `profileCoversList` WHERE `id_category` = '$category[id]' ORDER BY `price` ASC LIMIT $start, $set[p_str]");
        while ($post = mysql_fetch_assoc($q)) {
            echo "<div class='list-group-item'>";
            echo "<div style='float: left;'>\n";
            echo "<a href='?action=select&cover_id=$post[id]'>\n";
            show_cover_preview($post['id'], $preview_sizes['small']);
            echo "</a>\n";
            echo "</div>\n";
            echo "<div style='overflow: hidden;'>\n";
            echo "<b>Цена:</b> " . sklon_text($post['price'], $sklon_balance_array) . "<br />\n";
            echo "</div>\n";
            echo "<div style='clear: both;'></div>\n";
            echo "</div>\n";
        }
        if ($k_page > 1) {
            str("?action=category&category_id=$category[id]&", $k_page, $page);
        }
        echo "<div class='list-group-item'>\n";
        echo "<a href='?'>Назад</a><br />\n";
        echo '</div>';
        break;
    default:
        $q = mysql_query("SELECT * FROM `profileCoversCategories` ORDER BY `name` DESC");
        while ($post = mysql_fetch_assoc($q)) {
            echo "<div class='list-group-item'>";
            echo "<a href='?action=category&category_id=$post[id]'><i class='fa fa-folder fa-fw'></i> " . htmlspecialchars($post['name']) . "</a> (" . mysql_result(mysql_query("SELECT COUNT(*) FROM `profileCoversList` WHERE `id_category` = '$post[id]'"), 0) . ")<br />\n";
            echo "</div>\n";
        }
        echo "<div class='list-group-item'>\n";
        echo "<a href='/info.php'>Назад</a><br />\n";
        echo "</div>\n";
        break;
endswitch;
include_once(FOOT);
?>