package kz.cafe.admin;

import java.util.*;
import com.vaadin.data.*;
import com.vaadin.ui.*;
import kz.cafe.admin.constants.AccessConstants.EditMode;
import kz.cafe.admin.panels.*;
import kz.cafe.admin.panels.model.*;
import kz.cafe.db.model.*;



public class TabOrderItems extends AbstractTab{
   private static final long serialVersionUID = -8572183427270759965L;




   public TabOrderItems(AdminMain application, Set<EditMode> setMode){
      super(application, setMode);
   }
   
   

   
   
   

   @Override
   public DBListPanel<MOrderItem> build(){
		CustomDescriptor dscr = new CustomDescriptor();
		
		CustomField field = new CustomField();
		field.visible = true;
		field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
		field.name = "ID";
      field.db = "ID";
      field.isEditable = false;
      field.style.width = 50;
		field.isSortable = true;
		field.search.row = 0;
      dscr.addField(field);
      
      
      CustomFixedComboField ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Объект";
      ccf.db = "CID";
      ccf.isEditable = true;
      ccf.style.width = 110;
      ccf.isMust = true;
      ccf.search.row = 1;
      dscr.addField(ccf);
      
      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Клиент";
      ccf.db = "UID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = true;
      dscr.addField(ccf);
      
      
      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Акция";
      ccf.db = "AID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = false;
      dscr.addField(ccf);
      
      


      field = new CustomField();
      field.visible = true;
      field.name = "Комментарий";
      field.db = "comment";
      field.isEditable = true;
      field.style.width = 0;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      
      
      
      

      
      CustomDateField dateField = new CustomDateField();
      dateField.visible = true;
      dateField.name = "Создан";
      dateField.db = "created";
      dateField.isEditable = false;
      dateField.style.width = 120;
      dateField.isSortable = true;
      dateField.search.mode = CustomField.Search.SEARCH_MODES.DATE_RANGE; 
      dateField.search.row = 1;
      dscr.addField(dateField);
      
      
      
      
      
      dateField = new CustomDateField();
      dateField.visible = true;
      dateField.name = "Обновлен";
      dateField.db = "updated";
      dateField.isEditable = false;
      dateField.style.width = 120;
      dateField.isSortable = true;
      dateField.search.mode = CustomField.Search.SEARCH_MODES.DATE_RANGE;
      dateField.search.row = 1;
      dscr.addField(dateField);
      
      
      
      
      final CustomField fieldConfirmed = new CustomField();
      fieldConfirmed.visible = true;
      fieldConfirmed.name = "Обработан";
      fieldConfirmed.db = "confirmed";
      fieldConfirmed.isEditable = true;
      fieldConfirmed.style.width = 80;
      fieldConfirmed.isSortable = true;
      fieldConfirmed.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
      fieldConfirmed.search.row = 1;
      dscr.addField(fieldConfirmed);
      

      
      
      
      final CustomField fieldClosed = new CustomField();
      fieldClosed.visible = true;
      fieldClosed.name = "Закрыт";
      fieldClosed.db = "closed";
      fieldClosed.isEditable = true;
      fieldClosed.style.width = 80;
      fieldClosed.isSortable = true;
      fieldClosed.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
      fieldClosed.search.row = 1;
      dscr.addField(fieldClosed);
      
      
      
      final DBListPanel<MOrderItem> panel = new DBListPanel<MOrderItem>(application,
                                                                        MOrderItem.class,
                                                                        dscr,
                                                                        Dimension.getFull(application),
                                                                        setMode,
                                                                        new Order(Order.CREATER,
                                                                                 fieldConfirmed.db,
                                                                                 fieldConfirmed.name));
      
      
      
      panel.table.setCellStyleGenerator(new Table.CellStyleGenerator(){
         private static final long serialVersionUID = -3776734826403357164L;

         @Override
         public String getStyle(Object itemId, Object propertyId){
            if(propertyId == null){
               // Styling for row

               
               Item item = panel.table.getItem(itemId);
               
               boolean isClosed = (Boolean) item.getItemProperty(fieldClosed.name).getValue();
               if(isClosed){
                  return "highlight-gray";
               }
               
               
               boolean isProcessed = (Boolean) item.getItemProperty(fieldConfirmed.name).getValue();
               if(isProcessed){
               }
               else{
                  return "highlight-stressed";
               }
//               if(firstName.toLowerCase().startsWith("g")){
//                  return "highlight-green";
//               }
//               else if(firstName.contains("o")){
//                  return "highlight-red";
//               }
//               else{
//                  return null;
//               }               
               
               
               if(panel.setBlockedRecords.contains(itemId)){
                  return "highlight-blocked";
               }
               else{
                  return null;
               }

            }
            else{
               // styling for column propertyId
               return null;
            }
         }
      });      
      
      
		return panel;
	}




   @Override
   public DBSingle build(int ID){
      // TODO Auto-generated method stub
      return null;
   }




   



   public static CustomDescriptor buildSelector(){
      CustomDescriptor dscr = new CustomDescriptor();
      
      CustomField field = new CustomField();
      field.visible = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
      field.name = "ID";
      field.db = "ID";
      field.isEditable = false;
      field.style.width = 50;
      field.isSortable = true;
      field.search.row = 0;
      dscr.addField(field);
      
      
      CustomFixedComboField ccf = new CustomFixedComboField();
      ccf.visible = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.NONE;
      ccf.name = "Ордер";
      ccf.db = "OID";
      ccf.isEditable = false;
      ccf.style.width = 300;
      ccf.isMust = true;
      ccf.search.row = 0;
      dscr.addField(ccf);
      
      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Блюдо";
      ccf.db = "MID";
      ccf.isEditable = true;
      ccf.style.width = 400;
      ccf.style.alignment = CustomField.Alignment.LEFT;
      ccf.isMust = true;
      dscr.addField(ccf);
      

      
      field = new CustomField();
      field.visible = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
      field.name = "Кол-во";
      field.db = "quantity";
      field.isEditable = true;
      field.style.width = 60;
      field.style.expandable = false;
      field.isSortable = true;
      field.search.row = 0;
      field.style.alignment = CustomField.Alignment.LEFT;
      dscr.addField(field);
      

      
      field = new CustomField();
      field.visible = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER_RANGE;
      field.name = "Цена";
      field.db = "price";
      field.isEditable = false;
      field.style.width = 100;
      field.isSortable = true;
      field.search.row = 0;
      field.style.alignment = CustomField.Alignment.LEFT;
      dscr.addField(field);
      
      
      return dscr; 
   }
}

