<?php

/* DCMS Special
 * Дата последнего редактирования 01.10.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

$set['title'] = lang('Поиск по городу'); // заголовок страницы
require_once H . 'sys/inc/thead.php';

aut();

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='/?users'>" . lang('Пользователи') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Поиск по городу');
echo "</small></div><br />";

$sex = NULL;
if (isset($_SESSION['sex'])) {
    $sex = $_SESSION['sex'];
}
if (isset($_POST['sex'])) {
    $sex = $_POST['sex'];
}
$_SESSION['sex'] = $sex;
$sex = preg_replace("#( ){1,}#", "", $sex);

$ank_city = NULL;
if (isset($_SESSION['ank_city'])) {
    $ank_city = esc(stripcslashes(htmlspecialchars($_SESSION['ank_city'])));
}
if (isset($_POST['ank_city'])) {
    $ank_city = esc(stripcslashes(htmlspecialchars($_POST['ank_city'])));
}

$_SESSION['ank_city'] = $ank_city;
$ank_city = preg_replace("#( ){1,}#", "", $ank_city);
$age = NULL;

if (isset($_SESSION['age'])) {
    $age = esc(stripcslashes(htmlspecialchars($_SESSION['age'])));
}
if (isset($_POST['age'])) {
    $age = esc(stripcslashes(htmlspecialchars($_POST['age'])));
}
$_SESSION['age'] = $age;
$age = preg_replace("#( ){1,}#", "", $age);


if (isset($_GET['go']) && $ank_city != NULL) {
    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `sex` = '" . $sex . "' AND `ank_city` like '%" . $db->safesql($ank_city) . "%'");
    $k_post = $k_post['count'];
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

    if ($k_post == 0) {
        $doc->NoResult();
    }

    $q = $db->query("SELECT * FROM `user` WHERE `sex` = '" . $sex . "' AND `ank_city` like '%" . $db->safesql($ank_city) . "%' ORDER BY rating DESC LIMIT $start, $set[p_str]");
    while ($ank = $db->get_array($q)) {

        echo "<div class='list-group-item-null'>";

        echo user($ank['id']) . " \n";

        if ($ank['ank_d_r'] != NULL && $ank['ank_m_r'] != NULL && $ank['ank_g_r'] != NULL) {
            $ank['ank_age'] = date("Y") - $ank['ank_g_r'];
            if (date("n") < $ank['ank_m_r']) {
                $ank['ank_age'] = $ank['ank_age'] - 1;
            } elseif (date("n") == $ank['ank_m_r'] && date("j") < $ank['ank_d_r']) {
                $ank['ank_age'] = $ank['ank_age'] - 1;
            }
            echo "$ank[ank_age] лет\n";
        }

        if ($ank['ank_city'] != NULL) {
            echo ", $ank[ank_city]<br />\n";
        }
        echo "</div>\n";
    }

    if ($k_page > 1) {
        str("search.php?go&amp;", $k_page, $page);
    } // Вывод страниц
    echo "<a href=\"search.php\" class='list-group-item-null'><i class='fa fa-search fa-fw'></i> " . lang('Новый поиск') . "</a>";

    require_once H . 'sys/inc/tfoot.php';
} else {
    echo "<form method=\"post\" action=\"search.php?go\" class='list-group-item-null'>";
    echo lang('Пол') . ":<br />\n";
    echo "<select name=\"sex\" class='form-control'>\n";
    echo "<option value=\"1\">Мужской</option>\r\n";
    echo "<option value=\"0\">Женский</option>\r\n";
    echo "</select>\r\n";

    echo lang('Город') . ":<br/>";
    echo "<input type=\"text\" name=\"ank_city\" required class='form-control' value=\"$ank_city\"/>\n";

    echo lang('Возраст от') . ":<br />\r\n";
    echo "<input type=\"number\" value=\"\" class='form-control' style='width: 10%' name=\"age\" maxlength=\"2\" />";
    echo "<br />";

    $doc->Button('btn btn-primary btn-sm', null, 'search', 'Искать');
    echo "</form>\n";

    require_once H . 'sys/inc/tfoot.php';
}
