Ext.namespace("Ext.hansub.camping");

Ext.hansub.camping.CampingContentPanel = function(parent, config) {
	
	this.config = config;
	this.config.iconCls = 'board_content';
	this.parent = parent;
	
	this.config.title = Ext.util.Format.ellipsis(this.config.title, 15);

	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'
    });
    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);
	}
    
    // toolbar
    this.config.tbar = ['->',this.listButton,'-',this.modifyButton,'-',this.deleteButton];
	
	Ext.hansub.camping.CampingContentPanel.superclass.constructor.call(this, this.config);
	
	// render event
	this.on('render', this.init, this);
	
	this.on('activate', this.commentView, this);
	 
   
};

Ext.extend(Ext.hansub.camping.CampingContentPanel, 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.camping_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(){
		if (this.isparent) {
			Ext.getCmp('centerCenterPanel').activate(this.parent);
			Ext.getCmp('centerCenterPanel').remove(this);
		}else{
			
			board = new Ext.hansub.board.BoardGridPanel({
				text:'커뮤니티',
				id: "board",
				closable:true,
				data: {
					s_boardcat_uid: 0,
					s_boardtype_uid: 1,
					s_boardcat_name : "전체",
					viewtype:'title'
				}
						
			});
		
			Ext.getCmp('centerCenterPanel').add(board);
			Ext.getCmp('centerCenterPanel').activate(board);
			
			
			
		}
		
		
	},
	
	commentView : function(){
		/**
		if(!Ext.getCmp('commentGridPanelID')){
    		this.commentGridPanel = new Ext.ux.hansub.board.CommentGridPanel({
				title: Ext.util.Format.ellipsis(this.config.data.name, 10)+" 의견",
				id: "commentGridPanelID",
				closable:false,
				loadMask: {msg:'댓글화면 구성 중'},
				defaults: {autoScroll:true},
				split: true,
				collapsible: true,
		        animCollapse: false,
		        border:false,
				istoolbar:true,
				islogin:this.islogin,
		        hideHeaders:true
			});
    		
    	}
    	//alert(Ext.util.Format.ellipsis(this.config.data.name, 10)+" 의견");
    	Ext.getCmp('commentGridPanelID').setData({
				dbtable_uid:this.config.data.uid,
				islogin:this.islogin,
				dbtable:'s_board',
				title : Ext.util.Format.ellipsis(this.config.data.name, 10)+" 의견"
			});
	
		Ext.getCmp('centerBottomPanel').add('commentGridPanelID');
		Ext.getCmp('centerBottomPanel').activate('commentGridPanelID');
		Ext.getCmp('mainToolbarID').viewtype('community');
		**/
		
	},
	
	
	// Content
	setContent:function(data){

		this.config.data = data;
		// body
		if(this.body){
			this.reload();
		}
		
	},
	

	reload : function(){
		
		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'>"+this.config.data.name+"</div><pre><div class='board_content_content'>"+this.config.data.content+"</div></pre>").slideIn('l', {stopFx:true,duration:.5});
		
		
		
		if(this.config.data.s_boardtype_type =='acount'){
			
			
			this.detailEl.update(
					"<div class='board_content_header' bgcolor='ffcc33'>"+
						"<span class='board_content_date'>작성일 : "+
							this.config.data.regdate+
							"<h4 class='board_content_author'>변경일 : "+
								this.config.data.modifydate+
							"</h4>"+
						"</span>"+
						"<h3 class='post-title'>"+
							this.config.data.name+
						"</h3>"+
						"<h4 class='board_content_author'>위치 : "+
							this.config.data.camping_region1_name+" "+this.config.camping_region2_name+" "+this.config.address+
							" / 작성자  : "+
							this.config.data.s_user_name+
						"</h4>"+
					"</div>"+
					"<div class='board_content_body'>"+
						"<div id="+this.config.id+"_acount class='innerAcount'>"+
						"</div><br>"+
						"<p>"+
							this.config.data.content+
						"</p>"+
					"</div>"
					);
			acountData = {
				method : 'getBudgetItem',
				closable : false,
				uid : this.config.data.s_bitemcat_uid,
				name : "",
				istoolbar : false,
				istitle : false,
				isEdiable : false,
				forceFit:false,
				board_uid : this.config.data.uid
			};
					
			acountGridPanel = new AcountGridPanel(acountData);
			acountGridPanel.render(this.config.id+"_acount");
			acountGridPanel.loadRightPanel();
			
		}else{
			this.detailEl.update(
					"<div class='board_content_header' bgcolor='ffcc33'>"+
						"<span class='board_content_date'>작성일 : "+
							this.config.data.regdate+
							"<h4 class='board_content_author'>변경일 : "+
								this.config.data.modifydate+
							"</h4>"+
						"</span>"+
						"<h3 class='post-title'>"+
							this.config.data.name+
						"</h3>"+
						"<h4 class='board_content_author'>위치 : "+
							this.config.data.camping_region1_name+" "+this.config.data.camping_region2_name+" "+this.config.data.address+
						"</h4>"+
					"</div>"+
					"<div class='board_content_body'>"+
						"<p>"+
							this.config.data.content+
						"</p>"+
					"</div>"
					);
		}
		
		
		
		
		
		
		
		
		
	},
	
	// 
	modifyContent : function(){
		
		if (this.config.data.s_boardtype_type == 'acount') {
			
			if(!Ext.getCmp('addAcountBoardContentWindowID')){
		
				this.addAcountBoardContentWindow = new Ext.hansub.board.AddBoardContentWindow({
							id:'addAcountBoardContentWindowID',
							title: '예산 의견듣기 변경',
							iconCls:'add',
					        width:675,
					        resizable:false,
					        height: 495
					        				        
				});
			}
			
			Ext.getCmp('addAcountBoardContentWindowID').setData({
					
				s_boardtype_uid : this.config.data.s_boardtype_uid,
				s_boardcat_uid : this.config.data.s_boardcat_uid,
				s_board_uid : this.config.data.uid,
				s_boardcat_name: this.config.data.s_boardcat_name,
				name : this.config.data.name,
				content : this.config.data.content,
				parentID : this.getId()
									
			});
			
			Ext.getCmp('addAcountBoardContentWindowID').show();
			
			
		
		
		}else{
			
			
			if(!Ext.getCmp('addBoardContentWindowID')){
		
				this.addBoardContentWindow = new Ext.hansub.board.AddBoardContentWindow({
							id:'addBoardContentWindowID',
							title: '게시물 등록',
							iconCls:'add',
					        width: 600,
					        resizable:false,
					        height: 420			        
				});
			}
			
			Ext.getCmp('addBoardContentWindowID').setData({
				s_boardtype_uid : this.config.data.s_boardtype_uid,
				s_boardcat_uid : this.config.data.s_boardcat_uid,
				s_board_uid : this.config.data.uid,
				s_boardcat_name: this.config.data.s_boardcat_name,
				name : this.config.data.name,
				content : this.config.data.content,
				parentID : this.getId()
									
			});
			
			Ext.getCmp('addBoardContentWindowID').show();
			
			
			
		}
		
		
	},
	
	modified : function(){
		this.parent.reload();
		this.tolist();
		
	},
	
	
	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',
							's_board_uid':this.config.data.uid
						},
						waitMsg:'항목을 삭제하고 있습니다.',
						scope:this,
						success:function(response, options){
							this.parent.reload();
							this.tolist();
							
						},
						failure:function(response, options){
							Ext.MessageBox.alert('ERROR', '항목 삭제시 오류가 발생하였습니다..');
						}
						            	
								
					});
				}
			}
		});
		
		
		
	}
	
});

