<?php
// +----------------------------------------------------------------------
// | 天方夜谭 [ 世界上最好用的QQ秒赞系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//页面标题
define('Title', '用户管理');

//获取数据
$only = GET('only');
if (!$info = $TFYT_DB->get_row("select * from ".PREFIX."users,".PREFIX."users_service where ".PREFIX."users.`only`='{$only}' and ".PREFIX."users.only=".PREFIX."users_service.only limit 1")){
    exit('<script language=\'javascript\'>alert(\'系统找不到要管理的用户！\');window.location.href=\'?Mould=Admin_Users\';</script>');
}

//修改
if (POST('action')=='edit'){
    $name = POST('name');
    $qq = POST('qq');
    $music_id = POST('music_id');
    $chat_bg = POST('chat_bg');
    $aqproblem = POST('aqproblem');
    $aqanswer = POST('aqanswer');
    $email = POST('email');
    $password = POST('password');
    if (empty($name) || empty($qq) || empty($music_id) || empty($chat_bg) || empty($aqproblem) || empty($aqanswer) || empty($email)){
        $arr = '<script language=\'javascript\'>alert(\'所有项不能为空（除密码外）！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
    }else{
        if (empty($password)){
            $set = "name='{$name}',qq='{$qq}',aqproblem='{$aqproblem}',aqanswer='{$aqanswer}',email='{$email}'";
        }else{
            $password = md5Pwd($password);
            $set = "name='{$name}',qq='{$qq}',aqproblem='{$aqproblem}',aqanswer='{$aqanswer}',email='{$email}',password='{$password}'";
        }
        $set2 = "chat_bg='{$chat_bg}',music_id='{$music_id}'";
        $TFYT_DB->query("update ".PREFIX."users set {$set} where only='{$only}'");
        $TFYT_DB->query("update ".PREFIX."users_service set {$set2} where only='{$only}'");
        $arr = '<script language=\'javascript\'>alert(\'修改成功！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
    }
    exit($arr);
}elseif (POST('action')=='vip'){
    $vip = POST('vip');
    if ($vip==''){
        $arr = '<script language=\'javascript\'>alert(\'请输入天数！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
    }else{
        if (get_isvip($info->vip,$info->vipend)){
            if ($vip<0){
                if (date(date('Y-m-d H:i:s'),strtotime("{$vip} day",strtotime($info->vipend)))<date('Y-m-d H:i:s')) $vip='0';
                $vipend=date("Y-m-d H:i:s",strtotime("$vip day",strtotime($info->vipend)));
                if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}' where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除他的'.$vip.'天会员！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除他的会员失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else{
                $vipend=date("Y-m-d H:i:s",strtotime("+ $vip day",strtotime($info->vipend)));
                if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}' where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功为他续费'.$vip.'天会员！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'为他续费会员失败失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }
        }else{
            if ($vip<0){
                if (date($info->vipend,strtotime("{$vip} day"))<date('Y-m-d H:i:s')) $vip='0';
                $vipend=date("Y-m-d H:i:s",strtotime("$vip day"));
                if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}' where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除他的'.$vip.'天会员！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除他的会员失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else{
                if ($info->vipstart==''){
                    $vipstart = ",vipstart='".date("Y-m-d H:i:s")."'";
                }else{
                    $vipstart = "";
                }
                $vipend=date("Y-m-d H:i:s",strtotime("+ $vip day"));
                if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}'$vipstart where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功为他开通'.$vip.'天会员！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'为他开通会员失败失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }
        }
    }
    exit($arr);
}elseif (POST('action')=='money'){
    $money = POST('money');
    if ($money==''){
        $arr = '<script language=\'javascript\'>alert(\'请输入彩虹屁数量！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
    }else{
        if ($money<0){
            if (($info->money-str_replace('-','',$money))<0) $money = '-0';
            if ($TFYT_DB->query("update ".PREFIX."users_service set money=money{$money} where only='{$info->only}'")){
                $arr = '<script language=\'javascript\'>alert(\'成功扣除'.str_replace('-','',$money).'个彩虹屁！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }else{
                $arr = '<script language=\'javascript\'>alert(\'扣除彩虹屁失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }
        }else{
            if ($TFYT_DB->query("update ".PREFIX."users_service set money=money+{$money} where only='{$info->only}'")){
                $arr = '<script language=\'javascript\'>alert(\'成功充值'.$money.'个彩虹屁！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }else{
                $arr = '<script language=\'javascript\'>alert(\'充值彩虹屁失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }
        }
    }
    exit($arr);
}elseif (POST('action')=='quota'){
    $mold = POST('mold');
    $quota = POST('quota');
    if ($quota==''){
        $arr = '<script language=\'javascript\'>alert(\'请输入配额数量！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
    }else{
        if ($quota<0){
            if ($mold=='p_quota'){
                if (($info->p_quota-str_replace('-','',$quota))<0) $quota = '-0';
                if ($TFYT_DB->query("update ".PREFIX."users_service set p_quota=p_quota{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除'.str_replace('-','',$quota).'个播放器配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除播放器配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else if ($mold=='s_quota'){
                if (($info->s_quota-str_replace('-','',$quota))<0) $quota = '-0';
                if ($TFYT_DB->query("update ".PREFIX."users_service set s_quota=s_quota{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除'.str_replace('-','',$quota).'个歌单配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除歌单配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else if ($mold=='u_quota'){
                if (($info->u_quota-str_replace('-','',$quota))<0) $quota = '-0';
                if ($TFYT_DB->query("update ".PREFIX."users_service set u_quota=u_quota{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除'.str_replace('-','',$quota).'个歌曲储存配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除歌曲储存失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else if($mold=='q_quota'){
                if (($info->q_quota-str_replace('-','',$quota))<0) $quota = '-0';
                if ($TFYT_DB->query("update ".PREFIX."users_service set q_quota=q_quota{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功扣除'.str_replace('-','',$quota).'个QQ挂机配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'扣除QQ挂机失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else{
                $arr = '<script language=\'javascript\'>alert(\'参数有误！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }
        }else{
            if ($mold=='p_quota'){
                if ($TFYT_DB->query("update ".PREFIX."users_service set p_quota=p_quota+{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功充值'.$quota.'个播放器配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'充值播放器配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }elseif ($mold=='s_quota'){
                if ($TFYT_DB->query("update ".PREFIX."users_service set s_quota=s_quota+{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功充值'.$quota.'个歌单配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'充值歌单配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }elseif ($mold=='u_quota'){
                if ($TFYT_DB->query("update ".PREFIX."users_service set u_quota=u_quota+{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功充值'.$quota.'个歌曲配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'充值歌曲配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }elseif ($mold=='q_quota'){
                if ($TFYT_DB->query("update ".PREFIX."users_service set q_quota=q_quota+{$quota} where only='{$info->only}'")){
                    $arr = '<script language=\'javascript\'>alert(\'成功充值'.$quota.'个QQ挂机配额！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }else{
                    $arr = '<script language=\'javascript\'>alert(\'充值QQ挂机配额失败！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
                }
            }else{
                $arr = '<script language=\'javascript\'>alert(\'参数有误！\');window.location.href=\'?Mould=Admin_Users_Set&only='.$only.'\';</script>';
            }
        }
    }
    exit($arr);
}elseif (GET('action')=='del'){
    $only = GET('only');
    if (empty($only)){
        $arr = '<script language=\'javascript\'>alert(\'系统找不到要删除的用户！\');window.location.href=\'?Mould=Admin_Users\';</script>';
    }else{
        $TFYT_DB->query("delete from ".PREFIX."users where `only`='{$only}'");
        $TFYT_DB->query("delete from ".PREFIX."users_service where `only`='{$only}'");
        $TFYT_DB->query("delete from ".PREFIX."users_lastlog where `uid`='{$info->uid}'");
        $TFYT_DB->query("delete from ".PREFIX."player where `only`='{$only}'");
        $TFYT_DB->query("delete from ".PREFIX."player_song where `only`='{$only}'");
        $TFYT_DB->query("delete from ".PREFIX."player_music where `only`='{$only}'");
        $TFYT_DB->query("delete from ".PREFIX."users_chat where `qq`='{$info->qq}'");
        $arr = '<script language=\'javascript\'>alert(\'删除成功！\');window.location.href=\'?Mould=Admin_Users\';</script>';
    }
    exit($arr);
}

//加载头部
Load_Template(Mains.'head.php');

//加载视图
Load_Template(Mains.'admin_users_set.php');

//加载底部
Load_Template(Mains.'foot.php');