
Ext.namespace("Ext.hansub.board");

Ext.hansub.board.BoardContentPanel = function(parent, config) {

	this.config = config;
	this.config.iconCls = 'board_content';
	this.parent = parent;
	
	

	this.modifyButton = new Ext.Action({
        text: '변경',
        scope:this,
        //disabled:true,
        handler:this.modifyContent,
        iconCls: 'board_modify'
    });
    this.deleteButton = new Ext.Action({
        text: '삭제',
        scope:this,
        //disabled:true,
        handler:this.deleteItem,
        iconCls: 'board_delete'
    });
    
    this.listButton = new Ext.Action({
        text: '목록으로',
        scope:this,       
        handler:this.tolist,
        iconCls: 'board'
    });
	
    
    
    // toolbar
    this.config.tbar = ['->',this.listButton,'-',this.modifyButton,'-',this.deleteButton];
	
	Ext.hansub.board.BoardContentPanel.superclass.constructor.call(this, this.config);
	
	// render event
	//this.on('render', this.init, this);
	
	this.on('activate', this.commentView, this);

	 
   
};

Ext.extend(Ext.hansub.board.BoardContentPanel, Ext.Panel, {
	
	islogin: false,
	
	// rendering
	init:function(){
		//this.checkAuthor();
		//this.reload();
	},
	
	// 변경 권한 습득
	checkAuthor:function(){
		
		Ext.Ajax.request({
			url: './src/api/interface.php',
			params:{
					'class':'User',
					'method':'isLogin'
					},
			scope:this,
			success:function(response, options){
				
				var json = Ext.decode(response.responseText);
				
				if(json.success){
					islogin = true;
            		if(json.uid == this.config.data.s_user_uid || json.isadmin == "Y"){
				    	this.modifyButton.setDisabled(false);
				    	this.deleteButton.setDisabled(false);
						
				    }
            		
            	}else{
					islogin = false;
				}
			},
			failure:function(response, options){
				islogin = false;
				Ext.MessageBox.alert('오류', '서버에 응답이 없습니다. 잠시 후 다시 접근해 주세요.')
			}
			            	
					
		});	
		
		
	},
	
	tolist:function(){
		
		Ext.getCmp('centerCenterPanel').activate(this.parent);
		
	},
	/**
	commentView : function(){
		
		if(!Ext.getCmp('commentGridPanelID')){
    		this.commentGridPanel = new Ext.ux.hansub.board.CommentGridPanel({
				title: "댓글 목록",
				id: "commentGridPanelID",
				closable:true,
				loadMask: {msg:'댓글화면 구성 중'},
				defaults: {autoScroll:true},
				split: true,
				collapsible: true,
		        animCollapse: false,
		        border:false,
				istoolbar:true,
				islogin:this.islogin,
		        hideHeaders:true
			});
    		
    	}
    	
    	Ext.getCmp('commentGridPanelID').setData({
				dbtable_uid:this.config.data.uid,
				islogin:this.islogin,
				dbtable:'s_board'
			});
	
		Ext.getCmp('centerRightTabPanelID').add('commentGridPanelID');
		Ext.getCmp('centerRightTabPanelID').activate('commentGridPanelID');
		
		Ext.getCmp('mainToolbarID').layout('main_right');
	
		
	},
	**/
	
	// Content
	setContent:function(data){
			
			this.uid = data.uid;
			Ext.Ajax.request({
				url: './src/api/Interface.php',
				params: {
					'class': 'Board',
					method: 'getCampingBoard',
					uid: this.uid
				},
				scope: this,
				success: function(response, options){
					var json = Ext.decode(response.responseText);
					if (json.success) {
						
											
						if(this.body){
							this.updateView(json);
						}
												
					}
					else {
						Ext.MessageBox.alert('오류', '서버에 응답이 없습니다. 잠시 후 다시 접근해 주세요.')
					}
					
				},
				failure: function(response, options){
					Ext.MessageBox.alert('오류', '서버에 응답이 없습니다. 잠시 후 다시 접근해 주세요.')
				}
			});

		
	},
	
	reload : function(){
		
		this.setContent({
			uid: this.uid
		});
	},
	

	updateView : function(data){
		
		this.config.data = data;
		
		if(!this.detailEl){
	    
			var bd = this.body;
	    	bd.update('').setStyle('background','#fff');
	    	this.detailEl = bd.createChild(); //create default empty div
	    	
		}	
		//
    	//this.detailEl.hide().update("<div class='board_content_header'>"+data.name+"</div><pre><div class='board_content_content'>"+data.content+"</div></pre>").slideIn('l', {stopFx:true,duration:.5});
			data.nameEcllipsis = Ext.util.Format.ellipsis(data.name, 20)
			Ext.getCmp('BoardContentWrapper_'+data.uid).setTitle(data.nameEcllipsis);
		
			if (UID == this.config.data.camping_user_uid || ISADMIN == "Y") {
				this.modifyButton.setDisabled(false);
				this.deleteButton.setDisabled(false);
			}
			else {
				this.modifyButton.setDisabled(true);
				this.deleteButton.setDisabled(true);
			}
		
			this.detailEl.update(
					"<div class='board_content_header' bgcolor='ffcc33'>"+
						"<span class='board_content_date'>작성일 : "+
							data.regdate+
							"<h4 class='board_content_author'>변경일 : "+
								data.modifydate+
							"</h4>"+
						"</span>"+
						"<h3 class='post-title'>"+
							data.name+
						"</h3>"+
						"<h4 class='board_content_author'>분류 : "+
							data.camping_boardcat_name+
							" / 작성자  : "+
							data.camping_user_name+
						"</h4>"+
					"</div>"+
					"<div class='board_content_body'>"+
						"<p>"+
							data.content+
						"</p>"+
					"</div>"
					);
		
		
		
		
		
		
		
		
		
		
	},
	
	// 
	modifyContent : function(){
		
		
			
			
			if(!Ext.getCmp('addBoardContentPanelID')){
		
				this.addBoardContentPanel = new Ext.hansub.board.AddBoardHtmlContentPanel({
							id:'addBoardContentPanelID',
							title: '게시물 등록',
							iconCls:'add'//,
					        //width:770,
					        //resizable:true,
					        //height: 620		        
				});
			}
			this.config.data.parentID = this.config.parentID;
			

			Ext.getCmp('addBoardContentPanelID').setData(this.config.data);
			
			Ext.getCmp('centerCenterPanel').add('addBoardContentPanelID');
			Ext.getCmp('centerCenterPanel').activate('addBoardContentPanelID');
		
	},
	
	modified : function(){
		//this.parent.reload();
		//this.tolist();
		Ext.getCmp('centerTopPanelID').remove(this);
	},
	
	
	deleteItem : function(){
		
	
		Ext.Msg.show({
			title:'삭제 확인',
			scope:this,
			msg: this.config.data.name+' 을 삭제 하시겠습니까 ?',
			buttons:Ext.Msg.YESNO,
			icon:Ext.MessageBox.QUESTION,
			fn:function(btn, text){
				if(btn == 'yes'){
					Ext.Ajax.request({
						url: './src/api/Interface.php',
						params:{
							'class':'Board',
							'method':'deleteContent',
							'uid':this.config.data.uid
						},
						waitMsg:'항목을 삭제하고 있습니다.',
						scope:this,
						success:function(response, options){
							
							this.tolist();
							Ext.getCmp('centerTopPanelID').remove(this);
						},
						failure:function(response, options){
							Ext.MessageBox.alert('ERROR', '항목 삭제시 오류가 발생하였습니다..');
						}
						            	
								
					});
				}
			}
		});
		
		
		
	}
	
});

