<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: ,v1 10:39 2008-7-27 Leilei $
*/

require("../../include/header.php");
html_start("会员列表 - VeryIDE");
?>

<!--body start-->
	<?php

	//module
	require("config.php");

	$q = GetQuery("q");
	$s = GetQuery("s");
	$base = $_GET["b"];
	
	$list = $_POST["list"];
	$list = implode(",", $list);
	
	$fid = GetNumber("fid",0);
	$appid = GetQuery("appid");
	$action = GetQuery("action");
	$status = GetNumber("status",1);
	
	//状态筛选
	$filter = GetNumber("filter",-1);
	
	//分类模式
	$model = GetNumber("model",-1);
	
	//日期筛选
	$start = GetQuery("start");
	$end = GetQuery("end");

	//时间
	if ($start && $end){
	
		//开始时间
		$e_array=explode("-",$start);
		$time1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
		
		//到期时间
		$e_array=explode("-",$end);
		$time2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
		
		$ect=" and  dateline >=".$time1." and dateline <= ".$time2;
	}
	
	//加载缓存
	loader_cache($appid,"forms.".$fid);
	
	$groups = $VICACHE["mod.forms"]["groups"];
	
	$options = $VICACHE["mod.forms"]["options"];
	
	$config = fix_json($VICACHE["mod.forms"]["config"]);
	
	//mysql open
	reader_mysql();	

	//echo $VICACHE["mod.forms"]["appid"];
	//echo '<br />';

	if ($action){
	
		switch($action){
			//删除
			case "delete":
			
				//检查权限
				checker_func("mod-member-data-del");
				
				//更新选项组及选项统计
				$sql="SELECT config FROM ".VI_DBMODPRE."values where fid=".$fid." and id in(".$list.")";
				
				$result=mysql_query($sql,$mysql);
				
				while($row=mysql_fetch_assoc($result)){
					
					$config = fix_json($row["config"]);
					
					switch($VICACHE["mod.forms"]["appid"]){
						
						//表单
						case "form":
					
							//表单数据
							foreach( $groups as $gid => $array ){
							
								$value = $config['G-'.$gid];
								
								if( $value && in_array($groups[$gid]["type"],array("text","textarea","file")) ){
								
									$sql="update ".VI_DBMODPRE."groups set stat=stat-1 where stat>0 and id = ".$gid;
									mysql_query($sql,$mysql);
								
								}elseif( $value ){
									
									$sql="update ".VI_DBMODPRE."options set stat=stat-1 where stat>0 and id in(".$value.")";
									mysql_query($sql,$mysql);
								
								}
							}
							
						break;
						
						//表单
						case "pk":
						
							$OBJECT = $config["OBJECT"];
							
							if( $OBJECT && $config[$OBJECT."-RESULT"] ){
							
								$config[$OBJECT."-RESULT"] = $config[$OBJECT."-RESULT"] - 1;
								
								$config = json_encode($config);
								
								//更新人数统计
								$sql="update ".VI_DBMODPRE."forms set config='".$config."' where id = ".$fid;
								mysql_query($sql,$mysql);
							
							}
							
						break;
						
						//试题
						case "test":
						
							if( $config["RESULT"] == "Y" ){
							
								$config["RESULT"] = $config["RESULT"] - 1;
								
								$config = json_encode($config);
								
								//更新人数统计
								$sql="update ".VI_DBMODPRE."forms set config='".$config."' where id = ".$fid;
								mysql_query($sql,$mysql);
							
							}
					
							//表单数据
							foreach( $groups as $gid => $array ){
							
								$value = $config['G-'.$gid];
								
								if( $value && in_array($array["type"],array("radio","select","checkbox")) ){
								
									$sql="update ".VI_DBMODPRE."options set stat=stat-1 where stat >0 and id in(".$value.")";
									mysql_query($sql,$mysql);
								
								}
								
								//判断对错			
								$try = false;
								
								if( $value && $array["selected"] ){
									switch( $array["type"] ){
									
										case "radio":
										case "select":
											if( $value == $array["selected"] ){
												$try = true;	
											}
										break;
									
										case "checkbox":
											if( count(array_diff( explode(",",$array["selected"]) , explode(",",$value) )) == 0 ){
												$try = true;
											}					
										break;
									}
									
								}elseif( $value && !$array["selected"] ){
									$try = true;
								}
								
								if( $try ){
									$sql="update ".VI_DBMODPRE."groups set stat=stat-1 where stat > 0 and id = ".$gid;
									mysql_query($sql,$mysql);
								}
								
							}
							
							
						break;
						
						//投票
						case "poll":
						case "vote":
						
							//echo $VICACHE["mod.forms"]["appid"];
							//echo '<br />';
					
							//表单数据
							foreach( $groups as $gid => $array ){
							
								$value = $config['G-'.$gid];
								
								//echo $value;
								//echo '<br />';
								
								if( $value && in_array($groups[$gid]["type"],array("radio","checkbox","button")) ){
								
									$sql="update ".VI_DBMODPRE."options set stat=stat-1 where stat >0 and id in(".$value.")";
									mysql_query($sql,$mysql);									
									
									//echo $sql;
									//echo '<br />';
								
								}
							}
							
						break;
					
					}
					
				}
			
				//删除提交数据
				mysql_query("delete from ".VI_DBMODPRE."values where id in(".$list.")");
				
				//echo "delete from ".VI_DBMODPRE."values where id in(".$list.")";
				
				//echo '<br />';
				
				$result = mysql_affected_rows($mysql);
				
				//echo $result;
				//echo '<br />';
				
				//更新表单统计
				mysql_query("update ".VI_DBMODPRE."forms set stat=stat-".$result." where stat > 0 and id = ".$fid);				

				//echo "update ".VI_DBMODPRE."forms set stat=stat-".$result." where stat > 0 and id = ".$fid;
				//echo '<br />';				
								
				//更新缓存
				cache_forms($fid,$VICACHE["mod.forms"]["appid"],$mysql);
				
				//exit();
				
				//上页地址
				$url = GetQuery("url");
				
				reader_redirect( $url ? $url : "member.value.php?appid=".$VICACHE["mod.forms"]["appid"]."&fid=".$fid,"用户数据删除成功!");
				
			break;			
			
			case "status":
			
				//检查权限
				checker_func("mod-".$appid."-mod");	
				
				$status = GetNumber("status",0);
			
				$sql="update ".VI_DBMODPRE."values set status=".$status." where id in(".$list.")";
				mysql_query($sql);
				
				$Project["queries"]++;
				
				//reader_redirect($jump,"表单状态更改成功!");
				reader_redirect("member.value.php?appid=".$appid."&fid=".$fid,"用户数据更新成功!");

			break;
		}
	}
	
	require("../$appid/config.php");
	require("../$appid/function.php");
			
    ?>
    
    <div id="nav">
        <strong><?php echo $VICACHE["mod.forms"]["name"];?></strong> 用户列表
    </div>

	<?php    
    echo loader_script(array(VI_BASE."js/calendar.js"),"utf-8",$Project["version"]);    
    ?>
        
    <div id="search">
    
    <form name="formSearch" id="formSearch" method="get" action="?">
    
    	<span class="action">
			<button class="button" onclick="Mo('#formAdv').toggle(function(s){if(!s){setAll('#formAdv input',false); $('formSearch').submit();}});" type="button">高级选项</button>
            <?php 
			if( $Group["mod-member-data-dow"] == "Y" ){
			?>
			<button class="cancel" type="button" onclick="location.href=location.href.replace('value.php','report.php');">下载数据</button>
            <?php
			if( function_exists("mcrypt_module_open") ){
				
				//这里很奇怪，只支持长度为4的数组，所以要注意
				$hash = array("appid"=>$appid,"fid"=>$fid,"name"=>$VICACHE["mod.forms"]["name"],"expire"=>time());
				
				$keys = do_mencrypt(serialize($hash),VI_START);
			?>
				<button class="cancel" type="button" onclick="window.open('member.share.php?keys=<?php echo rawurlencode($keys);?>');">分享数据</button>
			<?php
            }else{?>
				<button class="cancel" type="button" title="本功能需要服务器开启 mcrypt 模块" onclick="alert(this.title);">分享数据</button>
            <?php
            
            }
            ?>
            <?php
			}
			?>
            <button class="cancel" type="button" onclick="new parent.Mo.Dialog( 'dialog','表单选项组', '<iframe src=module/member/api.php?appid=<?php echo $appid;?>&fid=<?php echo $fid;?> frameborder=0></iframe>' ,'80%','60%',{ 'remove' : true, 'draged' : true, 'create' : true }); return false;">数据接口</button>
        </span>
    
        <select name="s" id="s">
            <option value="id" checked> I D</option>
            <option value="account"> 用户名</option>
			<option value="ip"> IP</option>
			<option value="config"> 数据</option>			
        </select>
        
        <input name="q" type="text" class="text" id="q" value="<?php echo $q;?>" size="15" />
        <input name="fid" value="<?php echo $fid;?>" type="hidden" />
        <input name="appid" value="<?php echo $appid;?>" type="hidden" />
        
        <!--时间节点-->
        <input name="start" type="text" class="date" id="start" value="<?php echo $start;?>" size="10" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日" onChange="Mo('#formSeacrh').submit();">
        -
        <input name="end" type="text" class="date" id="end" value="<?php echo $end;?>" size="10" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日" onChange="Mo('#formSeacrh').submit();">
        <!--时间节点-->
        
        <select id="filter" name="filter">
            <option value="-1">所有状态</option>
            <option value="0">未确认</option>
            <option value="1">已确认</option>
        </select>
        
        <select id="model" name="model">
            <option value="-1">分页显示</option>
            <option value="0">系统默认</option>
            <option value="1">全部显示</option>
        </select>
       
        <button class="go" type="submit"></button>
        
        <div id="formAdv" class="advanced" style="display:none;">        
			<?php
            foreach ($Modules["member"]["fieds"] as $fied => $name){
                echo '<label><input name="b['.$fied.']" value="1" type="checkbox" class="checkbox" '.($base[$fied]?' checked="checked"':'').' onchange="$(\'formSearch\').submit();" onclick="$(\'formSearch\').submit();" ignore="true" /> '.$name.'</label>';
            }
            ?>        
        </div>
       
    </form>
    </div>
    
    <script type="text/javascript">
        Mo("#s").value("<?php echo $s;?>");
        Mo("#filter").value("<?php echo $filter;?>");
        Mo("#model").value("<?php echo $model;?>");
		
		<?php
		if( is_array($base) ){
			echo "Mo('#formAdv').toggle();";
		}
		?>
    </script>

    <?php
	
    $sql="SELECT * FROM ".VI_DBMODPRE."values where fid=".$fid." ";
	
    if ( $q && $s ){
        $sql.=" and  ".$s." like '%".$q."%'";
    }
	
    if ( $filter >- 1){
        $sql.=" and status = ".$filter;
    }
	
	if ($ect) $sql.= $ect;
    
    //排序
    $sql.=" order BY id desc";
    
    //查询数据库_总记录数
    $row_count = $db -> getValue( str_replace("*","count(id)",$sql) );   

    
    $Project["queries"]++;
	
	if( $model<1 ){

		//分页参数
		$page=GetPage("page");
		$page_start=$Global["pagesize"]*($page-1);
		$sql=$sql." limit $page_start,".$Global["pagesize"];
	}
    
    //分页链接
    $url="?q=".urlencode($q)."&s=".$s."&appid=".$VICACHE["mod.forms"]["appid"]."&fid=".$fid."&".format_get("b",$base)."&filter=$filter&model=$model&page=";
    
    $result=mysql_query($sql,$mysql);
	
	//echo $sql;
	
    $Project["queries"]++;
    ?>
    
	<?php
    
    //表格统计
    $x = 0;
    
    //表单数据
    foreach( $VICACHE["mod.forms"]["groups"] as $id => $array ){
        $thead .= '<td title="'.$id.'">'.$array["name"].'</td>';
        $x++;
    }
    
    //用户数据
    if( is_array($base) ){
        foreach ($Modules["member"]["fieds"] as $fied => $name){
            if( $base[$fied] ){
				$thead .= '<td title="'.$fied.'">'.$name.'</td>';
				$x++;	
			}
            
        }
    }
    
    //数据扩展
    foreach( $Modules[$appid]["value"] as $fied => $name ){
        $thead .= '<td title="'.$fied.'">'.$name.'</td>';
        $x++;
    }
	    
    ?>
	
	<!--用于动作处理_开始-->
	<form name="formPost" id="formPost" method="post" action="?">
		<input name="action" id="action" type="hidden" value="" />
		<input name="status" id="status" type="hidden" value="" />
		<input name="fid" value="<?php echo $fid;?>" type="hidden" />
		<input name="appid" value="<?php echo $appid;?>" type="hidden" />
		
		<input name="jump" type="hidden" value="" />
		<script>Mo('#formPost input[name=jump]').value( location.href );</script>
    
    <table width="100%" border="0" cellpadding="1" cellspacing="1" id="table" class="table">
		<?php
		if( mysql_num_rows($result)> $Global["pagesize"]/2 ){
		?>
        <tr class="thead">
            <td colspan="<?php echo ($x+7);?>" class="first"><?php echo $Modules["member"]["tool"];?></td>
        </tr>
      	<?php
		}
		?>
      
        <tr class="title" id="thead">
            <td width="30"></td>
            <td>ID</td>
            <td>用户名</td>
            <td>详细</td>
            <?php echo $thead;?>
            <td width="60">状态</td>
            <td width="110">IP 地址</td>
            <td width="120">提交时间</td>
        </tr>
      
    <?php
    //VeryIDE 绝对路径
    $uripath=uripath();
    
    while($row=mysql_fetch_assoc($result)){
		
		//删除空白字符
		$row["config"] = preg_replace("/([\r\n\t\f])/"," ",$row["config"]);
		
        $config = fix_json( $row["config"] );	
		
		//读取用户信息
		$user = array();
		
		if( is_array($base) && $row["uid"] ){
			
			$sql="select * from ".VI_DBMODPRE."members where id='".$row["uid"]."'";
			
			$db -> query($sql);
			$user = $db -> fetchRow();
		}
		
		//var_dump($config);
		//exit();
        
        ?>
          <tr class="line" mark="<?php echo $row["id"];?>">
            <td title="<?php echo $row["id"];?>">
                <input name="list[]" type="checkbox" class="checkbox" value="<?php echo $row["id"];?>">
            </td>
            <td>
                <?php echo $row["id"];?>
            </td>
            <td title='<?php echo $row["name"];?>'>
            	<?php echo $row["status"]?loader_image("icon/online.png","已确认"):loader_image("icon/offline.png","未确认");?>
                <a href="?fid=<?php echo $fid;?>&appid=<?php echo $appid;?>&s=account&q=<?php echo $row["account"];?>"><?php echo format_substr($row["account"],30,"...");?></a>
            </td>
            <td>
                <?php
				if( $row["uid"] ){
				?>
                <a href="<?php echo "member.profile.php?uid=".$row["uid"];?>" title="<?php echo $row["uid"];?>" target="_blank" onclick="parent.Mo.Window('dialog','用户个人信息' ,this.href ,'850','80%',{}); return false;">查看</a>
                <?php
				}
				?>
            </td>
            <?php
			
			//表单数据
			foreach( $groups as $gid => $array ){
				
				//echo '<td>'.'G-'.$gid." : ".$config['G-'.$gid].'</td>';
				
				if( in_array($groups[$gid]["type"],array("text","textarea","file")) ){
					
					$value = $config['G-'.$gid];
					
					if( $groups[$gid]["type"] == "file" ){
						echo '<td><a href="'.$value.'" target="_blank">'.$value.'</a></td>';
					}else{
						echo '<td>'.$value.'</td>';
					}
				
				}else{
					
					$array = explode(",", $config['G-'.$gid]);
					
					$value = "";
					
					$n = 1;
					foreach( $array as $oid ){					
						$value .= $options[$oid]["name"].($n!=count($array)?'、 ':'');
						$n++;
					}
					
					echo '<td>'.$value.'</td>';
				
				}	
				
			}
			
			//用户数据
			foreach ($Modules["member"]["fieds"] as $fied => $name){
				
				//转换数据					
				switch($fied){
				
					case 'sex':						
						$val = $Project["sex"][$user[$fied]];
					break;
				
					case 'marriage':						
						$val = $Project["marriage"][$user[$fied]];
					break;
				
					case 'dateline':						
						$val = date("Y/m/d H:i:s",$user[$fied]);
					break;
				
					//没有从字段中取得信息就从内容中获取
					case 'account':						
						$val = $user[$fied] ? $user[$fied] : $config['ACCOUNT'] ;
					break;
				
					case 'name':						
						$val = $user[$fied] ? $user[$fied] : $config['NAME'] ;
					break;
				
					case 'email':						
						$val = $user[$fied] ? $user[$fied] : $config['EMAIL'] ;
					break;

					case 'phone':						
						$val = $user[$fied] ? $user[$fied] : $config['PHONE'] ;
					break;
					
					case 'idcard':						
						$val = $user[$fied] ? $user[$fied] : $config['IDCARD'] ;
					break;
				
					default:						
						$val = $user[$fied];
					break;
				
				}
				
				if( $base[$fied] )	echo '<td> '.$val.' </td>';
			}
			
			//var_dump($config);
			
			//数据扩展
			foreach( $Modules[$appid]["value"] as $fied => $name ){
				echo '<td>'.parseValue($fied,$row,$config).'</td>';
			}
					
			?>
            <td>
				<?php echo $Modules[$appid]["sign"][$row["status"]];?>
            </td>
            <td>
                <?php
                echo '<span class="text-no">'.$row["ip"].'</span>';
                ?>
            </td>
            <td title="<?php echo date("Y-m-d H:i:s",$row["dateline"]);?>">
                <?php
                echo '<span class="text-yes">'.date("y-m-d H:i:s",$row["dateline"]).'</span>';
                ?>
            </td>
            </tr>
        <?php
        }
        ?>
        
            <tr class="tfoot">
                <td colspan="<?php echo ($x+4);?>" class="first">
                    <?php echo $Modules["member"]["tool"];?>
                </td>
                <td colspan="3">
                    <?php
                    echo loader_image("icon/online.png");
                    ?>
                    已确认
                    
                    <?php
                    echo loader_image("icon/offline.png");
                    ?>
                    未确认
                    
                    <?php
                    echo loader_image("icon/user.png");
                    ?>
                    用户信息
                    
                </td>
            </tr>
        </table>
    <?php
  
    //mysql close
    reader_mysql(true);
    
    ?>
    
	</form>
	<!--用于动作处理_结束-->
	
    <!--up-->
    
    <div id="saving">
		<?php
			if( $model===1 && $key ){
				$Global["pagesize"] = $row_count;
			}
            echo GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
        ?>
    </div>
    
<!--body end-->

<?php html_close();?>