<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

require '../../include/loader.php';
html_start("试题选项 - VeryIDE");
?>

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

	$appid = Module :: get_appid();

	//module
	require 'config.php';
	//require 'function.php';
	//require 'naver.php';	
	
	//连接数据库
	System :: connect();
	
	$do = getgpc('do');

	$action = getgpc('action');
	$jump = getgpc('jump');
	
	$fid = getnum('fid',0);
	$gid = getnum('gid',0);
	
	if ($action){

		$name = getgpc('name');
		$sort = getnum("sort",0);
		$oid = getnum('id',0);
		$description = getgpc("description");

		switch($action){
			
			case "add":
			
				//检查权限
				System :: check_func( 'mod-test-add', false );
			
				//批量插入
				foreach($_POST["_name"] as $key => $val){
					
					//不为空
					if( $val ){
					
						$sql="INSERT INTO `mod:option`(name,sort,fid,gid,dateline,stat,state) values('".FilterHtml($val)."',".intval($_POST["_sort"][$key],0).",".$fid.",".$gid.",".time().",0,1)";
						
						System :: $db -> execute( $sql );
						
					}
				}
				
				System :: redirect("?do=list&fid=".$fid."&gid=".$gid,"信息提交成功!");
				
			break;
				
			case "edit":
			
				$sql="SELECT * FROM `mod:option` WHERE id=".$oid;
				$row = System :: $db -> getOne( $sql );
			
			break;
				
			case "update":
			
				//检查权限
				System :: check_func( 'mod-test-mod', false );
			
				$sql="UPDATE `mod:option` SET name='".$name."',sort=".$sort.",state=1 WHERE id=".$oid;
				
				System :: $db -> execute( $sql );
				
			break;
				
			case "delete":
			
				//检查权限
				System :: check_func( 'mod-test-del', false );
			
				$sql="DELETE FROM `mod:option` WHERE id=".$oid;
				
				System :: $db -> execute( $sql );
				
			break;
				
			case "mass":
			
				//检查权限
				System :: check_func( 'mod-test-mod', false );
			
				//更新正确值
				$selected=$_POST["selected"];
				
				$sql="UPDATE `mod:group` SET selected='".implode(",",$selected)."' WHERE id=".$gid;
				
				System :: $db -> execute( $sql );
				
				//更新标签信息
				$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 `mod:option` SET name='".$name."',sort=".$sort.",state=1 WHERE id=".$id." and gid=".$gid;
						
						System :: $db -> execute( $sql );
						
						//echo $sql.'<br />';
					}
				}
				
				//System :: redirect("?do=list&fid=".$fid."&gid=".$gid,"信息批量修改成功!");
				
			break;
							
		}        
		
		if( $action != "edit" ){
			
			//生成缓存
			Cached :: form( $appid, $fid );
			
			System :: redirect("?do=list&fid=".$fid."&gid=".$gid,"子选项更新成功!");
		}
  	}	
	
	//加载缓存
	Cached :: loader( $appid, 'form.'.$fid );
	?>
<!--main-->

	<?php
	
	echo '
    <div class="item">
        <strong>'.$_CACHE[$appid]['form']["name"].' &gt;&gt; '.$_CACHE[$appid]['group'][$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>
	';
	?>
        
    <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 $_G['module']['test']["group"][$_CACHE[$appid]['group'][$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 digi" value="<?php echo $val;?>" size="20" fix_name="标签排序" fix_number="no" onclick="Mo.Soler( this, event, this.value, 'mo-soler', { '标签排序' : (function(){ var a = []; for(var i=-9;i<=60;i++){a.push(i);} return a; })() }, function( value ){ this.value = value; }, 1 , 23 );" readonly="true" />
                            </td>
                        </tr>
                    </table>
                    
                    </div>
                
                    <script type="text/javascript">
                    var Planer = new Mo.Planer(Mo.$("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 digi" value="<?php echo $row["sort"];?>" size="35" fix_name="标签排序" fix_number="no" onclick="Mo.Soler( this, event, this.value, 'mo-soler', { '标签排序' : (function(){ var a = []; for(var i=-9;i<=60;i++){a.push(i);} return a; })() }, function( value ){ this.value = value; }, 1 , 23 );" readonly="true">
                </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="70">操作</td>
        </tr>
        <?php
            $sql="SELECT * FROM `mod:option` WHERE gid=".$gid." order BY state desc,sort asc";
            
            $result = System :: $db -> getAll( $sql );
			
			//批量ID
			$all = array();

            foreach( $result as $row ){
				
				//记录批量ID
				array_push($all,$row['id']);
                
                switch($_CACHE[$appid]['group'][$gid]["type"]){
                
                    case 'radio':						
                        $input = '<input name="selected[]" type="radio" class="radio" value="'.$row['id'].'" '.($_CACHE[$appid]['group'][$gid]["selected"] == $row['id']?'checked':'').' />';
                    break;
                
                    case 'select':						
                        $input = '<input name="selected[]" type="radio" class="radio" value="'.$row['id'].'" '.($_CACHE[$appid]['group'][$gid]["selected"] == $row['id']?'checked':'').' />';
                    break;
                
                    case 'checkbox':						
                        $input = '<input name="selected[]" type="checkbox" class="checkbox" value="'.$row['id'].'" '.( in_array($row['id'],explode(",",$_CACHE[$appid]['group'][$gid]["selected"])) ?'checked':'').' />';
                    break;
                }
                ?>
                
                <tr class="<?php echo zebra( $i, array( "line" , "band" ) );?>" 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($_G['setting']['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 
	}
	
        //关闭数据库
        System :: connect();
	?>
    
    </div>
    
<!--body end-->

<?php html_close();?>