package kz.cafe.admin.panels;

import kz.cafe.admin.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

public class DBListSelectorMultiPanel extends AbstractPanel implements java.io.Serializable{
   private static final long serialVersionUID = -6150508871119562229L;
   
   
   private final Button BT_OK     = new Button("Выбрать");
   private final Button BT_CANCEL = new Button("Отменить");
   
   public final DBListPanel<?> panel;
   

   public DBListSelectorMultiPanel(final AdminMain application,
                                         AbstractTab abstractTab,
                                   final Window window,
                                   final MAbstractEditableDataElement mongoParentRecord,
                                   final java.lang.reflect.Field field
                                  ){

      panel = abstractTab.build();
      this.addComponent(panel);
      //super(application, classDB, descriptor, Dimension.get80(application), setMode);
      
      panel.table.setMultiSelect(true);
      
      HorizontalLayout layH = new HorizontalLayout();
      layH.setWidth("100%");
      
      Label lbSpaceLeft = new Label(" ");
      layH.addComponent(lbSpaceLeft);
      
      BT_OK.setWidth("200px");
      BT_OK.addListener(new Button.ClickListener(){
         private static final long serialVersionUID = 8435830261718609762L;
         @Override
         public void buttonClick(ClickEvent event){
            
            panel.grabSelectedIDes();
            
            try{
               field.set(mongoParentRecord, panel.setSelectedIDes);
            }
            catch (Throwable e){
               throw new Error(e);
            }
            
            MongoService.save(mongoParentRecord);

            
            
            application.getMainWindow().removeWindow(window);
         }
      });
      layH.addComponent(BT_OK);
      
      layH.addComponent(sizeX(10));
      
      BT_CANCEL.setWidth("200px");
      BT_CANCEL.addListener(new Button.ClickListener(){
         private static final long serialVersionUID = -8962051675512645746L;
         @Override
         public void buttonClick(ClickEvent event){
            application.getMainWindow().removeWindow(window);
         }
      });
      
      layH.addComponent(BT_CANCEL);
      Label lbSpaceRight = new Label(" ");
      layH.addComponent(lbSpaceRight);
      
      layH.setExpandRatio(lbSpaceLeft, 20.0f);
      layH.setExpandRatio(lbSpaceRight, 20.0f);
      
      
      this.addComponent(layH);
      this.addComponent(sizeY(5));
   }
}
