
Ext.namespace("Ext.hansub.camping");

Ext.hansub.camping.CampingScoreGridPanel = function(config) {
	this.isfirst = true;
	this.config = config;
	/**
	this.viewCampingButton = new Ext.menu.Item({
    	text:'캠핑장 상세 보기',
		tooltip:{title:'캠핑장 상세 보기', text:'캠핑장 상세정보를 화면에 표시해 줍니다.'},
		iconCls:'camping_icon',
		scope:this,
		handler:this.viewCamping
    });
	**/
	
	this.viewUserInfoButton = new Ext.menu.Item({
    	text:'등록자 정복지 보기',
		tooltip:{title:'등록자 정복지 보기', text:'등록자의 캠핑 정복지도를  화면에 표시해 줍니다.'},
		iconCls:'user_icon',
		scope:this,
		handler:this.viewUserInfo
    });
	
	/**
	 * 버튼 메뉴
	 */
	this.popupMenu = new Ext.menu.Menu({
		scope:this,
        items: [
        		//this.viewCampingButton ,
        		this.viewUserInfoButton
	        	]
    });
    this.config.ds = new Ext.data.GroupingStore({ 
		proxy : new Ext.data.HttpProxy({
	    	url : './src/api/Interface.php',
	       	method : 'POST'
	       	
	    }),
	    baseParams:{
				'class':'Camping',
				method:'getCampingScore'
			},
	    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: 'conquercount', mapping: 'conquercount', type: 'auto'},
				{name: 'conquercontents', mapping: 'conquercontents', type: 'auto'},
				{name: 'totalcount', mapping: 'totalcount', type: 'auto'},
				{name: 'conqueror_rate', mapping: 'conqueror_rate', type: 'auto'},
	        	{name: 'content', mapping: 'content', type: 'string'},
	           	{name: 'camping_list_name', mapping: 'camping_list_name', type: 'string'},
				{name: 'realusername', mapping: 'realusername', type: 'string'}
	        ])
    });
    // $result = $result."{uid:'$i', camping_user_uid:'$data[uid]' , camping_user_name:'$data[name]', conquercount:'$data[conquercount]', camping_list_uid:'$camping_list_uid', camping_list_name:'$camping_list_name'}";
	//$result = $result."{uid:'$i', camping_user_uid:'$data[uid]' , camping_user_name:'$data[name]', conquercount:'$data[conquercount]', totalcount:'$totalcount', conqueror_rate:'$conqueror_rate', camping_list_uid:'$camping_list_uid', camping_list_name:'$camping_list_name'}";			
    this.config.cm = new Ext.grid.ColumnModel([
			{header: "", hidden:false, width: 15,	sortable: false,dataIndex: 'uid'},
    		{header: "아이디", hidden:false, width: 45,	sortable: false,dataIndex: 'camping_user_name'},
			{header: "정복률", width: 50,	sortable: false,dataIndex: 'conquercontents'}
	]);
        
    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.closable = false;
					
	Ext.hansub.camping.CampingScoreGridPanel.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.config.ds.load();
	
	//this.on('rowcontextmenu',this.contextMenu, this);
	this.on('activate', function(){
		
			if(this.isfirst){
				this.config.ds.load();
			}
			this.isfirst = false;
			
								},this);
	
	
	
	this.on('rowclick',this.contextMenu, this);
	this.on('rowcontextmenu',this.contextMenu, this);
	//this.on('activate', this.reload, this);
    
};

Ext.extend(Ext.hansub.camping.CampingScoreGridPanel, Ext.grid.GridPanel, {
	/**
	 * 우측 버튼 팝업 메뉴
	 */
    contextMenu : function(grid, index, e){
		
		this.record = this.store.getAt(index);
		/*8
		if(this.record.data.camping_uid == "0" || this.record.data.camping_uid == ""){
			this.viewCampingButton.setDisabled(true);
		}else{
			this.viewCampingButton.setDisabled(false);
		}
		**/
		if(this.record.data.camping_user_uid == "0" || this.record.data.camping_user_uid == ""){
			this.viewUserInfoButton.setDisabled(true);
		}else{
			this.viewUserInfoButton.setDisabled(false);
		}
		
		
		
		
		//this.viewCampingButton.setText("<b>"+this.record.data.camping_list_name+"</b> 바로가기");
		this.viewUserInfoButton.setText("<b>"+this.record.data.realusername+"</b>님 캠핑지도 홈 바로가기");
		//alert(e.getXY());
		this.popupMenu.showAt(e.getXY());
      	//alert(e.getXY());
    },
	
	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.MyCampingPortalPanel({
				id:'myCampingWrapper_'+this.record.data.camping_user_uid,
				camping_user_uid : this.record.data.camping_user_uid,
				title : this.record.data.realusername+'님의 캠핑홈',
				camping_user_name : NAME,
				isall:false,
				closable:true
				
			})
			

			Ext.getCmp('centerCenterPanel').add(this.mycamping);
			Ext.getCmp('centerCenterPanel').activate(this.mycamping);
		}		
		
	},
	/**
	viewCamping : function(){
		
		
		if (Ext.getCmp("CampingWrapperPanel_"+this.record.data.camping_uid)) {
		
			Ext.getCmp('centerCenterPanel').activate("CampingWrapperPanel_"+this.record.data.camping_uid);
		}
		else {
		  if(this.record.data.name != "전체"){
  			this.campingWrapperPanel = new Ext.hansub.camping.CampingWrapperPanel({
  				id:"CampingWrapperPanel_"+this.record.data.camping_uid,
  				
  				name: this.record.data.camping_list_name,
  				'camping_uid':this.record.data.camping_uid,
  				record_data : this.record.data
  			});
  			Ext.getCmp('centerCenterPanel').add(this.campingWrapperPanel);
  			Ext.getCmp('centerCenterPanel').activate(this.campingWrapperPanel);
      }
		}
	},
	**/
	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) {

//		p.body = '<div class=score>캠핑 예정지 : \'<b>' + record.data.camping_list_name + '\'</b></div>';
        return 'x-grid3-row-expanded';
        
       
    },
    
	/**
	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()
			
		}
		
	},
	
	deleteItem : function(grid, records, action, groupId){
		
		
		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',
							's_comment_uid':records.data.uid
						},
						waitMsg:'항목을 삭제하고 있습니다.',
						scope:this,
						success:function(response, options){
							grid.getStore().remove(records);
							
						},
						failure:function(response, options){
							Ext.MessageBox.alert('ERROR', '항목 삭제시 오류가 발생하였습니다..');
						}
						            	
								
					});
				}
			}
		});

		
		
		
		
	},
	
	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();
	}
		

});

