<?php
$t = 'Редактирование пользователей';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank(3);
if (isset($_GET['password'])){
    $profile = mysqli_query($mysqli,"SELECT * FROM `users` WHERE `id` = ".intval($_GET['password']));
    if (mysqli_num_rows($profile) != 0) {$user = mysqli_fetch_object($profile);} else {echo msg_err('err',$lang['emptylogin']);require_once '../../wcore/foot.php';exit();}
    if (isset($_POST['OK'])){
        $pwd = isset($_POST['password']) && !empty($_POST['password'])?antixs($_POST['password']):$user['ps'];
        mysqli_query($mysqli,"UPDATE `users` SET `ps`='".security($pwd)."' WHERE `id` = '".intval($_GET['password'])."'");
        $mailsend->SendEmail($user->email, 'Сброс пароля на хостинге', 'Здравствуйте, '.$user->login.'!<br>Вы запросили сброс пароля на '.domain.'<br>Ваш логин: '.$user->login.'<br>Ваш новый пароль: '.$pwd.'<br>Это автоматическое сообщение и на него отвечать не требуется.<br>Восстановление пароля произвел администратор проекта.');
        echo msg_err('suc',$lang['admin_user_list_t15']);
        require_once '../../wcore/foot.php';
        exit();
    } else {
        echo $twig->render('admin_users.tpl', array('lang' =>$lang,'userq'=>'1','user'=>$user,'act'=>'password'));
    }
} elseif (isset($_GET['orders'])){
    $profile = mysqli_query($mysqli,"SELECT * FROM `users` WHERE `id` = ".intval($_GET['orders']));
    if (mysqli_num_rows($profile) != 0) {$user = mysqli_fetch_object($profile);} else {echo msg_err('err',$lang['emptylogin']);require_once '../../wcore/foot.php';exit();}
    
    $k_post=_mc('orders',"WHERE `uid` = '$user->id'");
    $k_page = k_page($k_post, 10);
    $page = page($k_page);
    $start = 10*$page-10;
    $_tmp = mysqli_query($mysqli,"SELECT * FROM `orders` WHERE `uid` = '$user->id' ORDER BY `oid` DESC LIMIT $start, 10") or die("Ошибка запроса: ".mysqli_error($mysqli));
    while ($ns = mysqli_fetch_assoc($_tmp)){
        $array_u[$ns['oid']] = $ns;
        $array_u[$ns['oid']]['data'] = json_decode($ns['data']);
        $array_u[$ns['oid']]['status'] = $lang['ord_status_'.$ns['status']];
        $array_u[$ns['oid']]['ptime'] = ptime($ns['time']).' ('.cunter_day_unix($ns['time'],false).')';
        
    }
    if (!isset($array_u)){$array_u = FALSE;}
    echo $twig->render('admin_users.tpl', array('user' => $array_u,'created'=>$k_post,'lang' =>$lang,'act'=>'orders','uid'=>$user->id));
    if ($k_page > 1){str('?list&',$k_page,$page);}
} elseif (isset($_GET['id'])){
    if (isset($_POST['ok'])){
        if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
        mysqli_query($mysqli,"UPDATE `users` SET `prv` = '".intval($_POST['dolz'])."', `bits` = '".antixs($_POST['bits'])."' WHERE `id` = '".intval($_GET['id'])."'");
        echo msg_err('suc',$lang['savech']);
    } else {
        $profile = mysqli_query($mysqli,"SELECT * FROM `users` WHERE `id` = ".intval($_GET['id']));
        if (mysqli_num_rows($profile) != 0) {$user = mysqli_fetch_object($profile);} else {echo msg_err('err',$lang['emptylogin']);require_once '../../wcore/foot.php';exit();}
        if ($user->prv == 3){$select['prv'] = 'a';} elseif ($user->prv == 2){$select['prv'] = 'm';} else {$select['prv'] = 'u';}
        echo $twig->render('admin_users.tpl', array('lang' =>$lang,'userq'=>intval($_GET['id']),'user'=>$user,'select'=>$select,'act'=>'edit','crfs'=>_csrf()));
    }
} elseif (isset($_GET['list'])){
    $k_post=_mc('users');
    $k_page = k_page($k_post, 10);
    $page = page($k_page);
    $start = 10*$page-10;
    $_tmp = mysqli_query($mysqli,"SELECT * FROM `users` ORDER BY `id` DESC LIMIT $start, 10") or die("Ошибка запроса: ".mysqli_error($mysqli));
    while ($ns = mysqli_fetch_assoc($_tmp)){
        $array_u[$ns['id']] = [
            'id'        =>  $ns['id'],
            'login'     =>  $ns['login'],
            'balance'   =>  $ns['bits'],
            'count'     =>  [
                'vhost' =>  mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `servers`,`orders` WHERE `servers`.`id`=`orders`.`l_s` AND `servers`.`type` = '1' AND `orders`.`uid` = '".$ns['id']."'")),
                'ress'  =>  mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `servers`,`orders` WHERE `servers`.`id`=`orders`.`l_s` AND `servers`.`type` = '2' AND `orders`.`uid` = '".$ns['id']."'")),
                'vds'   =>  mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `servers`,`orders` WHERE `servers`.`id`=`orders`.`l_s` AND `servers`.`type` = '3' AND `orders`.`uid` = '".$ns['id']."'")),
                'dedic' =>  mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `servers`,`orders` WHERE `servers`.`id`=`orders`.`l_s` AND `servers`.`type` = '4' AND `orders`.`uid` = '".$ns['id']."'")),
                'domain'=>  mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `servers`,`orders` WHERE `servers`.`id`=`orders`.`l_s` AND `servers`.`type` = '5' AND `orders`.`uid` = '".$ns['id']."'")),
            ],
        ];
    }
    if (!isset($array_u)){$array_u = FALSE;}
    echo $twig->render('admin_users_list.tpl', array('user' => $array_u,'created'=>$k_post,'lang' =>$lang,'act'=>'list'));
    if ($k_page > 1){str('?list&',$k_page,$page);}
} else {
    echo $twig->render('admin_users.tpl', array('lang' =>$lang,'userq'=>'0','act'=>'edit'));
}
require_once '../../wcore/foot.php';
?>