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

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

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.TextAlign;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.GridRowListener;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;
import com.dev.swms.client.ui.data.Page_PA_DataAccessor;
import com.dev.energy.client.message.IMessagePage;
import com.dev.energy.client.ui.panel.AbstractSupportPanel;
import com.dev.energy.client.utils.LocaleUtils;

public class Sub_DetailsTable_Panel extends AbstractSupportPanel {
	
	private final static String DEFAULT_NAME = "pickTicketDetails_Panel";
	transient GridPanel detailsTable;
	private RecordDef recordDef;
	private ColumnModel columnModel;
	private Store store;
	private MemoryProxy proxy;
	private ArrayReader reader;
	transient ToolbarButton autoAllocateButton;
	private ColumnConfig ptiColumn;
	private ColumnConfig iiColumn;
	private ColumnConfig pcColumn;
	private ColumnConfig pkuiColumn;
	private ColumnConfig icColumn;
	private ColumnConfig inColumn;
	private ColumnConfig puColumn;
	private ColumnConfig po1Column;
	private ColumnConfig po2Column;
	private ColumnConfig pp1Column;
	private ColumnConfig paColumn;
	private ColumnConfig pp2Column;
	private ColumnConfig psColumn;
	private ColumnConfig lColumn;
	private String flag;
	transient CheckBox checkBox;
	
	public Sub_DetailsTable_Panel(IMessagePage page) {
		super(page, DEFAULT_NAME);
		this.drawWidget();
	}
		
	private void initAvailableDatas(GridPanel grid, int rowIndex,boolean isFitAsLot) {
		Long itemId = Long.valueOf(grid.getStore().getAt(rowIndex).getAsString("item.id"));
		Integer convertFigure = grid.getStore().getAt(rowIndex).getAsInteger("packageUnit.convertFigure");
		String propC1 = grid.getStore().getAt(rowIndex).getAsString("propC1");
		String propC2 = grid.getStore().getAt(rowIndex).getAsString("propC2");
		String propC3 = grid.getStore().getAt(rowIndex).getAsString("propC3");
		String propC4 = grid.getStore().getAt(rowIndex).getAsString("propC4");
		String propC5 = grid.getStore().getAt(rowIndex).getAsString("propC5");
		String propC6 = grid.getStore().getAt(rowIndex).getAsString("propC6");
		String propC7 = grid.getStore().getAt(rowIndex).getAsString("propC7");
		String propC8 = grid.getStore().getAt(rowIndex).getAsString("propC8");
		String propC9 = grid.getStore().getAt(rowIndex).getAsString("propC9");
		String propN1 = grid.getStore().getAt(rowIndex).getAsString("propN1");
		String propN2 = grid.getStore().getAt(rowIndex).getAsString("propN2");
		String propN3 = grid.getStore().getAt(rowIndex).getAsString("propN3");
		String propD1 = grid.getStore().getAt(rowIndex).getAsString("propD1");
		String propD2 = grid.getStore().getAt(rowIndex).getAsString("propD2");
		String propD3 = grid.getStore().getAt(rowIndex).getAsString("propD3");
		String propL1 = grid.getStore().getAt(rowIndex).getAsString("propL1");
		String propL2 = grid.getStore().getAt(rowIndex).getAsString("propL2");
		String propL3 = grid.getStore().getAt(rowIndex).getAsString("propL3");
				
		final Map<String,Object> param = new HashMap<String,Object>();
		param.put("itemId", itemId);
		param.put("convertFigure", convertFigure);
		if(isFitAsLot){
			param.put("propC1", propC1);
			param.put("propC2", propC2);
			param.put("propC3", propC3);
			param.put("propC4", propC4);
			param.put("propC5", propC5);
			param.put("propC6", propC6);
			param.put("propC7", propC7);
			param.put("propC8", propC8);
			param.put("propC9", propC9);
			param.put("propN1", propN1);
			param.put("propN2", propN2);
			param.put("propN3", propN3);
			param.put("propD1", propD1);
			param.put("propD2", propD2);
			param.put("propD3", propD3);
			param.put("propL1", propL1);
			param.put("propL2", propL2);
			param.put("propL3", propL3);
		}
		if(flag == "pickTicketPage"){
			//查询对应发货单明细的库存信息
			this.curPADataAccessor().initAvailableInfo(param);
    	}else if(flag == "waveDocPage"){
    		//查询对应波次明细的库存信息
    		this.curPADataAccessor().initWaveDocAvailableInfo(param);
    	}

	}
	

	protected void drawWidget(){	
		
		flag = curPADataAccessor().getFlag();
		recordDef = new RecordDef(new FieldDef[] { 
				 new IntegerFieldDef("pickTicketDetail.id"), 
				 new IntegerFieldDef("item.id"), 
				 new IntegerFieldDef("packageUnit.id"), 
				 new StringFieldDef("packageUnit.convertFigure"),			 			 			 		
				 new StringFieldDef("item.code"), 
				 new StringFieldDef("item.name"), 
				 new StringFieldDef("packageUnit.unit"), 
				 new IntegerFieldDef("pickTicketDetail.orderQuantity"), 
				 new IntegerFieldDef("pickTicketDetail.orderQtyOfMasterUnit"),
				 new IntegerFieldDef("pickTicketDetail.preAllocatedQuantity"), 
				 new IntegerFieldDef("pickTicketDetail.allocatedQuantity"),
				 new IntegerFieldDef("pickTicketDetail.pickedQuantity"), 
				 new IntegerFieldDef("pickTicketDetail.shippedQuantity"),
				 new StringFieldDef("lotInfor"),
				 new StringFieldDef("propC1"),
				 new StringFieldDef("propC2"),
				 new StringFieldDef("propC3"),
				 new StringFieldDef("propC4"),
				 new StringFieldDef("propC5"),
				 new StringFieldDef("propC6"),
				 new StringFieldDef("propC7"),
				 new StringFieldDef("propC8"),
				 new StringFieldDef("propC9"),
				 new StringFieldDef("propN1"),
				 new StringFieldDef("propN2"),
				 new StringFieldDef("propN3"),
				 new StringFieldDef("propD1"),
				 new StringFieldDef("propD2"),
				 new StringFieldDef("propD3"),
				 new StringFieldDef("propL1"),
				 new StringFieldDef("propL2"),
				 new StringFieldDef("propL3")
				 }); 			
		 
			ptiColumn = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.id"), "pickTicketDetail.id"); 
			ptiColumn.setHidden(true);
		 
			iiColumn = new ColumnConfig(LocaleUtils.getText("item.id"), "item.id"); 
			iiColumn.setHidden(true);
		 
			pcColumn = new ColumnConfig(LocaleUtils.getText("packageUnit.convertFigure"), "packageUnit.convertFigure"); 
			pcColumn.setHidden(true);
		 
			pkuiColumn = new ColumnConfig(LocaleUtils.getText("packageUnit.id"), "packageUnit.id"); 
			pkuiColumn.setHidden(true);	 	 
		 
			icColumn = new ColumnConfig(LocaleUtils.getText("item.code"), "item.code", 150, false); 
			icColumn.setAlign(TextAlign.CENTER);
			icColumn.setSortable(true);

			inColumn = new ColumnConfig(LocaleUtils.getText("item.name"), "item.name", 200, false); 
			inColumn.setAlign(TextAlign.CENTER); 

			puColumn = new ColumnConfig(LocaleUtils.getText("packageUnit.unit"), "packageUnit.unit", 80, false); 
			puColumn.setAlign(TextAlign.CENTER); 

			po1Column = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.orderQuantity"), "pickTicketDetail.orderQuantity", 100, false); 
			po1Column.setAlign(TextAlign.CENTER); 

			po2Column = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.orderQtyOfMasterUnit"), "pickTicketDetail.orderQtyOfMasterUnit", 100, false); 
			po2Column.setAlign(TextAlign.CENTER); 
		 
			pp1Column = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.preAllocatedQuantity"), "pickTicketDetail.preAllocatedQuantity", 100, false); 
			pp1Column.setAlign(TextAlign.CENTER);
			if(flag == "waveDocPage"){
				pp1Column.setHidden(true);
        	}
		 
			paColumn = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.allocatedQuantity"), "pickTicketDetail.allocatedQuantity", 100, false); 
			paColumn.setAlign(TextAlign.CENTER); 

			pp2Column = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.pickedQuantity"), "pickTicketDetail.pickedQuantity", 100, false); 
			pp2Column.setAlign(TextAlign.CENTER); 

			psColumn = new ColumnConfig(LocaleUtils.getText("pickTicketDetail.shippedQuantity"), "pickTicketDetail.shippedQuantity", 100, false); 
			psColumn.setAlign(TextAlign.CENTER);
			if(flag == "waveDocPage"){
				psColumn.setHidden(true);
        	}
		 
			lColumn = new ColumnConfig(LocaleUtils.getText("lotInfor"), "lotInfor", 150, false); 
			lColumn.setAlign(TextAlign.CENTER);
		 
			ColumnConfig propC1Column = new ColumnConfig("lotInfo.propC1","lotInfo.propC1"); 
			propC1Column.setHidden(true);
		 
			ColumnConfig propC2Column = new ColumnConfig("lotInfo.propC2","lotInfo.propC2"); 
			propC2Column.setHidden(true);
		 
			ColumnConfig propC3Column = new ColumnConfig("lotInfo.propC3","lotInfo.propC3"); 
			propC3Column.setHidden(true);
		 
			ColumnConfig propC4Column = new ColumnConfig("lotInfo.propC4","lotInfo.propC4"); 
			propC4Column.setHidden(true);
		 
		 	ColumnConfig propC5Column = new ColumnConfig("lotInfo.propC5","lotInfo.propC5"); 
		 	propC5Column.setHidden(true);
		 
		 	ColumnConfig propC6Column = new ColumnConfig("lotInfo.propC6","lotInfo.propC6"); 
		 	propC6Column.setHidden(true);
		 
		 	ColumnConfig propC7Column = new ColumnConfig("lotInfo.propC7","lotInfo.propC7"); 
		 	propC7Column.setHidden(true);
		 
		 	ColumnConfig propC8Column = new ColumnConfig("lotInfo.propC8","lotInfo.propC8"); 
		 	propC8Column.setHidden(true);
		 
		 	ColumnConfig propC9Column = new ColumnConfig("lotInfo.propC9","lotInfo.propC9"); 
		 	propC9Column.setHidden(true);
		 
		 	ColumnConfig propN1Column = new ColumnConfig("lotInfo.propN1","lotInfo.propN1"); 
		 	propN1Column.setHidden(true);
		 
		 	ColumnConfig propN2Column = new ColumnConfig("lotInfo.propN2","lotInfo.propN2"); 
		 	propN2Column.setHidden(true);
		 
		 	ColumnConfig propN3Column = new ColumnConfig("lotInfo.propN3","lotInfo.propN3"); 
		 	propN3Column.setHidden(true);
		 
		 	ColumnConfig propD1Column = new ColumnConfig("lotInfo.propD1","lotInfo.propD1"); 
		 	propD1Column.setHidden(true);
		 
		 	ColumnConfig propD2Column = new ColumnConfig("lotInfo.propD2","lotInfo.propD2"); 
		 	propD2Column.setHidden(true);
		 
		 	ColumnConfig propD3Column = new ColumnConfig("lotInfo.propD3","lotInfo.propD3"); 
		 	propD3Column.setHidden(true);
		 
		 	ColumnConfig propL1Column = new ColumnConfig("lotInfo.propL1","lotInfo.propL1"); 
		 	propL1Column.setHidden(true);
		 
		 	ColumnConfig propL2Column = new ColumnConfig("lotInfo.propL2","lotInfo.propL2"); 
		 	propL2Column.setHidden(true);
		 
		 	ColumnConfig propL3Column = new ColumnConfig("lotInfo.propL3","lotInfo.propL3"); 
		 	propL3Column.setHidden(true);
		 
		 	columnModel = new ColumnModel(new ColumnConfig[]{ 
				 ptiColumn,iiColumn,pcColumn,pkuiColumn,icColumn,inColumn,puColumn,po1Column,
				 po2Column,pp1Column,paColumn,pp2Column,psColumn,lColumn,propC1Column,propC2Column,
				 propC3Column,propC4Column,propC5Column,propC6Column,propC7Column,propC8Column,
				 propC9Column,propN1Column,propN2Column,propN3Column,propD1Column,propD2Column,
				 propD3Column,propL1Column,propL2Column,propL3Column
				 }); 
		 	
	
			proxy = new MemoryProxy(new String[][]{}); 
			reader = new ArrayReader(recordDef); 
			store = new Store(proxy, reader);
			store.load();
			detailsTable = new GridPanel();

			detailsTable.setAutoScroll(true);
			detailsTable.setStore(store); 							 		 		 			 	
			detailsTable.setColumnModel(columnModel);
			
			detailsTable.setSelectionModel(new RowSelectionModel(false));
			
            ButtonListenerAdapter listener = new ButtonListenerAdapter() {
                public void onClick(Button button, EventObject e) {
//整单分配	
                	autoAllocate();
                	
                }
            };
            
            autoAllocateButton = new ToolbarButton("整单分配",listener);
            autoAllocateButton.setDisabled(true);
            
            checkBox = new CheckBox("强制货品批次匹配");
            checkBox.setChecked(true);
			detailsTable.addGridRowListener(new GridRowListener(){
				public void onRowClick(GridPanel grid, int rowIndex,
						EventObject e) {
					autoAllocateButton.setDisabled(false);
                	if(checkBox.isChecked()){
                		initAvailableDatas(grid,rowIndex,true);
                	}else{
                		initAvailableDatas(grid,rowIndex,false);
                	}
					if(flag == "pickTicketPage"){					
						//设置当前选中的发货单ID
						final Long ptdId = Long.valueOf(grid.getStore().getAt(rowIndex).getAsString("pickTicketDetail.id"));
						curPADataAccessor().setPtdId(ptdId);
					}else if(flag == "waveDocPage"){						
						//设置当前选中的波次ID
						final Long wddId = Long.valueOf(grid.getStore().getAt(rowIndex).getAsString("pickTicketDetail.id"));
						curPADataAccessor().setWddId(wddId);
					}
				}

				public void onRowContextMenu(GridPanel grid, int rowIndex,
						EventObject e) {
					// TODO Auto-generated method stub
					
				}

				public void onRowDblClick(GridPanel grid, int rowIndex,
						EventObject e) {
					// TODO Auto-generated method stub
					
				}
			});
			HorizontalPanel panel2 = new HorizontalPanel();
			panel2.add(autoAllocateButton);
			panel2.add(checkBox);
			panel2.setCellWidth(autoAllocateButton, "120px");

			this.setLayout(new RowLayout());
			this.add(detailsTable,new RowLayoutData("88%"));
			this.add(panel2,new RowLayoutData("12%"));
//			this.addButton(autoAllocateButton);

			
	}
//整单分配	 
	@SuppressWarnings("unchecked")
	private void autoAllocate() {
		final Map param = new HashMap();
		if(flag == "pickTicketPage"){
			//存放发货单ID，以便整单分配后刷新信息
			Long pickTicketId = this.curPADataAccessor().currentPickTicket().getId();
			param.put("pickTicketId", pickTicketId);
		}else if(flag == "waveDocPage"){
			//存放波次ID，以便整单分配后刷新信息
			Long waveDocId = this.curPADataAccessor().currentPickTicket().getId();
			param.put("waveDocId", waveDocId);
		}
		final List<Long> result = new ArrayList<Long>();
		for(int i=0; i<detailsTable.getStore().getCount();i++){
			if(detailsTable.getSelectionModel().isSelected(i)){
				result.add(Long.valueOf(detailsTable.getStore().getAt(i).getAsString("pickTicketDetail.id")));
			}
		}
		param.put("parentIds", result);
		if(flag == "pickTicketPage"){
			//发货单整单分配
			this.curPADataAccessor().atuoAllocate(param);
		}else if(flag == "waveDocPage"){
			//波次整单分配
			this.curPADataAccessor().atuoWaveDocAllocate(param);
		}
	}
	
	public Page_PA_DataAccessor curPADataAccessor() {
		return (Page_PA_DataAccessor)super.getData();
	}	
	
	public void doDispath(String message){
		super.doDispath(message);
//	System.out.print("进入doDispath()方法了！");
		
		if(Page_PA_DataAccessor.INIT_DETAILS_INFO.equals(message) || Page_PA_DataAccessor.INIT_WD_DETAILS_INFO.equals(message)){
			Object[][] data = new Object[this.curPADataAccessor().all_PDS().size()][];
				for (int i=0;i<this.curPADataAccessor().all_PDS().size();i++){
					data[i] = this.curPADataAccessor().all_PDS().get(i).toArray();
				}
		

			proxy = new MemoryProxy(data);
			store.setDataProxy(proxy);
			store.reload();
			detailsTable.reconfigure(store, columnModel);						 
	
		}
	}
}

