<?php

$device_type_array = array( 'ios' => 1, 'android' => 2);
//1 安卓 2 ios
$device_type = $device_type_array[trim($_SERVER['HTTP_PLATFORM'])];

$udid = $_SERVER['HTTP_MOCUZUDID'];

$uid = $_G['uid'];
$ui
$pagesize = 10;

$setting = get_mocuz_setting(array('invite_self_num', 'invite_user_num', 'invite_comments', 'invite_comment_open','invite_score_type', 'invite_rule'));



$score_type = $setting['invite_score_type'];
$score_type_name = $_G['setting']['extcredits'][$score_type]['title'];
$extcredits = 'extcredits'.$score_type;




if($_GET['action'] == 'check'){
	$device_type = $device_type_array[trim($_GET['platform'])];
	$udid = $_GET['udid'];
	if(!$uid){
		echo 'no login';
		exit;
	}


	$log = C::t('mocuz_invite_log')->fetch_user_by_udid_or_uid($udid, $uid);

	if(!empty($log)){
		echo json_encode(array('code' => -1, 'msg' => '一个账号/设备只能参与一次'));
		exit;
	}

	$code = trim($_GET['code']);


	//自己得积分
	if($code == '00000000'){
		$log_id = C::t('mocuz_invite_log')->insert_log(2, $udid, $device_type, $uid);
		updatemembercount($uid, array($extcredits => '+'.$setting['invite_self_num']), true, 'INV', $log_id, '');
		echo json_encode(array('code' => 0, 'msg' => '成功'));
		exit;
	}else{

		$code_info  = C::t('mocuz_invite')->fetch_user_by_code($code);

		if(empty($code_info)){
			echo json_encode(array('code' => -2, 'msg' => '您输入的邀请码不存在'));
			exit;
		}

		if($code_info['uid'] == $uid){
			echo json_encode(array('code' => -2, 'msg' => '您不能邀请自己啊'));
			exit;
		}

		$log_id = C::t('mocuz_invite_log')->insert_log(2, $udid, $device_type, $uid, $code_info['uid']);

		//自己增加
		updatemembercount($uid, array($extcredits => '+'.$setting['invite_self_num']), true, 'INV', $log_id, '');
		//对方增加
		updatemembercount($code_info['uid'], array($extcredits => '+'.$setting['invite_user_num']), true, 'INV', $log_id, '');

		//更新邀请人数和积分
		C::t('mocuz_invite')->update_invite($setting['invite_user_num'],$code_info['uid']);


		$comments = explode("\n", $setting['invite_comments']);
		$comment = array_rand($comments);

		C::t('mocuz_invite_comment')->insert_comment($uid, $_G['username'], $comments[$comment], $setting['invite_self_num']);

		echo json_encode(array('code' => 0, 'msg' => '成功'));
		exit;

	}
}elseif($_GET['action'] == 'down'){

	$uid = intval($_GET['downloadid']);
	$code_info = C::t('mocuz_invite')->fetch_user_by_uid($uid);

	if(empty($code_info)){
		echo 'error';
		exit;
	}
	$comments = C::t('mocuz_invite_comment')->fetch_comments(0,$pagesize);
	include 'template/invite/download.php';
	exit;
}elseif($_GET['action'] == 'downcomment'){
	$page = intval($_GET['page']);
	$start = ($page - 1) * $pagesize;
	$comments = C::t('mocuz_invite_comment')->fetch_comments($start,$pagesize);

	if(empty($comments)){
		echo '';
		exit;
	}
	include 'template/invite/comments.php';
	exit;

}elseif($_GET['action'] == "rank"){
	echo $uid;exit;
	if(!$uid){
		echo 'no login';
		exit;
	}

	$pagesize = 50;
	$page = $_GET['page'] ? intval($_GET['page']) : 1;
	$start = ($page - 1) * $pagesize;
	//计算总页数
	$count = DB::fetch_all("SELECT count(*) as num FROM ".DB::table("mocuz_invite"));
	$count = $count[0]['num'];
	$total_page = intval(ceil($count/$pagesize));
	
	//我的排行
	$total = DB::fetch_all("SELECT uid,username,invite_num FROM ".DB::table("mocuz_invite")." WHERE invite_num > 0 ORDER BY invite_num DESC");
	for($i=0;$i<count($total);$i++){
		if($total[$i]['invite_num'] == $total[$i-1]['invite_num']){
			$total[$i]['rank'] = $total[$i-1]['rank'];
		}else{
			$total[$i]['rank'] = $i +1;
		}
		if($total[$i]['uid'] == $uid){
			$my_rank = $total[$i];
			if($total[$i]['rank'] > 999){
				$my_rank['rank'] = "999+";
			}
		}
	}

	$rank = DB::fetch_all("SELECT * FROM ".DB::table("mocuz_invite")." WHERE invite_num > 0 ORDER BY invite_num DESC limit $start,$pagesize");

	if(!empty($rank)){
		$j = 0;

		for($i=0;$i<count($rank);$i++){
			if($page >1){
				if($i == 0){	
					if($rank[$i]['invite_num'] == $end_rank){
						$j = $rank[$i]['rank'] = $end['rank'];
					}else{
						$j = ($page - 1) * $pagesize + $i + 1;	
					}
				}else{	
					$j = ($page - 1) * $pagesize + $i + 1;	
				}	
			}else{
				$j++;	
			}
			$rank[$i]['rank'] = $j;
			if($rank[$i]['invite_num'] == $rank[$i-1]['invite_num']){
				$rank[$i]['rank'] = $rank[$i-1]['rank'];
			}
			if($i == count($rank)-1){
				$k = $rank[count($rank)-1];
				$end_rank = $k['rank'];
			}
			
		}	

	}else{	
		include 'template/invite/no_message.html';
	}
	
	include 'template/invite/rank.php';
	exit;
}elseif($_GET['action'] == "invite_log"){
	if(!$uid){
		echo 'no login';
		exit;
	}
	
	$invite_log = DB::fetch_all("SELECT uid,create_time FROM ".DB::table("mocuz_invite_log")." WHERE invite_uid = $uid");
	if(!empty($invite_log)){
		foreach($invite_log as $key => $val){
			$username = getuserbyuid($val['uid']);
			$username = $username['username'];
			$invite_log[$key]['username'] = $username;

		}
		// print_r($invite_log);exit;
		include 'template/invite/invite_log.php';
	}else{
		include 'template/invite/no_message.html';exit;
	}

}else{
	if(!$uid){
		echo 'no login';
		exit;
	}

	$user_info = C::t('mocuz_invite')->init_user($uid, $_G['username']);
	$log = C::t('mocuz_invite_log')->fetch_user_by_udid_or_uid($udid, $uid);
	include 'template/invite/invite.php';
	exit;
}








//获取用户没有则创建
//显示页面

//验证兑换码
//000000
//查库判断

//通过插入评论（后台设置）
//更新邀请数
//增加金钱
//
//
//
