<?php

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

require '../../include/loader.php';
html_start("表单编辑 - VeryIDE");
?>

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

	//module
	require 'naver.php';

	//连接数据库
	System :: connect();
	
	$jump = getgpc('jump');
	$action = getgpc('action');
	$fid = getnum('fid',0);
	$gid = getnum('gid',0);
	
	if ($action){

		$name = getgpc('name');
		$state = getnum('state',0);
		$tags = getgpc("tags");
		$skin = getgpc("skin");
		$thumb = getgpc("thumb");
		$description = getgpc("description");
		$quote = getgpc("quote");
		$category = getgpc("category");
		
		//配置
		$config = clear_array( $_POST['config'] );
		$config = format_json( fix_json( $config ) );
		
		//开始时间
		$start = getgpc('start');
		
		if( !$start || !isDateTime($start["date"]." ".$start["time"]) ){
			$start = time();				
		}else{				
			$start = strtotime($start["date"]." ".$start["time"]);
		}
		
		//结束时间
		$expire = getgpc("expire");
		
		if( !$expire || !isDateTime($expire["date"]." ".$expire["time"]) ){
			$expire = time();				
		}else{				
			$expire = strtotime($expire["date"]." ".$expire["time"]);
		}
		
		switch($action){
		
			case "add":
			
				//检查权限
				System :: check_func( 'mod-form-add', false );		
			
				$sql="INSERT INTO `mod:form`(name,aid,account,state,description,tags,dateline,start,expire,skin,quote,thumb,appid,config,ip,stat,category) values('".$name."',".$_SESSION['Manager']['id'].",'".$_SESSION['Manager']['account']."',".$state.",'".$description."','".$tags."',".time().",".$start.",".$expire.",'".$skin."','".$quote."','".$thumb."','".$appid."','".$config."','".GetIP()."',0,'".$category."')";
				
				System :: $db -> execute( $sql );
				
				$fid = System :: $db -> getInsertId();
				
				//生成缓存
				Cached :: form( $appid, $fid );
				
				System :: redirect($appid.".list.php?mark=".$fid,"信息提交成功!");
				
			break;
				
			case "update":
			
				//检查权限
				System :: check_func( 'mod-form-mod', false );
			
				$sql="UPDATE `mod:form` SET name='".$name."',state=".$state.",description='".$description."',tags='".$tags."',skin='".$skin."',quote='".$quote."',thumb='".$thumb."',config='".$config."',start=".$start.",expire=".$expire.",category='".$category."',`modify`=".time().",mender='".$_SESSION['Manager']['account']."' WHERE id=".$fid;
				
				System :: $db -> execute( $sql );
				
				//生成缓存
				Cached :: form( $appid, $fid );
								
				System :: redirect($jump,"信息修改成功!");
			
			break;
			
			case "edit";
			
				$sql="SELECT * FROM `mod:form` WHERE id=".$fid;
				
				$row = System :: $db -> getOne( $sql );

				if( $row ){                  
					$config = fix_json($row['config']);
				}
			
			break;
		}
		
	}
	  
	//关闭数据库
	System :: connect();
    
    ?>
    
	<?php    
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$_G['product']['version']);
    ?>
    
<!--main-->

	<?php
	
	//显示权限状态
	switch($action){
		case "":
			echo System :: check_func("mod-form-add",true);
		break;
		
		case "edit":
			echo System :: check_func("mod-form-mod",true);
		break;
	}
	
	?>

	<div id="box">
	    <form action="" method="post" name="from1" mode="edit" verify="true">
                        
        <table cellpadding="0" cellspacing="0" class="form">
    
            <tr><td colspan="2" class="section"><strong>基本信息</strong></td></tr>

            <tr>
                <td width="60"><strong>表单主题：</strong></td>
                <td>
                
                    <input type="text" name="category" id="category" class="text digi" fix_name="内容分类" fix_null="yes" onclick="Mo.Soler( this, event, this.value, 'mo-soler', { '内容分类' : ['<?php echo implode("','",explode(" ",trim($_G['setting']['global']['category'])));?>'] }, function( value ){ this.value = value; }, 1 , 23 );" readonly="true" value="<?php echo $row["category"];?>" onfocus="this.blur();" />

                    <input name="name" type="text" class="text" id="name" value="<?php echo $row['name'];?>" size="65" fix_name="表单主题" fix_null="yes" />
                    <select name="skin" id="skin" style=" width:120px;">
                    <?php
                    //遍历皮肤目录
                    $root = "content/theme/";
                    
                    $dirs = loop_dir( $root );
        
                    foreach( $dirs as $file ){
                        
                        //皮肤名称
                        $name = fileparm( $root.$file."/style.css", 'name' );
			
						//皮肤配置
						$conf = fileparm( $root.$file."/style.css", 'config' );
                        
                        //忽略没有名称的，为删除皮肤提供方法
                        if( $name ){	
							echo '<option value="'.$file.'" config="'.$conf.'">'.$name.'</option>';
                        }
                        
                    }
                    ?>
                    </select>
                
                    <script type="text/javascript">
                    
                    Mo("#skin").bind( 'change', function( ele, index, event ){

			var option = Mo( 'option', this ).item( this.selectedIndex );			
			var config = option.getAttribute('config') ? eval('('+option.getAttribute('config')+');') : [];
			
			//隐藏全部
			Mo("tr[extra=display]").hide();
			
			//显示当前
			Mo("tr[mark="+ config['display'] +"]").show();
                        
                        Mo("#preview").show().html( '<img src="content/theme/'+ Mo(this).value() +'/preview.jpg" onerror="this.src=\'<?php echo VI_BASE;?>image/none.gif\'" />' );							
                    
                    });						
                    
                    Mo.Tools("Tip","<strong>添加新风格</strong> <br />将风格文件夹存放至 <span class=text-key>module/<?php echo $appid;?>/content/theme/</span> 下，系统会自动识别。<br />风格文件夹中的 <span class=text-key>preview.jpg</span> 将作为此风格的预览图使用。");
                    </script>
                
                </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","flash","zoom","zoomin","zoomout","html","stat","about"]							
                    });
                    </script>
                    <textarea name="description" cols="81" rows="7" id="description" style="float:left;"><?php echo $row["description"];?></textarea>
                    <div id="preview"></div>
                    
                    <div style="clear:both;"></div>
                    <script type="text/javascript">
                    //new Serv.Upload("<?php echo $appid;?>","<?php echo $row["file"];?>",{'format':['<?php echo implode("','",$_G['upload']['image']);?>','<?php echo implode("','",$_G['upload']['flash']);?>'],'again':true,'recovery':true,'input':false,'ubb':Mo.$('description')});
                    </script>
                </td>
                
            </tr>
        
            <tr text='表单个性化功能'>
                <td><strong>特殊标记：</strong></td>
                <td>
                    <a href="javascript:;"  onclick="Mo('#description').value('$CLOCK',true);" >倒计时</a>
                    <a href="javascript:;"  onclick="Mo('#description').value('[header=宽度,高度,样式(CSS)]Flash地址[/header]',true);" >插入Flash</a>
                    
                    <script type="text/javascript">
                    Mo.Tools("Tip","<strong>统计人数</strong> 将会在页面上实时显示参于本活动的人数<br /><strong>点 击 数</strong> 将会在页面上动态显示本活动被点击次数<br /><strong>倒 计 时</strong> 将会在页面上动态显示本活动倒数记时器<br /><strong>用户列表</strong> 在页面上显示参与本活动用户的页面链接<br /><strong>隐藏统计</strong> 根据需要可以将统计按钮隐藏起来<br /><strong>隐藏标题</strong> 根据需要可以将表单标题进行隐藏<br /><strong>隐藏编号</strong> 根据需要可以将表单选项前的自动编号进行隐藏");
                    </script>
                    
                </td>                    
            </tr>
        
            <tr text='表单自动关闭时间'>
                <td><strong>起止时间：</strong></td>
                <td>
                <input name="start[date]" type="text" class="text date" value="<?php echo date("Y-m-d",($row['start']?$row['start']:time()));?>" size="12" readonly="true" onclick="return showCalendar(this,'y-mm-dd');" title="年-月-日">
                
                <input name="start[time]" type="text" title="小时:分钟" class="text time" onclick="Mo.Timer(this,this.value);" readonly="true" value="<?php echo $row['start'] ? date("H:i",$row['start']) : "00:00";?>" />
                
                -
                <input name="expire[date]" type="text" class="text date" value="<?php echo date("Y-m-d",($row['expire']?$row['expire']:time()+604800));?>" size="12" readonly="true" onclick="return showCalendar(this,'y-mm-dd');" title="年-月-日">
                
                <input name="expire[time]" type="text" title="小时:分钟" class="text time" onclick='Mo.Timer(this,this.value);' readonly="true" value="<?php echo $row['expire'] ? date("H:i",$row['expire']) : "23:59";?>" />
                </td>
            </tr>
        
            <tr>
                <td><strong>表单状态：</strong></td>
                <td>
                    <label>
                    <input type="radio" class="radio" name="state" value="1" checked> 
                    开放表单
                    </label>
                    <label>
                    <input type="radio" class="radio" name="state" value="0">
                    关闭表单
                    </label>
                </td>                
            </tr>
	    
	    <tr extra="display" mark="price">
		<td><strong>商品设置：</strong></td>
		<td>
		
		    <table cellpadding="0" cellspacing="1" border="0" class="gird">
			<tr>
			    <td>
			    商品原价<br />
			    <input name="config[GOOD_PRICE]" type="text" class="text" value="<?php echo $config['GOOD_PRICE'];?>" size="8" fix_name="商品原价" fix_number="no" /> 元
			    </td>
			    <td>
			    秒杀特价<br />
			    <input name="config[GOOD_SALE]" type="text" class="text" value="<?php echo $config['GOOD_SALE'];?>" size="8" fix_name="秒杀特价" fix_number="no" /> 元
			    </td>
			</tr>
		    </table>
		    
		</td>
	    </tr>
            
            <tr>
                <td><strong>预览图片：</strong></td>
                <td>
                <script type="text/javascript">
                new Serv.Upload("thumb","<?php echo $row["thumb"];?>",{'format':['<?php echo implode("','",$_G['upload']['image']);?>'],'again':true,'recovery':true,'input':true });
                </script>
                </td>
            </tr>
            
            <?php
            
            //载入表单高级选项
            file_exists(VI_ROOT."module/passport/") && require(VI_ROOT."module/passport/include.form.php");
            
            //载入表单高级选项
            file_exists(VI_ROOT."module/payment/") && require(VI_ROOT."module/payment/include.form.php");
            
            //载入表单高级选项
            file_exists(VI_ROOT."module/sms/") && require(VI_ROOT."module/sms/include.form.php");
            
            ?>                
          
            <tbody>
                <tr class="nobr">
                    <td><strong></strong></td>
                    <td>
                        <?php
                            if ($action=="edit"){
                                echo '
                                <input name="action" type="hidden" id="action" value="update" />
                                <input name="fid" type="hidden" id="fid" value="'.$fid.'" />
                                ';
                                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>
            </tbody>
        
        </table>
     
        <script type='text/javascript'>
        
            Mo("#category").value( "<?php echo $row["category"];?>" );
            Mo("input[name=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>");
            Mo("#skin").value( "<?php echo $row["skin"];?>" );
            Mo("#skin").item(0).onchange();
            
        </script>
	 </form>
	</div>
	
<!--body end-->

<?php html_close();?>