<?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");
    //require("function.php");
    //require("naver.php");
	
	
	//mysql open
	reader_mysql();
	
	$do = GetQuery("do");

	$action = GetQuery("action");
	$jump = GetQuery("jump");
	
	$fid = GetNumber("fid",0);
	$gid = GetNumber("gid",0);
	
	if ($action){

		$name = GetQuery("name");
		$sort = GetNumber("sort",0);
		$oid = GetNumber("id",0);
		$description = GetQuery("description");

		switch($action){
			
			case "add":
		
				//批量插入
				foreach($_POST["_name"] as $key => $val){
					
					//不为空
					if( $val ){
					
						$sql="insert into ".VI_DBMODPRE."options(name,sort,fid,gid,dateline,stat) values('".FilterHtml($val)."',".intval($_POST["_sort"][$key],0).",".$fid.",".$gid.",".time().",0)";
						
						mysql_query($sql,$mysql);
						
						$Project["queries"]++;
						
						$id = mysql_insert_id($mysql);
						
						//错误处理
						if (!$id) {
							trigger_error($sql, E_USER_ERROR);
							exit();
						}
						
					}
				}
				
				reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息提交成功!");
				
			break;
				
			case "edit":
			
				$sql="select * from ".VI_DBMODPRE."options where id=".$oid;
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				if(mysql_num_rows($result)==1){
					$row=mysql_fetch_assoc($result);				
				}
			
			break;
				
			case "update":
				$sql="update ".VI_DBMODPRE."options set name='".$name."',sort=".$sort." where id=".$oid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
			break;
				
			case "delete":
				$sql="delete from ".VI_DBMODPRE."options where id=".$oid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
			break;
				
			case "mass":
			
				//检查权限
				checker_func("mod-form-mod");
			
				//更新正确值
				$selected=$_POST["selected"];
				
				$sql="update ".VI_DBMODPRE."groups set selected='".implode(",",$selected)."' where id=".$gid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				//更新标签信息
				$arr	= explode(",",$_POST["all"]);
				
				for( $i=0; $i<count($arr); $i++ ){
				
					$id = intval($arr[$i],0);
					$name = $_POST["name-".$id];
					
					if( $id>0 && $name ){
					
						$sort=$_POST["sort-".$id];
						
						$sql="update ".VI_DBMODPRE."options set name='".$name."',sort=".$sort." where id=".$id." and gid=".$gid;
						mysql_query($sql,$mysql);
						
						$Project["queries"]++;
						
						//echo $sql.'<br />';
					}
				}
				
				//reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息批量修改成功!");
				
			break;
							
		}        
		
		if( $action != "edit" ){
			
			//生成缓存
			cache_forms($fid,$appid,$mysql);
			
			reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"子选项更新成功!");
		}
  	}	
	
	//加载缓存
	loader_cache($appid,"forms.".$fid);
	?>
<!--main-->

	<?php
	
	echo '
    <div id="nav">
        <strong>'.$VICACHE["mod.forms"]["name"].' &gt;&gt; '.$VICACHE["mod.forms"]["groups"][$gid]["name"].'</strong> 	
    </div>
    
    <ul id="naver">
        <li'.($do=='new'?' class="active"':'').'><a href="?do=new&fid='.$fid.'&gid='.$gid.'">'.($action=="edit"?'编辑':'新增').'子选项</a></li>
        <li'.($do=='list'?' class="active"':'').'><a href="?do=list&fid='.$fid.'&gid='.$gid.'">管理子选项</a></li>
    </ul>
	';
	?>
    
	<?php    
    if ( in_array($VICACHE["mod.forms"]["groups"][$gid]["type"],array("text","textarea","file")) ){
    	echo '
			<div id="board">
			<p>
				<strong>标签说明：</strong>
				此类型项目不需要子类标签！
			</p>
			</div>
		   ';
		   
		   $do = '';
	}
    ?>
    
    <div id="box">
    
    <?php 
	if($do=="new"){
	?>
    
    	<form action="?fid=<?php echo $fid;?>&gid=<?php echo $gid;?>" method="post" verify="true">

        <table cellpadding="0" cellspacing="0" class="form">
        
        	<tr>
            	<td width="60"><strong>目标类型：</strong></td>
                <td>
                <?php
				echo $Modules[$appid]["groups"][$VICACHE["mod.forms"]["groups"][$gid]["type"]];
				?>
                </td>
            </tr>
            
            <?php
			//编辑状态
			if( $action == "" ){			
			?>
        
        	<tr>
            	<td><strong>批量创建：</strong></td>
                <td>

                	<div id="tag-box">
                    
                    <table id="tag-<?php echo $i;?>" cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td>
                            	<span class="action"><a onclick="javascript:Planer.remove(this);void(0);">删除</a></span>
                           		名称：
                                <br />
                                <input name="_name[]" type="text" class="text" value="<?php echo $key;?>" size="20" fix_name="标签名称" fix_null="yes" />
                                <br />
                                排序：<br />
                                <input name="_sort[]" type="text" class="text" value="<?php echo $val;?>" size="20" fix_name="标签排序" fix_number="no" />
                            </td>
                        </tr>
                    </table>
                    
                    </div>
                
                    <script type="text/javascript">
                    var Planer = new Mo.Planer($("tag-box"));
                    </script>
                    
                    <a onclick="javascript:Planer.copy();void(0);">添加</a>
                
                </td>
            </tr>
            
            <?php
			}else{			
			?>
        
        	<tr>
            	<td><strong>标签名称：</strong></td>
                <td><input name="name" type="text" class="text" id="name" value="<?php echo $row["name"];?>" size="35" fix_name="标签名称" fix_null="yes" />
                </td>
            </tr>
        
        	<tr>
            	<td><strong>标签排序：</strong></td>
                <td><input name="sort" type="text" class="text" value="<?php echo $row["sort"];?>" size="35" fix_name="标签排序" fix_number="no">
                </td>
            </tr>
            
            <?php
			}			
			?>
        
        	<tr class="nobr">
            	<td><strong></strong></td>
                <td>
					<?php
                    if ($action=="edit"){
						 echo '
						<input name="action" type="hidden" id="action" value="update" />
						<input name="id" type="hidden" id="id" value="'.$row["id"].'" />
						';
                       echo '<input type="submit" name="Submit" value="修改项目" class="submit" />';
                    }else{
						echo '
						<input name="action" type="hidden" id="action" value="add" />
						';
                        echo '<input type="submit" name="Submit" value="新增项目" class="submit" />';
                    }
                    ?>
                </td>
            </tr>
        
        </table>
        
	 </form>
	
    <?php
	}elseif($do=="list"){
	?>
    
    <form name="formPost" id="formPost" method="post" action="?fid=<?php echo $fid;?>&gid=<?php echo $gid;?>">

    <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table" id="table">
        <tr class="title">
        <td></td>
        <td>ID</td>
        <td>排序</td>
        <td>标签名称</td>
        <td>统计</td>
        <td>添加时间</td>
        <td width="60">操作</td>
        </tr>
        <?php
            $sql="SELECT * FROM ".VI_DBMODPRE."options where gid=".$gid." order BY sort";
            
            $result=mysql_query($sql,$mysql);
            
            $Project["queries"]++;
			
			//批量ID
			$all = array();

            while($row=mysql_fetch_assoc($result)){
                //'.$VICACHE["mod.forms"]["groups"][$gid]["selected"].'
				
				//记录批量ID
				array_push($all,$row["id"]);
                
                switch($VICACHE["mod.forms"]["groups"][$gid]["type"]){
                
                    case 'radio':						
                        $input = '<input name="selected[]" type="radio" class="radio" value="'.$row["id"].'" '.($VICACHE["mod.forms"]["groups"][$gid]["selected"] == $row["id"]?'checked':'').' />';
                    break;
                
                    case 'select':						
                        $input = '<input name="selected[]" type="radio" class="radio" value="'.$row["id"].'" '.($VICACHE["mod.forms"]["groups"][$gid]["selected"] == $row["id"]?'checked':'').' />';
                    break;
                
                    case 'checkbox':						
                        $input = '<input name="selected[]" type="checkbox" class="checkbox" value="'.$row["id"].'" '.( in_array($row["id"],explode(",",$VICACHE["mod.forms"]["groups"][$gid]["selected"])) ?'checked':'').' />';
                    break;
                }
                ?>
                
                <tr class="line" mark="<?php echo $row["id"];?>" edit="<?php echo $appid;?>.option.php?do=new&action=edit&fid=<?php echo $fid;?>&gid=<?php echo $gid;?>&id=<?php echo $row["id"];?>&jump={jump}">
                    <td width="30">
                        <?php echo $input;?>
                    </td>
                    <td><?php echo $row["id"];?></td>
                    <td><input type="text" class="text" name="sort-<?php echo $row["id"];?>" size="4" value="<?php echo $row["sort"];?>" /></td>
                    <td><input type="text" class="text text-yes" name="name-<?php echo $row["id"];?>" size="22" value="<?php echo $row["name"];?>" /></td>
                    <td><?php echo $row["stat"];?></td>
                    <td><?php echo date($Global["dateformat"],$row["dateline"]);?></td>
                    <td>
                        <script type="text/javascript">
                            Mo.Tools("RsEdit","?do=new&action=edit&fid=<?php echo $fid;?>&gid=<?php echo $gid;?>&id=<?php echo $row["id"];?>","");
                            Mo.Tools("RsDelete","?do=list&action=delete&fid=<?php echo $fid;?>&gid=<?php echo $gid;?>&id=<?php echo $row["id"];?>","");
                        </script>
                    </td>
                </tr>

                <?php
                
            }
        ?>
    </table>
    
    <div id="saving">
		<?php
        
        //有子选项时显示提交按钮
        if( count($all) > 0 ){		
        ?>
            <input type="hidden" name="all" value="<?php echo implode(",",$all);?>" />
            <input name="action" id="action" type="hidden" value="mass" />
            <input type="submit" class="submit" value="保存更改" />
            注意：如果没有选择任何子选项，表示任意选项都为正确答案
        <?php
        }else{
		?>
        	<input type="button" class="submit" value="新增子选项" onclick="location.href = '?do=new&fid=<?php echo $fid;?>&gid=<?php echo $gid;?>';" />
        <?php
		}
        ?>
    </div>
    
    </form>
	
    <?php 
	}
	
	//mysql close
	reader_mysql(true);
	?>
    
    </div>
    
<!--body end-->

<?php html_close();?>