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

	//连接数据库
	System :: connect();

	$do = getgpc('do');
	$jump = getgpc('jump');
	$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');
		$jump = getgpc('jump');
		$sort = getnum("sort",0);
		$state = getnum('state',0);
		$type = getgpc("type");
		$description = getgpc("description");
		
		$config = $_POST['config'];				
		$config = format_json( fix_json( $config ) );
		
		switch($action){
				
			case "add":
			
				//检查权限
				System :: check_func( 'mod-vote-add', false );		
			
				if (!$fid || !$name){
					$_G['project']['message'] ="表单必选项不能为空！";
				}else{
				
					$sql="INSERT INTO `mod:group`(name,aid,account,state,fid,sort,type,dateline,description,config,stat) 
					values('".$name."',".$_SESSION['Manager']['id'].",'".$_SESSION['Manager']['account']."',".$state.",".$fid.",".$sort.",'".$type."',".time().",'".$description."','".$config."',0)";
				
					System :: $db -> execute( $sql );
					
					$id = System :: $db -> getInsertId();
					
					//生成缓存
					Cached :: form( $appid, $fid );
					
					System :: redirect("?do=list&fid=".$fid."&mark=".$id,"信息提交成功!");
				}
				
			break;
				
			case "update":
			
				//检查权限
				System :: check_func( 'mod-vote-mod', false );		
			
				$sql="UPDATE `mod:group` SET name='".$name."',state=".$state.",type='".$type."',sort=".$sort.",description='".$description."',config='".$config."' WHERE id=".$gid;
				System :: $db -> execute($sql);
				
				//生成缓存
				Cached :: form( $appid, $fid );
				
				System :: redirect("?do=list&gid=".$gid."&fid=".$fid,"选项组修改成功!");
				
			break;
				
			case "edit":
						
				$sql="SELECT *  from `mod:group` WHERE id=".$gid;
				$row = System :: $db -> getOne( $sql );

				if( $row ){
					$config = fix_json($row['config']);
				}
				
			break;
				
			case "delete":
			
				System :: $db -> execute("DELETE FROM `mod:group` WHERE id in(".$list.")");
				System :: $db -> execute("DELETE FROM `mod:option` WHERE gid in(".$list.")");
				
				//生成缓存
				Cached :: form( $appid, $fid );
				
				System :: redirect("?do=list&fid=".$fid,"选项组删除成功!");
			
			break;
			
			case "state":
			
				//检查权限
				System :: check_func( 'mod-vote-mod', false );		
			
				$sql="UPDATE `mod:group` SET state=".$state." WHERE id in(".$list.")";
				System :: $db -> execute($sql);
				
				//生成缓存
				Cached :: form( $appid, $fid );
				
				System :: redirect($jump,"选项组修改成功!");
				
			break;
		}
		
	}
	  
	//加载缓存
	Cached :: loader( $appid, 'form.'.$fid );
	?>
<!--main-->
    
	<?php
	
	echo '
    <div class="item">
        <strong>'.$_CACHE[$appid]['form']["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>
	';
	?>
    
    <div id="box">
    
    <?php 
	if($do=="new"){
	?>
    
	<?php    
    echo loader_script(array(VI_BASE."js/mo.ubb.js"),"utf-8",$_G['product']['version']);
    ?>
	<script type="text/javascript">
        
        function doChanged(s){
        
            var a = ['radio','checkbox'];
            for(var i=0; i<a.length; i++){
                Mo('#extra_'+a[i]).hide();	
            }
            Mo('#extra_'+s).show();
            
        }	
		
		/******************/
		
		var styles = ["<?php echo implode('","',str_replace('"','\"',$_G['module']['vote']["style"]));?>"];	
        
        function doStyle(s){        
            
			if( s == '0' ){
				Mo("textarea[name='config[GROUP_TEMPLATE]']").item(0).readOnly = false;	
			}else{
				Mo("textarea[name='config[GROUP_TEMPLATE]']").value( styles[s] );
				Mo("textarea[name='config[GROUP_TEMPLATE]']").item(0).readOnly = true;
			}
			
        }
    
    </script>

	<form action="?" method="post" name="from1" verify="true">
        
		<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" />
                    <select id="config[GROUP_BREAK]" name="config[GROUP_BREAK]" fix_name="每行显示数量" fix_null="yes">
                        <option value="">每行显示数量…</option> 
                        <option value=1>1个		(100%)</option> 
                        <option value=2>2个		(50%)</option> 
                        <option value=3>3个		(33%)</option> 
                        <option value=4>4个		(25%)</option> 
                        <option value=5>5个		(20%)</option> 
                        <option value=6>6个		(16%)</option> 
                        <option value=7>7个		(13%)</option> 
                        <option value=8>8个		(12%)</option> 
                        <option value=9>9个		(11%)</option>
                        <option value=10>10个	(10%)</option>
                    </select>                    
                </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>
                    <?php if( $_G['licence']['type'] != "free" ){ ?>
                    <label> <input type="radio" class="radio" name="type" value="button" onclick='doChanged(this.value)'>	按钮</label>
                    <?php } ?>
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>项目属性：</strong></td>
                <td>
                    
                    <table cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td>
                                起始票数<br />                                    
                                <input type="text" id="GROUP_STAT" name="config[GROUP_STAT]" value="<?php echo $config["GROUP_STAT"] ? $config["GROUP_STAT"] : 0;?>" fix_name="起始票数" fix_number="yes" />
                                <span style="color:red;">慎重设置！</span>
                                <script>Mo.Tools("Tip","为空则为0，仅针对新增子选项，此处一但设置，就写入数据库，不能撤消，请慎重！");</script>
                            </td>
                            <td>
                            
                                <span id="extra_radio" class="none">               
                                	
                                </span>
                            
                                <span id="extra_button" class="none">               
                                	
                                </span>
                                
                                <span id="extra_checkbox" class="none">
                                    选择范围<br />                                    
                                    <input type="text" name="config[GROUP_MIN]" value="<?php echo $config["GROUP_MIN"];?>" size="6" fix_name="选择范围" fix_number="no" />                            		-
                                    <input type="text" name="config[GROUP_MAX]" value="<?php echo $config["GROUP_MAX"];?>" size="7" fix_name="选择范围" fix_number="no" />                            
                                </span>
                                
                            </td>
                        </tr>
                    </table>
	     	
                </td>
                
            </tr>
        
        	<tr>
            	<td><strong>扩展属性：</strong></td>
                <td>
                    
                    <table cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td>
				分页显示<br />
				<input name="config[GROUP_SIZE]" type="text" class="text" value="<?php echo $config["GROUP_SIZE"];?>" size="20" fix_name="分页显示" fix_number="no">
                            <label>
                                <input type="checkbox" class="checkbox" name="config[GROUP_MUST]" value="Y" />
                                必选项
                            </label>
                            </td>
                            <td>
                            显示排序<br />
                            <input name="sort" type="text" class="text" value="<?php echo $row["sort"];?>" size="20" fix_name="项目排序" fix_number="no">
                            <label>
                                <input name="config[GROUP_ALIGN]" type="checkbox" class="checkbox" value="left" />	
                                左对齐
                            </label>
                            <script>Mo.Tools("Tip","将以从小到大的顺序进行显示");</script>
                            </td>
                        </tr>
                    </table>
                    
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>展示风格：</strong></td>
                <td>
                	<ul id='group'>
                    <?php
					
					//遍历皮肤目录
					$base = "image/";
					
					foreach( $_G['module']['vote']["style"] as $key => $val ){
						echo '<li rel="'.$key.'" ><img src="'.$base."style.".$key.'.gif" /></li>';
					}
					
					//默认模板
					$template = fileparm( 'content/theme/'.$_CACHE[$appid]['form']['skin'].'/style.css', 'template' );
					
					//默认类型
					$type = fileparm( 'content/theme/'.$_CACHE[$appid]['form']['skin'].'/style.css', 'type' );

                    ?>
                    </ul>
                    <input type="hidden" value="<?php echo $config["GROUP_STYLE"] ? $config["GROUP_STYLE"] : 0;?>" fix_name="展示风格" fix_null="yes" name="config[GROUP_STYLE]" />
                </td>
                
            </tr>
            
        	<tr>
            	<td><strong>展示模板：</strong></td>
                <td>
					<p>
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","链接地址，将会在插入的位置输出一个链接","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{LINK}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","图片地址，将会在插入的位置显示一个图片","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{IMAGE}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","选项标签，将会在插入的位置显示当前选项名称","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{LABEL}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","内容描述，将会在插入的位置显示一段文字描述","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{DESC}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","详细介绍，将会在插入的位置输出详细页的链接","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{DETAIL}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","投票按钮，将会在插入的位置显示一个小按钮，用于选择或着提交","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{INPUT}</a>
						
						<a href="javascript:void(0);" onmouseover='Mo.Tips(this,event,"tips","票数统计，将会在插入的位置显示当前选项的票数","mouseout",0,-50,{"unique":"tips"});' onclick="Mo('textarea[name=\'config[GROUP_TEMPLATE]\']').value(this.innerHTML,true)">{COUNT}</a>
					</p>
                	<textarea name="config[GROUP_TEMPLATE]" cols="80" rows="5"><?php echo $config["GROUP_TEMPLATE"] ? $config["GROUP_TEMPLATE"] : $template;?></textarea>
                </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="80" rows="5" id="description"><?php echo $row["description"];?></textarea>
                <br />
				 <script type="text/javascript">
				new Serv.Upload("vote","",{'format':['<?php echo implode("','",$_G['upload']['image']);?>'],'again':true,'recovery':true,'input':false,'ubb':Mo.$('description')});
                </script>
                </td>
                
            </tr>
        
        	<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="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'>
            //setSelect("GROUP_MAX","<?php echo $config["GROUP_MAX"];?>");
            Mo("select[name='config[GROUP_BREAK]']").value("<?php echo $config["GROUP_BREAK"] ? $config["GROUP_BREAK"] : 4;?>");            
           	//setCheckBox("GROUP_INPUT","<?php echo $config["GROUP_INPUT"];?>");            
           
            Mo("input[name='config[GROUP_MUST]']").value("<?php echo $config["GROUP_MUST"];?>");            
            Mo("input[name='config[GROUP_ALIGN]']").value("<?php echo $config["GROUP_ALIGN"];?>");            
            //Mo("input[name='config[GROUP_VERIFY]']").value("<?php echo $config["GROUP_VERIFY"];?>");
			
            Mo("input[name=state]").value("<?php echo !empty($row['state']) ? $row['state'] : 1;?>");
            
            Mo("input[name=type]").value("<?php echo $row["type"]?$row["type"]:'radio';?>");
            
            doChanged("<?php echo $row["type"]?$row["type"]:'radio';?>");
			
			Mo("#group li").bind('click',function( ele, index ){
												   
				Mo("#group li").attr({"class":""});
												   
				Mo( ele ).attr({"class":"active"});
				
				Mo("input[name='config[GROUP_STYLE]']").value( ele.getAttribute("rel") );
				
				doStyle( ele.getAttribute("rel") );
				
			}).each(function( ele, index ){
				
				if( ele.getAttribute("rel") == "<?php echo $config["GROUP_STYLE"]?$config["GROUP_STYLE"]:'0';?>" ){
					
					Mo( ele ).attr({"class":"active"});
					
				}else{
					Mo( ele ).attr({"class":""});	
				}
			
			});
			            
        </script>
	 </form>
	
    <?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="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" class="table" id="table">
	  
		<?php
		if( count($result)> $_G['setting']['global']['pagesize']/2 ){
		?>
		<tr class="thead">
			<td colspan="10" class="first">
				<?php echo $_G['module']['vote']['tool'];?>
			</td>
		</tr>
		<?php
		}
		?>
		
        <tr class="title">
            <td></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>有效性</td>
            <td>添加时间</td>
            <td>状态</td>
            <td>操作</td>
        </tr>
        
        <?php
        $sql="SELECT * FROM `mod:group` WHERE fid=".$fid." order BY sort asc";
        
        $result = System :: $db -> getAll( $sql );
        
        foreach( $result as $row ){
            
		//配置
		$config = fix_json( $row['config'] );

		//统计页面数量
		$count = System :: $db -> getValue( "SELECT count(id) FROM `mod:option` WHERE gid=".$row['id'] );
            
            ?>
            <tr class="<?php echo zebra( $i, array( "line" , "band" ) );?>" 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>                        
                <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="parent.Serv.Dialog( 'dialog','表单子选项', '<iframe src='+this.href+' frameborder=0></iframe>' ,'80%','60%',{ 'remove' : true, 'draged' : true, 'create' : true }); return false;">选项</a>
                <?php echo '<sup class="text-yes">['.$count.']</sup>';?>
                </td>
                <td><?php echo $_G['module']['vote']["group"][$row["type"]];?></td>		
		<td class="text-key"><?php echo ($config["GROUP_BREAK"]?'每行'.$config["GROUP_BREAK"].'个':'无');?></td>
		<td class="text-key"><?php echo ($config["GROUP_SIZE"]?'每页'.$config["GROUP_SIZE"].'个':'无');?></td>
		<td class="text-key"><?php echo ($config["GROUP_STYLE"]?'内置（'.$config["GROUP_STYLE"].'）':'自定义');?></td>
                <td><?php echo ($config["GROUP_MUST"]=='Y'?'必选项':'可以空');?></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['id'];?>","");
                    Mo.Tools("RsDelete","?do=list&action=delete&fid=<?php echo $row["fid"];?>&list=<?php echo $row['id'];?>","");
                </script>
                </td>
            </tr>
            <?php
        
        }
	  
		?>
		<tr class="tfoot">
            <td colspan="13" class="first">
                <?php echo $_G['module']['vote']['tool'];?>
            </td>
		</tr>
    </table>
    </form>
	<!--用于动作处理_结束-->

    <?php 
	}
	
        //关闭数据库
        System :: connect();				
	
	?>
    
    </div>
    
<!--body end-->

<?php html_close();?>