<?php
	if( !is_array($active) ){
		ob_clean();
		header("Location: index.php?id=".$fid);	
	}
?>
<script type="text/javascript">
	window.scroll(0,0);
</script>
<?php

	//防止重复提交
	$verify = $_POST["verify"];
	
	if( !$verify || $verify == $_SESSION["__VERIFY"] ){
		echo '
		<div id="post" class="error">
			<p>
				提交失败，请不要重复提交！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
			</p>		
		</div>	
		';
	}elseif( $config["FORM_MAX"]>0 && $config["FORM_MAX"] <= $VICACHE["mod.forms"]["stat"] ){
		echo '
		<div id="post" class="error">
			<p>
				提交失败，已超过人数上限！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
			</p>		
		</div>	
		';
	}elseif( $VICACHE["mod.forms"]["start"]>time() || !$VICACHE["mod.forms"]["status"] ){
		echo '
		<div id="post" class="error">
			<p>
				提交失败，表单还未启用！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
			</p>		
		</div>	
		';
		
	}elseif( $VICACHE["mod.forms"]["expire"]<time() ){
		echo '
		<div id="post" class="error">
			<p>
				提交失败，表单已经过期！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
			</p>		
		</div>	
		';
		
	}else{
		
		//mysql open
		reader_mysql();
		
		$post = true;
		
		//会话验证		
		if( $config["FORM_SPEED"] ){
			
			switch( $config["VERIFY-MODE"] ){
				
				//Cookies验证
				case "CK":
				
					$cookie = $appid."-".$fid;
					
					if( $_COOKIE[$cookie] ){
						
						echo '
						<div id="post" class="error">
							<p>
								提交失败，当前计算机已经提交过了？<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
							</p>		
						</div>	
						';
						
						$post = false;
					}				
				break;
				
				//IP提交数检查
				case "IP":
					
					$sql="SELECT id FROM ".VI_DBMODPRE."values where ip = '".GetIP()."' and dateline>".(time()-$config["FORM_SPEED"]*60)." and fid=".$fid;
							
					$result=mysql_query($sql,$mysql);
					
					if(mysql_num_rows($result)){
						
						echo '
						<div id="post" class="error">
							<p>
								提交失败，当前计算机已经提交过了！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
							</p>		
						</div>	
						';
						
						$post = false;
					}				
				break;				
			}
		}
		
		//注册用户，不允许用户名重复
		if( $post && $config['USER_MODE'] == "REG" && $config['FORM_NUMBER'] ){
			
			$sql="SELECT count(id) FROM ".VI_DBMODPRE."values where uid = '".$_SESSION["Member"]["id"]."' and fid=".$fid;
					
			if( $db -> getValue($sql) >= $config['FORM_NUMBER'] ){
				
				echo '
				<div id="post" class="error">
					<p>
						提交失败，每位用户只能参加 '.$config['FORM_NUMBER'].' 次！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
					</p>		
				</div>	
				';
				
				$post = false;
			}
			
		}
		
		if( $post ){
		
			//处理表单
			$arr = array();
			
			//收集标记
			$res = false;
			
			//收集错误
			$err = 0;
			
			foreach( $VICACHE["mod.forms"]["groups"] as $gid => $group ){
				
				$item = '';				
				$temp = 'G-'.$gid;
				$config = json_decode($group["config"],true);		
				
				$value = '';
				
				//类型换算
				switch($group["type"]){
						
					case 'checkbox':
						$array = $_POST[$temp];
						
						for($n=0;$n<count($array);$n++){
							$value .= $array[$n].',';
						}
						
						$value=substr($value,0,strlen($value)-1);
						
					break;
						
					default:
						$value = $_POST[$temp];
					break;
				}
				
				$arr[$temp] = $value;
				
				//更新统计
				if ( $value ){
					$sql="update ".VI_DBMODPRE."options set stat=stat+1 where id in(".$value.")";
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					$res = true;
				}
				
				//判断对错			
				$try = false;
				
				if( $value && $group["selected"] ){
					switch( $group["type"] ){
					
						case "radio":
						case "select":
							if( $value == $group["selected"] ){
								$try = true;	
							}
						break;
					
						case "checkbox":
							if( count(array_diff( explode(",",$group["selected"]) , $array )) == 0 ){
								$try = true;
							}					
						break;
					}
					
				}elseif( $value && !$group["selected"] ){
					$try = true;
				}
				
				if( $try ){
					$sql="update ".VI_DBMODPRE."groups set stat=stat+1 where id = ".$gid;
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
				}else{
					$err++;	
				}
				
			}
			
			//已接收数据
			if( $res ){
				
				//更新邀请统计
				if ( $hash["oid"] ){
					
					$sql="update ".VI_DBMODPRE."options set stat=stat+1 where id = ".$hash["oid"];
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
				
					//更新缓存
					cache_forms($hash["fid"],$hash["appid"],$mysql);				
					
				}
				
				//是否测试成功
				$arr["RESULT"] = ( $err ? "N" : "Y" );
				
				//使用匿名扩展模式
				//ACCOUNT	姓名	PHONE	电话号码	IDCARD	身份证	EMAIL	电子邮箱
				// !$_SESSION["Member"]["id"] &&
				if( $config['USER_MODE'] == "ANY" ){
					$arr["ACCOUNT"]	= $_POST["ACCOUNT"];
					$arr["NAME"]	= $_POST["NAME"];
					$arr["PHONE"] 	= $_POST["PHONE"];
					$arr["IDCARD"] 	= $_POST["IDCARD"];
				}

				//记录用户提交数据	
				$config = format_json( fix_json($arr) );
				
				$sql="insert into ".VI_DBMODPRE."values(appid,fid,uid,account,config,status,dateline,ip) values('".$VICACHE["mod.forms"]["appid"]."',".$fid.",'".$_SESSION["Member"]["id"]."','".$_SESSION["Member"]["name"]."','".$config."',0 ,".time().",'".GetIP()."')";
				mysql_query($sql,$mysql);
				
				if( $_SESSION["Member"]["id"] ){
				
					//更新活跃统计
					$sql="update ".VI_DBMODPRE."members set stat_level=stat_level+1 where id=".$_SESSION["Member"]["id"];
					mysql_query($sql);
				
				}
				
				//测试结果统计
				$config["RESULT"] = ( is_numeric($config["RESULT"]) ? $config["RESULT"] : 0 );
				$config["RESULT"] = ( $err ? $config["RESULT"] : $config["RESULT"]+=1 );
				$config = format_json( fix_json($config) );
				
				//更新人数统计
				$sql="update ".VI_DBMODPRE."forms set stat=stat+1,config='".$config."' where id = ".$fid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]+3;
				
				//更新缓存
				cache_forms($fid,$VICACHE["mod.forms"]["appid"],$mysql);				
				
				//记录验证码
				$_SESSION["__VERIFY"] = $verify;
				
				//写入Cookies
				$expire=time() + (60*$config["FORM_SPEED"]);
				setcookie($cookie, 'Y', $expire ,"" ,"");
				
				if( $err ){
					
					echo '
					<div id="post" class="error">
						<p>
							哦哦~~你有 '.$err.' 个位置没有答对哦！<a href="?action=result&id='.$fid.'">查看结果</a>
						</p>		
					</div>	
					';			
					
				}else{
					
					echo '
					<div id="post">
						<p>
							恭喜你啦，全部答对！<a href="?action=result&id='.$fid.'">查看结果</a>
						</p>		
					</div>	
					';			
					
				}
				
			
			}else{
				
				echo '
				<div id="post" class="error">
					<p>
						提交失败，没有收到数据！<a href="?id='.$fid.'" onclick="javascript:history.back(); return false;">返回操作</a>
					</p>		
				</div>	
				';			
			
			}
			
		}
	  
		//mysql close
		reader_mysql(true);		
		
		if( $res && $VICACHE["mod.forms"]["quote"] && $config["PAGE_REDIRECT"] ){
			switch($config["PAGE_REDIRECT"]){
				//新窗口
				case "A":
					echo '<script type="text/javascript">window.setTimeout(function(){window.open("'.$VICACHE["mod.forms"]["quote"].'")},2000)</script>';
				break;
				
				//本窗口
				case "B":
					echo '<script type="text/javascript">window.setTimeout(function(){top.location.href="'.$VICACHE["mod.forms"]["quote"].'";},2000)</script>';
				break;
			}	
		}
		
	}
?>