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

	$action = GetQuery("action");
	
	$fid = GetNumber("fid",0);
	$gid = GetNumber("gid",0);
	
	//批处理列表
	$list = $_POST["list"];
	$list = implode(",", $list);	

	//上一页
	$jump = $_POST["jump"];
	
	if ($action){

		$name = GetQuery("name");
		$sort = GetNumber("sort",0);
		$status = GetNumber("status",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,status) values('".FilterHtml($val)."',".intval($_POST["_sort"][$key],0).",".$fid.",".$gid.",".time().",0,".$status.")";
						
						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.",status=".$status." where id=".$oid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				//reader_redirect($jump,"信息更新成功！");
				//reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息修改成功!");
				
			break;
				
			case "delete":
			
				$sql="delete from ".VI_DBMODPRE."options where id in(".$list.")";
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				reader_redirect($jump,"信息删除成功！");
				
			break;
			
			case "status":

				$sql="update ".VI_DBMODPRE."options set status=".$status." where id in(".$list.")";
				mysql_query($sql);
				
				$Project["queries"]++;
				
				reader_redirect( $jump ,"选项状态更改成功!");
				
			break;
			
			//批量修改
			case "mass":
			
				//检查权限
				checker_func("mod-form-mod");
				
				$name = $_POST["name"];
				$sort = $_POST["sort"];
				
				foreach( $name as $id => $row ){
						
					$sql="update ".VI_DBMODPRE."options set name='".$name[$id]."',sort=".$sort[$id]." where id=".$id;
					mysql_query($sql,$mysql);

					$Project["queries"]++;
					
				}
				
				reader_redirect( $jump,"信息批量修改成功!");
				
			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 = '';
	}
    ?>
    
    
    <?php 
	if($do=="new"){
	?>
		<div id="box">
    
    	<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(Mo("#tag-box").item(0));
                    </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>
            	<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="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>
        
        <script type="text/javascript">
        	Mo("input[name=status]").value("<?php echo $row["status"];?>");
        </script>
        
	 </form>
	 
	 </div>
	
    <?php
	}elseif($do=="list"){
	?>
    
    <form name="formPost" id="formPost" method="post" action="?fid=<?php echo $fid;?>&gid=<?php echo $gid;?>">
		<input name="action" id="action" type="hidden" value="mass" />
		<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" id="table" class="table">
	
		<?php
		if( mysql_num_rows($result)> $Global["pagesize"]/2 ){
		?>
		<tr class="thead">
			<td colspan="10" class="first">
				<?php echo $Modules[$appid]["tool"];?>
			</td>
		</tr>
		<?php
		}
		?>
	
        <tr class="title">
		<td width="30"></td>
        <td>ID</td>
        <td>排序</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)){
				
				?>
                <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 $row["gid"];?>&id=<?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><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>
                        <?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["gid"];?>&id=<?php echo $row["id"];?>","");
                            Mo.Tools("RsDelete","?do=list&action=delete&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["gid"];?>&id=<?php echo $row["id"];?>","");
                        </script>
                    </td>
                </tr>				
				<?php
            }
        ?>
		
			<tr class="tfoot">
				<td colspan="10" class="first">
					<?php echo $Modules[$appid]["tool"];?>
					 - <a href="javascript:Mo('#formPost input[name=action]').value('mass'); Mo('#formPost').item(0).submit();void(0);"><?php echo loader_image("action/save.png","管理","nobr");?></a>
				</td>
			</tr>
		</table>
    
    </form>
	
    <?php 
	}
	
	//mysql close
	reader_mysql(true);
	?>
    
<!--body end-->

<?php html_close();?>