<?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', '在线商城');

//购买商品
if (POST('action')=='buy'){
    $id = POST('id');
    if (!$info = $TFYT_DB->get_row("select * from ".PREFIX."website_goods where id='{$id}'")){
        $arr = array('state' => 'no', 'msg' => '系统找不到你要购买的商品！');
    }else{
        if ($GLOBALS['users']['money']<$info->money){
            $arr = array('state' => 'no', 'msg' => '余额不足以扣除，请充值！');
        }else{
            switch ($info->mold){
                case 'vip':
                    $num = $info->stock;
                    if (get_isvip($GLOBALS['users']['vip'],$GLOBALS['users']['vipend'])){
                        $vipend=date("Y-m-d H:i:s",strtotime("+ $num day",strtotime($GLOBALS['users']['vipend'])));
                        if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}' where only='{$GLOBALS['users']['only']}'")){
                            $arr = array('state'=>'ok','msg'=>'购买成功，成功续费'.$num.'天会员！');
                        }else{
                            $arr = array('state'=>'no','msg'=>'购买失败，续费会员失败！');
                        }
                    }else{
                        if ($GLOBALS['users']['vipstart']==''){
                            $vipstart = ",vipstart='".date("Y-m-d H:i:s")."'";
                        }else{
                            $vipstart = "";
                        }
                        $vipend=date("Y-m-d H:i:s",strtotime("+ $num day"));
                        if ($TFYT_DB->query("update ".PREFIX."users_service set vip=1,vipend='{$vipend}'$vipstart where only='{$GLOBALS['users']['only']}'")){
                            $arr = array('state'=>'ok','msg'=>'购买成功，成功开通'.$num.'天会员！');
                        }else{
                            $arr = array('state'=>'no','msg'=>'购买失败，开通会员失败！');
                        }
                    }
                    break;
                case 'money':
                    if ($TFYT_DB->query("update ".PREFIX."users_service set money=money+'{$info->stock}' where only='{$GLOBALS['users']['only']}'")){
                        $arr = array('state'=>'ok','msg'=>'恭喜你，成功充值'.$info->stock.'个彩虹屁！');
                    }else{
                        $arr = array('state'=>'no','msg'=>'充值彩虹屁失败，请稍后再试！');
                    }
                    break;
                case 'q_quota':
                    if ($TFYT_DB->query("update ".PREFIX."users_service set q_quota=q_quota+'{$info->stock}' where only='{$GLOBALS['users']['only']}'")){
                        $arr = array('state'=>'ok','msg'=>'恭喜你，成功充值'.$info->stock.'个QQ挂机配额！');
                    }else{
                        $arr = array('state'=>'no','msg'=>'充值挂机配额失败，请稍后再试！');
                    }
                    break;
                case 'p_quota':
                    if ($TFYT_DB->query("update ".PREFIX."users_service set p_quota=p_quota+'{$info->stock}' where only='{$GLOBALS['users']['only']}'")){
                        $arr = array('state'=>'ok','msg'=>'恭喜你，成功充值'.$info->stock.'个播放器配额！');
                    }else{
                        $arr = array('state'=>'no','msg'=>'充值播放器配额失败，请稍后再试！');
                    }
                    break;
                case 's_quota':
                    if ($TFYT_DB->query("update ".PREFIX."users_service set s_quota=s_quota+'{$info->stock}' where only='{$GLOBALS['users']['only']}'")){
                        $arr = array('state'=>'ok','msg'=>'恭喜你，成功充值'.$info->stock.'个歌单配额！');
                    }else{
                        $arr = array('state'=>'no','msg'=>'充值歌单配额失败，请稍后再试！');
                    }
                    break;
                case 'u_quota':
                    if ($TFYT_DB->query("update ".PREFIX."users_service set u_quota=u_quota+'{$info->stock}' where only='{$GLOBALS['users']['only']}'")){
                        $arr = array('state'=>'ok','msg'=>'恭喜你，成功充值'.$info->stock.'个音乐储存配额！');
                    }else{
                        $arr = array('state'=>'no','msg'=>'充值音乐储存配额失败，请稍后再试！');
                    }
                    break;
                default:
                    $arr = array('state' => 'no', 'msg' => '系统找不到要购买的商品！');
                    break;
            }
            //扣除余额
            $TFYT_DB->query("update ".PREFIX."users_service set money=money-{$info->money} where only='{$GLOBALS['users']['only']}'");
        }
    }
    exit(json_encode($arr));
}

//获取数据
$mold = GET('mold');
if (empty($mold)){
    $list = $TFYT_DB->get_results("select * from ".PREFIX."website_goods order by id asc");
}else{
    $list = $TFYT_DB->get_results("select * from ".PREFIX."website_goods where mold='".$mold."' order by id asc");
}

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

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

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