package com.dev.swms.client.ui.page;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.Window;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;

import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;

import com.dev.energy.client.rpc.DefineRPCAdapter;
import com.dev.energy.client.utils.StringUtils;

public class BrowsPickItemRulePage extends BrowseRulePage {
	protected transient TabPanel centerPanel;

    protected transient Panel container;
    
    protected transient Panel introPanel;
    
    protected transient Panel westPanel;
    
    protected transient AbstractWestPanel aw;
    
    protected CustomEntity mainNodeObj;
    
    public void draw(VerticalPanel content) {
    	initContainer();
        content.add(container);

    }
    
    public BrowsPickItemRulePage(){
    	params = new HashMap();
    }
    
    protected void initContainer(){
    	this.pageConfig.setTitle("拣货策略管理");
    	
    	container = new Panel();
    	container.setWidth(Window.getClientWidth() - 30);
    	container.setHeight(Window.getClientHeight() - 130);
    	container.setLayout(new BorderLayout());
    	
    	centerPanel = new TabPanel();
        centerPanel.setBodyBorder(false);
        centerPanel.setEnableTabScroll(true);
        centerPanel.setAutoDestroy(false);
        centerPanel.setActiveTab(0);
        
        Panel centerPanelWrappper = new Panel();
        centerPanelWrappper.setLayout(new FitLayout());
        centerPanelWrappper.setBorder(false);
        centerPanelWrappper.setBodyBorder(false);
        
        westPanel = getWestPanel();
        westPanel.setAutoDestroy(true);
        westPanel.setBorder(true);
        westPanel.setBodyBorder(true);
        
        BorderLayoutData centerLayoutData = new BorderLayoutData(RegionPosition.CENTER);
        centerLayoutData.setMargins(new Margins(1, 1, 1, 1));

        BorderLayoutData westLayoutData = new BorderLayoutData(RegionPosition.WEST);
        westLayoutData.setMargins(new Margins(1, 1, 1, 1));
        westLayoutData.setCMargins(new Margins(1, 1, 1, 1));
        westLayoutData.setMinSize(155);
        westLayoutData.setMaxSize(350);
        westLayoutData.setSplit(true);
        
        introPanel = new Panel("策略详情");
        introPanel.setPaddings(10);
        introPanel.setLayout(new FitLayout());
        introPanel.setAutoShow(false);
        introPanel.setAutoScroll(true);
       
        centerPanel.add(introPanel, centerLayoutData);
        centerPanelWrappper.add(centerPanel);
        
        container.add(westPanel,westLayoutData);
        container.add(centerPanelWrappper, centerLayoutData);
    }
    
    public Panel getWestPanel() {
    	aw =new AbstractWestPanel("",this,"pickItemRuleManager");
		return aw;
    	
	}
    
    public void addConterPanel(Component component){
    	centerPanel.clear();
    	BorderLayoutData centerLayoutData = new BorderLayoutData(RegionPosition.CENTER);
        centerLayoutData.setMargins(new Margins(1, 1, 1, 1));
    	centerPanel.add(component,centerLayoutData);
    	centerPanel.setActiveTab(0);
    }
    
    public void resetConterPanel(){
    	addConterPanel(introPanel);
    }
    
    public void displayRule(CustomEntity nodeObj){
    	mainNodeObj = nodeObj;
    	rpcGetTreeByUserObj(nodeObj);
    }
    
    /**
	 * 根据传入的节点信息,得到相关策略
	 * @param params
	 */
	protected void rpcGetTreeByUserObj(CustomEntity nodeObj){
		Map params = new HashMap();
		params.put("nodeObj",nodeObj);
		DefineRPCAdapter rcp = new DefineRPCAdapter() {
			public void successProcessor(Map map) {
				List rules = (List)map.get("result");
				disRules(rules);
			}
		};
		rcp.ajaxRpcCallBackExec("pickItemRuleManager",
				"getPutRackRules",params);
	}
	
	private void disRules(List rules){

		addConterPanel(new ListRuleGridPanel(rules,"pick",this,mainNodeObj));
	}

	
	/**　
	 * 显示策略信息
	 * @return
	 */
	private String getNoticePanel(DisplayEntity displayEntity) {
		String html = "";
		html += "<TR style=\" line-height:30px;font-weight:normal; color:#666666;\" >"+
		"<TD style=\" border-bottom:1px solid #a0c8e0;vertical-align:middle;\" >"+displayEntity.getLeftDis()+
		"&nbsp;&nbsp;&nbsp;<img src=\"images/silk/user_female.gif\" />&nbsp;&nbsp;&nbsp;";
		
		if(!StringUtils.isEmpty(displayEntity.getDefaultLocation())){
			html +=	"<img src=\"images/silk/flag_green.gif\" />&nbsp;&nbsp;"+displayEntity.getDefaultLocation()+"("+displayEntity.getMothed()+")";
		}
		if(!StringUtils.isEmpty(displayEntity.getPosition())){
			html +=	"<img src=\"images/silk/flag_red.gif\" />&nbsp;&nbsp;"+displayEntity.getPosition()+"("+displayEntity.getMothed()+")";
		}
		
		html +="</TD></TR>";
		
		return html;
	}
	public void reload() {
	
		//刷新父节点，显示新加节点
		aw.clearRoot(aw.treePanel.getRoot());
		aw.rpcInitTree(null);
		//刷新右边规则面板
		displayRule(mainNodeObj);
	}

	public void release() {
		// TODO Auto-generated method stub
		
	}
}
