<?php

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

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

<!--body start-->
	<?php
	
    //form
	require 'function.php';
	require 'config.php';	

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

	$sid = getnum("sid",0);
	
	$jump = getgpc('jump');
	$action = getgpc('action');
	$id = getnum('id',0);
	
	if ($action){

		$title = getgpc("title");
		$state = getgpc("state");
		/**************/
		
		$sort = getnum("sort",0);
		
		$attr=addslashes($_POST["attr"]);
		
		$content=($_POST["content"]);
		//$content=stripslashes($_POST["content"]);
		
		//exit($content);
		
		switch($action){
			case "add":
			
				//检查权限
				System :: check_func("mod-shop-page-add", false );		

				$sql="INSERT INTO `mod:page`(title,uid,account,state,content,sid,sort,dateline) values('".$title."',".$_SESSION['Manager']['id'].",'".$_SESSION['Manager']['account']."',".$state.",'".$content."','".$sid."','".$sort."',".time().")";
				
				System :: $db -> execute( $sql );
				
				$id = System :: $db -> getInsertId();
				
				//缓存页面缓存
				//Cached :: rows( $appid,"SELECT * FROM `mod:page` WHERE id=".$id);		
				Cached :: multi( $appid,"SELECT * FROM `mod:page` WHERE sid=".$sid." ORDER BY sort asc,id desc","page.$sid");				
				
				$sql="UPDATE `mod:shop` SET `modify`=".time()." WHERE id=".$sid;
				
				System :: $db -> execute($sql);
			
				System :: redirect("page.list.php?sid=".$sid,"信息提交成功!");
				
			break;
			
			case "update":
			
				//检查权限
				System :: check_func("mod-shop-page-mod", false );		

				$sql="UPDATE `mod:page` SET title='".$title."',state=".$state.",content='".$content."',sort='".$sort."',`modify`=".time()." WHERE id=".$id." and sid=".$sid;
				
				System :: $db -> execute($sql);
				
				//缓存页面缓存
				//Cached :: rows( $appid,"SELECT * FROM `mod:page` WHERE id=".$id);		
				Cached :: multi( $appid,"SELECT * FROM `mod:page` WHERE sid=".$sid." ORDER BY sort asc,id desc","page.$sid");		
				
				$sql="UPDATE `mod:shop` SET `modify`=".time()." WHERE id=".$sid;
				
				System :: $db -> execute($sql);
			
				System :: redirect("page.list.php?sid=".$sid,"信息修改成功!");
			
			break;
			
			case "edit";

				$sql="SELECT * FROM `mod:page` WHERE id=".$id;
				$row = System :: $db -> getOne( $sql );
			
			break;
		}
		
	  }
	  
	  
		//关闭数据库
		System :: connect();
	  
	  ?>
<!--main-->
	<?php
	
	//加载缓存
	Cached :: loader($appid,"shop.".$sid);
	
	echo '
    <div id="nav">
        <strong>'.$_CACHE["mod.shop"]["name"].'</strong> 	
    </div>
    
    <ul id="naver">
        <li><a href="page.edit.php?sid='.$sid.'">新增页面</a></li>
        <li><a href="page.list.php?sid='.$sid.'">管理页面</a></li>
    </ul>
	';
	?>
    
    
    
	<?php    
    echo loader_script(array(VI_BASE."editor/kindeditor.js",VI_BASE."editor/lang/zh_CN.js"),'utf-8',$_G['product']['version']); ?>

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

	<div id="box">
		<form action="?" method="post" name="from1" mode="edit" verify="true">     
        
        	<input type="hidden" name="sid" value="<?php echo $sid;?>" />   
                        
        <table cellpadding="0" cellspacing="0" class="form">
        
            <tr>
                <td width="60"><strong>页面标题：</strong></td>
                <td>
                	<input name="title" type="text" class="text" id="title" value="<?php echo $row["title"];?>" size="35" fix_name="页面标题" fix_null="yes" />
                </td>                
            </tr>
        
            <tr>
                <td><strong>所属商家：</strong></td>
                <td>
                	<?php echo $_CACHE["mod.shop"]["name"];?>
                </td>                
            </tr>
        
            <tr>
                <td><strong>详细内容：</strong></td>
                <td>
                	<textarea name="content" cols="50" rows="10" id="content" style="width:95%;height:350px;"><?php echo $row["content"];?></textarea>
					<script type="text/javascript">
					KE.show({
						id : 'content',
						resizeMode : 2,
						newlineTag : 'p',
						allowFileManager : true,
						filterMode : false,
						loadStyleMode : false
						//cssPath : './css/common.css'
					});
                    </script>  
                </td>                
            </tr>
        
            <tr>
                <td><strong>页面排序：</strong></td>
                <td>
                	<input name="sort" type="text" class="text" id="sort" value="<?php echo $row["sort"];?>" /> 
                     <script type="text/javascript">Mo.Tools("Tip","从小到大依次排序");</script>
                </td>                
            </tr>
        
            <tr>
                <td><strong>页面开放：</strong></td>
                <td>
                        
					<?php
                    foreach($_G['project']['state'] as $key => $val){
						if( $key < 0 || $key > 1 ) continue;
                        echo '<label><input type="radio" class="radio" name="state" value="'.$key.'" /> '.$val.'</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>
	 </form>
	</div>
    
	<script type='text/javascript'>
    	Mo("input[name=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>"); 
    </script>
	
<!--body end-->

<?php html_close();?>