<?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("function.php");
	require("config.php");
	//require("naver.php");

	//mysql open
	reader_mysql();

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

		$name = GetQuery("name");
		$jump = GetQuery("jump");
		$sort = GetNumber("sort",0);
		$status = GetNumber("status",0);
		$type = GetQuery("type");
		$description = GetQuery("description");
		
		$config = $_POST["config"];
	
		switch($action){
				
			case "add":
			
				if (!$fid || !$name){
					$Project["message"] ="表单必选项不能为空！";
					
				}else{
				
					$sql="insert into ".VI_DBMODPRE."groups(name,aid,account,status,fid,sort,type,dateline,description,config,stat) 
					values('".$name."',".$_SESSION["Manager"]["id"].",'".$_SESSION["Manager"]["account"]."',".$status.",".$fid.",".$sort.",'".$type."',".time().",'".$description."','".$config."',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."&mark=".$id,"信息提交成功!");
				}
				
			break;
				
			case "update":
			
				$sql="update ".VI_DBMODPRE."groups set name='".$name."',status=".$status.",type='".$type."',sort=".$sort.",description='".$description."',config='".$config."' where id=".$gid;
				mysql_query($sql);
				
				$Project["queries"]++;
				
				reader_redirect("?do=list&gid=".$gid."&fid=".$fid,"选项组修改成功!");
				
			break;
				
			case "edit":
						
				$sql="select *  from ".VI_DBMODPRE."groups where id=".$gid;
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				if(mysql_num_rows($result)==1){
					$row=mysql_fetch_assoc($result);
					
					//配置
        $config = fix_json( $row["config"] );
				}
				
			break;
				
			case "delete":
			
				mysql_query("delete from ".VI_DBMODPRE."groups where id=".$gid);
				mysql_query("delete from ".VI_DBMODPRE."options where gid=".$gid);
				
				$Project["queries"]+=2;
				
				reader_redirect("?do=list&fid=".$fid,"选项组删除成功!");
			
			break;
			
			case "status":

				if ($gid){
					$sql="update ".VI_DBMODPRE."groups set status=".$status." where id=".$gid;
					mysql_query($sql);
					
					$Project["queries"]++;
					
					reader_redirect($jump,"选项组修改成功!");
					exit();
				}
				
			break;
		}
                    
		//生成缓存
		cache_forms($fid,$appid,$mysql);
		
	}
	  
	//加载缓存
	//$file = VI_ROOT."".$Project["cache"]."mod.forms.".$fid.".php";
	//loader_data($file);
	loader_cache($appid,"forms.".$fid);
	?>
<!--main-->

	<?php
	
	echo '
    <div id="nav">
        <strong>'.$VICACHE["mod.forms"]["name"].'</strong> 	
    </div>
    
    <ul id="naver">
        <li'.($do=='new'?' class="active"':'').'><a href="?do=new&fid='.$fid.'">'.($action=="edit"?'编辑':'新增').'选项组</a></li>
        <li'.($do=='list'?' class="active"':'').'><a href="?do=list&fid='.$fid.'">管理选项组</a></li>
    </ul>
	';
	?>
    
    <?php 
	if($do=="new"){
	?>
    
	<?php    
    echo loader_script(array(VI_BASE."js/ubb.js"),"utf-8",$Project["version"]);    
    ?>
    <script type="text/javascript">
		function doChanged(s){
			var b = ['radio','checkbox'];
			if( Mo.Array( b ).indexOf( s ) > -1 ){                
				Mo( '#addv' ).enabled();                
			}else{                
				Mo( '#addv' ).disabled();
			}
		}
		
	</script>
    
    <div id="box">

	<form action="?" method="post" name="from1" verify="true">
        <textarea name="config" cols="50" rows="6" id="config" style="display:none;"><?php echo $row["config"];?></textarea>
		<input name="fid" type="hidden" id="fid" value="<?php echo $fid;?>" />
        
        <table cellpadding="0" cellspacing="0" class="form">
        
        	<tr>
            	<td width="60"><strong>项目标题：</strong></td>
                <td><input name="name" type="text" class="text" id="name" value="<?php echo $row["name"];?>" size="60" fix_name="项目标题" fix_null="yes" /></td>
                
            </tr>
        
        	<tr>
            	<td><strong>项目类型：</strong></td>
                <td>
                    <label> <input type="radio" class="radio" name="type" value="radio" checked onclick='doChanged(this.value)'>	单选</label>
                    <label> <input type="radio" class="radio" name="type" value="checkbox" onclick='doChanged(this.value)'>	复选</label>
                    <label> <input type="radio" class="radio" name="type" value="select" onclick='doChanged(this.value)'>	下拉选框</label>
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>项目属性：</strong></td>
                <td>
                    
                    <table cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td id="addv">
                            	换行设置<br />
                                <select id="GROUP_BREAK" name="GROUP_BREAK" onchange="cfg.setValue(this[this.selectedIndex].value,this.id,this[this.selectedIndex].value,function(S){$('config').value = S;});">
                                    <option value=0>无换行</option> 
                                    <option value=1>每隔1项换行</option> 
                                    <option value=2>每隔2项换行</option> 
                                    <option value=3>每隔3项换行</option> 
                                    <option value=4>每隔4项换行</option> 
                                    <option value=5>每隔5项换行</option> 
                                    <option value=6>每隔6项换行</option> 
                                    <option value=7>每隔7项换行</option> 
                                    <option value=8>每隔8项换行</option> 
                                    <option value=9>每隔9项换行</option> 
                                </select>
                            </td>
                            <td>
                            </td>
                        </tr>
                    </table>
	     	
                </td>
                
            </tr>
        
        	<tr>
            	<td><strong>扩展属性：</strong></td>
                <td>
                    
                    <table cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td>
                            空值验证<br />
                            <label>
                                <input type="checkbox" class="checkbox" id="GROUP_MUST" name="GROUP_MUST" value="Y" onclick="cfg.setValue(this.checked,this.id,'Y',function(S){$('config').value = S;});" />
                                必选项，不能为空
                            </label>
                            </td>
                            <td>
                            显示排序<br />
                            <input name="sort" type="text" class="text" value="<?php echo $row["sort"];?>" size="20" fix_name="项目排序" fix_number="no">
                            <script>Mo.Tools("Tip","将以从小到大的顺序进行显示");</script>
                            </td>
                        </tr>
                    </table>
                    
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>简要介绍：</strong></td>
                <td>
                <script type="text/javascript">
					Mo.UBB.show({
						id:"description",
						toolbar:["font","size","color","bold","italic","under","link","image","zoom","zoomin","zoomout","html","stat","about"]							
					});
                </script>
                <textarea name="description" cols="50" rows="5" id="description"><?php echo $row["description"];?></textarea>
                <br />
				 <script type="text/javascript">
				new Serv.Upload("group","",{'format':['<?php echo implode("','",$Upload["image"]);?>'],'again':true,'recovery':true,'input':false,'ubb':"description"});
                </script>
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>项目状态：</strong></td>
                <td>
                    <label>
                    <input name="status" type="radio" class="radio" value="1" checked> 
                    完全开放
                    </label>
                    <label>
                    <input name="status" type="radio" class="radio" value="0">
                    禁用项目
                    </label>
                </td>
                
            </tr>
        
        	<tr class="nobr">
            	<td><strong></strong></td>
                <td>
                
					<?php
                    if ($action=="edit"){
                        echo '
                        <input name="action" type="hidden" id="action" value="update" />
                        <input name="gid" type="hidden" id="gid" value="'.$row["id"].'" />
                        ';
                        echo '<input type="submit" name="submit" value="修改此项目" class="submit" />';
                    }else{
                    
                        echo '
                        <input name="action" type="hidden" id="action" value="add" />
                        ';
                        if (!$fid){
                            echo '<input type="button" name="submit" value="请先选择相应的调查表" class="submit" />';
                        }else{
                            echo '<input type="submit" name="submit" value="新增此项目" class="submit" />';
                        }
                    }
                    ?>
                </td>
                
            </tr>
        
        </table>        
		<script type='text/javascript'>
            Mo("select[name='config[GROUP_BREAK]']").value("<?php echo $config["GROUP_BREAK"];?>");            
           
            Mo("input[name='config[GROUP_MUST]']").value("<?php echo $config["GROUP_MUST"];?>");            
            Mo("input[name='config[GROUP_VERIFY]']").value("<?php echo $config["GROUP_VERIFY"];?>");
			
            Mo("input[name=status]").value("<?php echo $row["status"];?>");
            
            Mo("input[name=type]").value("<?php echo $row["type"]?$row["type"]:'radio';?>");
            
            doChanged("<?php echo $row["type"]?$row["type"]:'radio';?>");
            
        </script>
        
	</form>
    
    </div>
	
    <?php
	}elseif($do=="list"){
	?>
    
	<script type="text/javascript">
        Mo.reader(function(){
    
            <?php
            //只针对新增内容
            if($_GET['mark']){
            ?>
            //操作提示
            Mo.Tips(null,$("mark-<?php echo $_GET['mark'];?>"),'info','点这里开始新增子选项，鼠标指向“小灯泡”将会有小提示。',document,'click',-20,15);
            <?php
            }
            ?>				
            
        });
        
    </script>    
    
	<!--用于动作处理_开始-->
	<form name="formPost" id="formPost" method="post">
		<input name="action" id="action" type="hidden" value="" />        
		<input name="status" id="status" type="hidden" value="" />

		<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" class="table" id="table">
        <tr class="title">
			<td width="30"></td>
            <td>ID</td>
            <td>排序</td>
            <td>项目名称</td>
            <td>子选项 <script>Mo.Tools("Tip","<strong>子选项</strong><br />本项目组供用户选择的选项");</script></td>
            <td>类型</td>
            <td>有效性</td>
            <td>添加时间</td>
            <td>状态</td>
            <td width="60">操作</td>
        </tr>
        
        <?php
        $sql="SELECT * FROM ".VI_DBMODPRE."groups where fid=".$fid." order BY sort asc";
        
        $result=mysql_query($sql,$mysql);
        
        $Project["queries"]++;
        
        while($row=mysql_fetch_assoc($result)){
            
            //配置
        $config = fix_json( $row["config"] );
            
            ?>
            <tr class="line" mark="<?php echo $row["id"];?>" edit="<?php echo $appid;?>.group.php?do=new&action=edit&fid=<?php echo $fid;?>&gid=<?php echo $row["id"];?>&jump={jump}">
				<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><?php echo $row["sort"];?></td>
                <td><strong class="text-yes"><?php echo $row["name"];?></strong></td>
                <td>                        
                <?php 
                if ( !in_array($row["type"],array("text","textarea","file")) ){
                ?>
                
                <a id="mark-<?php echo $row["id"];?>" href="<?php echo $appid;?>.option.php?do=list&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["id"];?>" onclick="new parent.Mo.Dialog( 'dialog','表单子选项', '<iframe src='+this.href+' frameborder=0></iframe>' ,'80%','60%',{ 'remove' : true, 'draged' : true, 'create' : true }); return false;">选项</a>
                
                <?php
                }
                ?>
                </td>
                <td>
                    <?php echo $Modules[$appid]["groups"][$row["type"]];?>
                    <?php
                    if( $config["GROUP_INPUT"]=='Y' ){
                        echo " + 输入";
                    }
                    ?>
                </td>
                <td><?php echo ($config["GROUP_MUST"]=='Y'?'必选项':'可以空');?></td>
                <td><?php echo date($Global["dateformat"],$row["dateline"]);?></td>
                <td><?php echo $Project["status"][$row["status"]];?></td>
                <td>
                <script type="text/javascript">
                    Mo.Tools("RsEdit","?do=new&action=edit&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["id"];?>","");
                    Mo.Tools("RsDelete","?do=list&action=delete&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["id"];?>","");
                </script>
                </td>
            </tr>
            <?php
        
        }
	  
		?>
		<tr class="tfoot">
				<td colspan="10" class="first">
					<?php echo $Modules[$appid]["tool"];?>
				</td>
		</tr>
	</table>
    
    <?php 
	}
	
	//mysql close
	reader_mysql(true);
	
	?>
    </form>
	<!--用于动作处理_结束-->
    
<!--body end-->

<?php html_close();?>