<?php
error_reporting(0);
global $db, $CcTvAi_com_userid;
if(!eregi($_SERVER['SERVER_NAME'], $_SERVER['HTTP_REFERER'])) {
    exit();
}
close_browse();
if($CcTvAi_com_userid) {
    if(cd_vipoff == "no") {
        exit('10001');
    }
    if($CcTvAi_com_points >= cd_vippoints) {
    if($CcTvAi_com_grade == 1) {
        $cd_vippoints=cd_vippoints;
        $cd_viptime=cd_viptime;
        $cd_vipenddate=date("Y-m-d", strtotime("+".$cd_viptime." day", strtotime($CcTvAi_com_vipenddate)));
        $db->query("update " . tname('user') . " set cd_points=cd_points-$cd_vippoints,cd_vipenddate='$cd_vipenddate' where cd_id='$CcTvAi_com_userid'");
        //记录账单
        $setarr=array(
            'cd_type'=>0, //1为加,0为减
            'cd_uid'=>$CcTvAi_com_userid,
            'cd_uname'=>$CcTvAi_com_username,
            'cd_icon'=>'payvip',
            'cd_title'=>'续费'.cd_viptime.'天VIP会员',
            'cd_points'=>cd_vippoints,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('bill', $setarr, 1);
        //发送通知
        $setarr=array(
            'cd_uid'=>0,
            'cd_uname'=>'系统提示',
            'cd_uids'=>$CcTvAi_com_userid,
            'cd_unames'=>$CcTvAi_com_username,
            'cd_icon'=>'payvip',
            'cd_data'=>cd_vipxtxt,
            'cd_dataid'=>0,
            'cd_state'=>1,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('notice', $setarr, 1);
        exit('10004');
    }else{
        $cd_vippoints=cd_vippoints;
        $cd_viptime=cd_viptime;
        $cd_vipindate=date('Y-m-d H:i:s');
        $tomorrow=mktime(date("H"), date("i"), date("s"), date("m"), date("d")+$cd_viptime, date("Y"));
        $cd_vipenddate=date("Y-m-d H:i:s", $tomorrow);
        $db->query("update " . tname('user') . " set cd_points=cd_points-$cd_vippoints,cd_vipgrade=1,cd_grade=1,cd_vipindate='$cd_vipindate',cd_vipenddate='$cd_vipenddate' where cd_id='$CcTvAi_com_userid'");
        //记录账单
        $setarr=array(
            'cd_type'=>0, //1为加,0为减
            'cd_uid'=>$CcTvAi_com_userid,
            'cd_uname'=>$CcTvAi_com_username,
            'cd_icon'=>'payvip',
            'cd_title'=>'充值'.cd_viptime.'天VIP会员',
            'cd_points'=>cd_vippoints,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('bill', $setarr, 1);
        //发送通知
        $setarr=array(
            'cd_uid'=>0,
            'cd_uname'=>'系统提示',
            'cd_uids'=>$CcTvAi_com_userid,
            'cd_unames'=>$CcTvAi_com_username,
            'cd_icon'=>'payvip',
            'cd_data'=>cd_vipktxt,
            'cd_dataid'=>0,
            'cd_state'=>1,
            'cd_addtime'=>date('Y-m-d H:i:s')
        );
        inserttable('notice', $setarr, 1);
        exit('10005');
	}
    } else {
        exit('10002');
    }
} else {
    exit('20001');
}
?>