
Ext.namespace("Ext.ux.hansub.board");

Ext.ux.hansub.board.CommentGridPanel = function(config) {
	
	this.config = config;

	this.addButton = new Ext.Action({
		id:'addCommentID',
        text: '댓글 추가',
        scope:this,
        handler:this.addComment,
        
        iconCls: 'add'
    });
	
	
	this.viewUserInfoButton = new Ext.menu.Item({
    	text:'등록자 정복지 보기',
		tooltip:{title:'등록자 정복지 보기', text:'등록자의 캠핑 정복지도를  화면에 표시해 줍니다.'},
		iconCls:'user_icon',
		scope:this,
		handler:this.viewUserInfo
    });
	
	this.deleteButton = new Ext.menu.Item({
    	text:'댓글 삭제',
		iconCls:'delete',
		scope:this,
		handler:this.deleteComment
    });
	


	/**
	 * 버튼 메뉴
	 */
	this.popupMenu = new Ext.menu.Menu({
		scope:this,
        items: [
        		this.viewUserInfoButton,
				'-',
				this.deleteButton
	        	]
    });
	
	
	/**
	if(IS_LOGIN){
		this.addButton.setDisabled(false);
	}else{
		this.addButton.setDisabled(true);
	}
	**/
    

    
    // toolbar 추가
	
	this.config.tbar = ['->', this.addButton];
	
    this.config.ds = new Ext.data.GroupingStore({ 
		proxy : new Ext.data.HttpProxy({
	    	url : './src/api/Interface.php',
	       	method : 'POST'
	       	
	    }),
	    baseParams:{
				'class':'Board',
				method:'getComment'
			},
	    reader: new Ext.data.JsonReader({ 
            root: 'rows', 
            totalProperty: 'count',
            id:'uid'
	        }, [ 
	        	{name: 'uid', mapping:'uid', type: 'string'},
	        	{name: 'camping_user_uid', mapping: 'camping_user_uid', type: 'string'},
	        	{name: 'camping_user_name', mapping: 'camping_user_name', type: 'string'},
	        	{name: 'title', mapping: 'title', type: 'string'},
				{name: 'emotion', mapping: 'emotion', type: 'string'},
	        	{name: 'content', mapping: 'content', type: 'string'},
	           	{name: 'regdate', mapping: 'regdate', type: 'string'}
	        ]),
	    groupField: 'title',
        sortInfo: {field: 'regdate', direction: 'DESC'}
    });
    
    this.config.cm = new Ext.grid.ColumnModel([
    		{header: "제목", hidden:true, width: 50,	sortable: true,dataIndex: 'title'},
			//{header: "아이콘", width: 22,	sortable: true,dataIndex: 'emotion', renderer: this.getEmotion.createDelegate(this)},
            {header: "등록자", width: 40,	sortable: true,dataIndex: 'camping_user_name'},
            {header: "작성일", width: 100,	sortable: true,dataIndex: 'regdate'}
	]);
        
    this.config.sm =  new Ext.grid.RowSelectionModel({
														singleSelect:true
		        									});
		        		
	this.config.viewConfig = {
            					forceFit:true
								};
	
	this.config.view = new Ext.grid.GroupingView({
				            forceFit:true,
				            enableRowBody:true,
				            showGroupName: false,
				           	showPreview:true,
				            getRowClass : this.applyRowClass,
				            groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "댓글" : "댓글"]})'
					})
	
	//this.config.plugins = [this.rowAction];
	
	this.config.closable = false;
					
	Ext.ux.hansub.board.CommentGridPanel.superclass.constructor.call(this, this.config);
	
	//this.on('render',function(){Ext.getCmp('layout').doLayout();},this);
	
	//this.boardGridSM = this.getSelectionModel();
	//this.boardGridSM.on('rowselect', this.selectedRow, this);

				
	this.view.showView = "full";
	
	this.config.ds.on('load',function(){
	
		//this.addButton 
		/**
		record = this.getStore().getAt(0);
		if(record.get('uid') == "0"){
			this.addButton.setDisabled(true);
		}else{
			this.addButton.setDisabled(false);
		}
		**/
		
		
	},this);
	
	this.on('rowclick',this.contextMenu, this);
	this.on('rowcontextmenu',this.contextMenu, this);
    
};

Ext.extend(Ext.ux.hansub.board.CommentGridPanel, Ext.grid.GridPanel, {
	/**
	 * 우측 버튼 팝업 메뉴
	 */
    contextMenu : function(grid, index, e){
		
		this.record = this.store.getAt(index);
		
		if(this.record.data.camping_user_uid == "x"){
			return;
		}
		
		if(this.record.data.camping_user_uid == "0" || this.record.data.camping_user_uid == ""){
			this.viewUserInfoButton.setDisabled(true);
		}else{
			this.viewUserInfoButton.setDisabled(false);
		}
		
		if(this.record.data.camping_user_uid == UID){
			this.deleteButton.setDisabled(false);
		}else{
			this.deleteButton.setDisabled(true);
		}
		
	
		this.viewUserInfoButton.setText("<b>"+this.record.data.camping_user_name+"</b>님 캠핑지도 홈 바로가기");
		this.popupMenu.showAt(e.getXY());

    },
	
	
	deleteComment : function(){
		
		Ext.Msg.show({
			title:'삭제 확인',
			scope:this,
			msg: '선택 항목을 삭제 하시겠습니까 ?',
			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':'deleteComment',
							'uid':this.record.data.uid
						},
						waitMsg:'항목을 삭제하고 있습니다.',
						scope:this,
						success:function(response, options){
							
							this.getStore().remove(this.record);
							
						},
						failure:function(response, options){
							Ext.MessageBox.alert('ERROR', '항목 삭제시 오류가 발생하였습니다..');
						}
						            	
								
					});
				}
			}
		});
	},
	
	viewUserInfo : function(){
		
		if (Ext.getCmp("myCampingWrapper_" + this.record.data.camping_user_uid)) {
			
			Ext.getCmp('centerCenterPanel').activate("myCampingWrapper_" + this.record.data.camping_user_uid);
			
		}else {
			
			this.mycamping = new Ext.hansub.camping.MyCampingWrapperPanel({
				id:'myCampingWrapper_'+this.record.data.camping_user_uid,
				camping_user_uid : this.record.data.camping_user_uid,
				title : this.record.data.camping_user_name+'님의 캠핑장 정복지도',
				camping_user_name : this.record.data.camping_user_name,
				closable:true
				
			})
			Ext.getCmp('centerCenterPanel').add(this.mycamping);
			Ext.getCmp('centerCenterPanel').activate(this.mycamping);
		}		
		
	},
	
	getEmotion : function(value,p,record){
		return "<img src='./images/emotion/"+record.data.emotion+".gif'>";
	},
	
	setData:function(obj){
		
		if(this.obj){
			
			if( obj.dbtable_uid != undefined && this.obj.dbtable_uid == obj.dbtable_uid){
								
				return;				
			}
			if( obj.s_bitemcat_uid != undefined  && this.obj.s_bitemcat_uid == obj.s_bitemcat_uid){	
						
				return;				
			}
		}
		
		this.obj = obj;
		if (this.obj.title) {
			this.setTitle(this.obj.title);
		}
		
		this.config.ds.load({
			params:{
					dbtable_uid:this.obj.dbtable_uid,
					//s_bitemcat_uid:this.obj.s_bitemcat_uid,
					dbtable:this.obj.dbtable
					}
				});
				
	},
	
	applyRowClass: function(record, rowIndex, p, ds) {

		if(this.showView == 'title'){
        	return 'x-grid3-row-collapsed';
        }else if(this.showView == 'summary'){
        	p.body = '<p>' + Ext.util.Format.ellipsis(Ext.util.Format.stripTags(record.data.content), 200) + '</p>';
            return 'x-grid3-row-expanded';
        }else if(this.showView == 'full'){
			p.body = '<div class=boardcomment>' + record.data.content + '</p>';
            return 'x-grid3-row-expanded';
        }else{
        	return 'x-grid3-row-collapsed';
        }
       
    },
    
	/**
	newWin : function(){
		
		var window = new DetailWindow();
        window.init("centerBottomTabPanelID", Ext.getCmp('ItemInfoLayoutPanelID'));
	},
    **/
    /**
    selectedRow : function(sm, index, record){
    	
    	this.selecteduid = record.data.uid;
  	
	},
	**/
	
	addItem : function(){
		if(Ext.getCmp('AddBoardContentWindowID')){
			
			
			
			Ext.getCmp('AddBoardContentWindowID').setData({
				
				s_boardtype_uid : this.obj.data.s_boardtype_uid,
				parentID : this.obj.text
				
				
			});
			
			Ext.getCmp('AddBoardContentWindowID').show();
			
		}else{
			
			addBoardContentWindow = new AddBoardContentWindow({
				
				s_boardtype_uid : this.obj.data.s_boardtype_uid,
				parentID : this.obj.text
				
				
			});
			addBoardContentWindow.show()
			
		}
		
	},
	

	
	addComment:function(){
		if(Ext.getCmp('AddCommentWindowID')){
			Ext.getCmp('AddCommentWindowID').reset();
			Ext.getCmp('AddCommentWindowID').init(
			{
				dbtable_uid:this.obj.dbtable_uid,
				s_bitemcat_uid:this.obj.s_bitemcat_uid,
				dbtable:this.obj.dbtable
			
			},this);
			Ext.getCmp('AddCommentWindowID').show();
		}else{
			commentWindow = new Ext.ux.hansub.window.AddCommentWindow(
			{
				dbtable_uid:this.obj.dbtable_uid,
				s_bitemcat_uid:this.obj.s_bitemcat_uid,
				dbtable:this.obj.dbtable
			
			},this);
			commentWindow.show();
		}
		
	},
	
	reload : function(){
		this.config.ds.reload();
	}
		

});

