<?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';
	
	//连接数据库
	System :: connect();
	
	$do = getgpc('do');

	$action = getgpc('action');
	
	$fid = getnum('fid',0);
	$gid = getnum('gid',0);
	
	//批处理列表
	$list = getgpc('list');
	$list = is_array($list) ? implode(",", $list) : $list;

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

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

		switch($action){
			
			case "add":
			
				//检查权限
				System :: check_func( 'mod-form-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,".$state.")";						
						System :: $db -> execute( $sql );
						
					}
				}
				
			break;
				
			case "edit":
			
				$sql="SELECT * FROM `mod:option` WHERE id=".$oid;				
				$row = System :: $db -> getOne( $sql );
							
			break;
				
			case "update":
			
				//检查权限
				System :: check_func( 'mod-form-mod', false );
				
				$sql="UPDATE `mod:option` SET name='".$name."',sort=".$sort.",state=".$state." WHERE id=".$oid;
				System :: $db -> execute( $sql );
				
			break;
				
			case "delete":
			
				//检查权限
				System :: check_func( 'mod-form-del', false );
				
				$sql="DELETE FROM `mod:option` WHERE id in(".$list.")";
				System :: $db -> execute( $sql );
				
				System :: redirect($jump,"信息删除成功！");
				
			break;
			
			case "state":

				$sql="UPDATE `mod:option` SET state=".$state." WHERE id in(".$list.")";
				System :: $db -> execute($sql);				
				
				System :: redirect( $jump ,"选项状态更改成功!");
				
			break;
			
			//批量修改
			case "mass":
			
				//检查权限
				System :: check_func( 'mod-form-mod', false );
				
				$name = $_POST["name"];
				$sort = $_POST["sort"];
				
				foreach( $name as $id => $row ){						
					$sql="UPDATE `mod:option` SET name='".$name[$id]."',sort=".$sort[$id]." WHERE id=".$id;
					System :: $db -> execute( $sql );					
				}
				
				System :: redirect( $jump,"信息批量修改成功!");
				
			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>
	';
	?>
    
	<?php    
    if ( in_array($_CACHE[$appid]['group'][$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 $_G['module']['form']["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>
            	<td><strong>项目状态：</strong></td>
                <td>
                    <label>
                    <input name="state" type="radio" class="radio" value="1" checked> 
                    完全开放
                    </label>
                    <label>
                    <input name="state" 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=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>"); </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="state" id="state" 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( count($result)> $_G['setting']['global']['pagesize']/2 ){
		?>
		<tr class="thead">
			<td colspan="10" class="first">
				<?php echo $_G['module']['form']['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="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 ){
				
				?>
                <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 $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($_G['setting']['global']['dateformat'],$row['dateline']);?></td>
                    <td>
                        <?php echo $_G['project']['state'][$row['state']];?>           
                    </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"];?>&list=<?php echo $row["gid"];?>&id=<?php echo $row['id'];?>","");
                        </script>
                    </td>
                </tr>				
				<?php
            }
        ?>
		
			<tr class="tfoot">
				<td colspan="10" class="first">
					<?php echo $_G['module']['form']['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 
	}
	
        //关闭数据库
        System :: connect();
	?>
    
<!--body end-->

<?php html_close();?>