/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enterpriseserverwar;

import com.sun.data.provider.RowKey;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Alert;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.StaticText;
import com.sun.webui.jsf.component.Table;
import com.sun.webui.jsf.component.TableRowGroup;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.component.Tree;
import com.sun.webui.jsf.model.DefaultOptionsList;
import com.sun.webui.jsf.model.DefaultTableDataProvider;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import inventario.entity.Category;
import inventario.entity.Facturer;
import inventario.entity.Item;
import inventario.entity.Outcome;
import inventario.entity.Person;
import inventario.entity.Supplier;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version Inventario.java
 * @version Created on 24-11-2008, 01:40:06 PM
 * @author Daniel
 */
public class Inventario extends AbstractPageBean {

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private TextField textFieldFacturerMail = new TextField();

    public TextField getTextFieldFacturerMail() {
        return textFieldFacturerMail;
    }

    public void setTextFieldFacturerMail(TextField tf) {
        this.textFieldFacturerMail = tf;
    }
    private TextField textFieldFacturerWeb = new TextField();

    public TextField getTextFieldFacturerWeb() {
        return textFieldFacturerWeb;
    }

    public void setTextFieldFacturerWeb(TextField tf) {
        this.textFieldFacturerWeb = tf;
    }
    private TextField textFieldFacturerName = new TextField();

    public TextField getTextFieldFacturerName() {
        return textFieldFacturerName;
    }

    public void setTextFieldFacturerName(TextField tf) {
        this.textFieldFacturerName = tf;
    }
    private TextField textFieldSupplierMail = new TextField();

    public TextField getTextFieldSupplierMail() {
        return textFieldSupplierMail;
    }

    public void setTextFieldSupplierMail(TextField tf) {
        this.textFieldSupplierMail = tf;
    }
    private TextField textFieldSupplierWeb = new TextField();

    public TextField getTextFieldSupplierWeb() {
        return textFieldSupplierWeb;
    }

    public void setTextFieldSupplierWeb(TextField tf) {
        this.textFieldSupplierWeb = tf;
    }
    private TextField textFieldSupplierName = new TextField();

    public TextField getTextFieldSupplierName() {
        return textFieldSupplierName;
    }

    public void setTextFieldSupplierName(TextField tf) {
        this.textFieldSupplierName = tf;
    }
    private TableRowGroup tableRowGroupSupplier = new TableRowGroup();

    public TableRowGroup getTableRowGroupSupplier() {
        return tableRowGroupSupplier;
    }

    public void setTableRowGroupSupplier(TableRowGroup trg) {
        this.tableRowGroupSupplier = trg;
    }
    private TableRowGroup tableRowGroupFacturers = new TableRowGroup();

    public TableRowGroup getTableRowGroupFacturers() {
        return tableRowGroupFacturers;
    }

    public void setTableRowGroupFacturers(TableRowGroup trg) {
        this.tableRowGroupFacturers = trg;
    }
    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }
    private TextField textFieldUser = new TextField();

    public TextField getTextFieldUser() {
        return textFieldUser;
    }

    public void setTextFieldUser(TextField tf) {
        this.textFieldUser = tf;
    }
    private TextField textFieldPass = new TextField();

    public TextField getTextFieldPass() {
        return textFieldPass;
    }

    public void setTextFieldPass(TextField tf) {
        this.textFieldPass = tf;
    }
    private PanelLayout layoutPanelLogin = new PanelLayout();

    public PanelLayout getLayoutPanelLogin() {
        return layoutPanelLogin;
    }

    public void setLayoutPanelLogin(PanelLayout pl) {
        this.layoutPanelLogin = pl;
    }
    private Alert alertLogin = new Alert();

    public Alert getAlertLogin() {
        return alertLogin;
    }

    public void setAlertLogin(Alert a) {
        this.alertLogin = a;
    }
    private DropDown dropDownCats = new DropDown();

    public DropDown getDropDownCats() {
        return dropDownCats;
    }

    public void setDropDownCats(DropDown dd) {
        this.dropDownCats = dd;
    }
    private Table tableItems = new Table();

    public Table getTableItems() {
        return tableItems;
    }

    public void setTableItems(Table t) {
        this.tableItems = t;
    }
    private StaticText titleCategory = new StaticText();

    public StaticText getTitleCategory() {
        return titleCategory;
    }

    public void setTitleCategory(StaticText st) {
        this.titleCategory = st;
    }
    private TextField textFieldItemName = new TextField();

    public TextField getTextFieldItemName() {
        return textFieldItemName;
    }

    public void setTextFieldItemName(TextField tf) {
        this.textFieldItemName = tf;
    }
    private DropDown dropDownCatsNewItem = new DropDown();

    public DropDown getDropDownCatsNewItem() {
        return dropDownCatsNewItem;
    }

    public void setDropDownCatsNewItem(DropDown dd) {
        this.dropDownCatsNewItem = dd;
    }
    private TextField textFieldNewCat = new TextField();

    public TextField getTextFieldNewCat() {
        return textFieldNewCat;
    }

    public void setTextFieldNewCat(TextField tf) {
        this.textFieldNewCat = tf;
    }
    private TextField textFieldUserName = new TextField();

    public TextField getTextFieldUserName() {
        return textFieldUserName;
    }

    public void setTextFieldUserName(TextField tf) {
        this.textFieldUserName = tf;
    }
    private TextField textFieldUserPass = new TextField();

    public TextField getTextFieldUserPass() {
        return textFieldUserPass;
    }

    public void setTextFieldUserPass(TextField tf) {
        this.textFieldUserPass = tf;
    }
    private TextField textFieldUserLogin = new TextField();

    public TextField getTextFieldUserLogin() {
        return textFieldUserLogin;
    }

    public void setTextFieldUserLogin(TextField tf) {
        this.textFieldUserLogin = tf;
    }
    private TextField textFieldItemCant = new TextField();

    public TextField getTextFieldItemCant() {
        return textFieldItemCant;
    }

    public void setTextFieldItemCant(TextField tf) {
        this.textFieldItemCant = tf;
    }
    private DropDown dropDownListaItems = new DropDown();

    public DropDown getDropDownListaItems() {
        return dropDownListaItems;
    }

    public void setDropDownListaItems(DropDown dd) {
        this.dropDownListaItems = dd;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Inventario() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Inventario Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    public String tabFacturers_action() {
        getSessionBeanWeb().updateFacturers();
        return null;
    }

    public String buttonAddFacturer_action() {
        Facturer new_facturer = new Facturer();
        new_facturer.setEnable(true);
        new_facturer.setFacturer_name(getTextFieldFacturerName().getText().toString());
        new_facturer.setWeb_site(getTextFieldFacturerWeb().getText().toString());
        new_facturer.setContact_email(getTextFieldFacturerMail().getText().toString());
        getSessionBeanWeb().addFacturer(new_facturer);
        getTextFieldFacturerName().setText("");
        getTextFieldFacturerWeb().setText("");
        getTextFieldFacturerMail().setText("");
        getSessionBeanWeb().updateFacturers();
        return null;
    }

    public String buttonRemoveFacturer_action() {
        RowKey rowKey = getTableRowGroupFacturers().getRowKey();
        int rowId = Integer.parseInt(rowKey.getRowId());
        Facturer facturer = this.getSessionBeanWeb().getFacturers()[rowId];
        getSessionBeanWeb().removeFacturer(facturer);
        return null;
    }

    public String buttonRemoveSuplier_action() {
        RowKey rowKey = getTableRowGroupSupplier().getRowKey();
        int rowId = Integer.parseInt(rowKey.getRowId());
        Supplier supplier = this.getSessionBeanWeb().getSuppliers()[rowId];
        getSessionBeanWeb().removeSupplier(supplier);
        return null;
    }

    public String tabSuppliers_action() {
        getSessionBeanWeb().updateSuppliers();
        return null;
    }

    public String buttonEditSupplier_action() {
        RowKey rowKey = getTableRowGroupSupplier().getRowKey();
        int rowId = Integer.parseInt(rowKey.getRowId());
        Supplier proveedor = this.getSessionBeanWeb().getSuppliers()[rowId];
        getTextFieldSupplierName().setText(proveedor.getDistributor_name());
        getTextFieldSupplierWeb().setText(proveedor.getWeb_site());
        getTextFieldSupplierMail().setText(proveedor.getContact_email());
        getSessionBeanWeb().setSupplierEdit(proveedor);
        return null;
    }

    public String buttonEditFacturer_action() {
        RowKey rowKey = getTableRowGroupFacturers().getRowKey();
        int rowId = Integer.parseInt(rowKey.getRowId());
        Facturer facturer = getSessionBeanWeb().getFacturers()[rowId];
        getTextFieldFacturerName().setText(facturer.getFacturer_name());
        getTextFieldFacturerWeb().setText(facturer.getWeb_site());
        getTextFieldFacturerMail().setText(facturer.getContact_email());
        getSessionBeanWeb().setFacturerEdit(facturer);
        return null;
    }

    public String buttonAddSupplier_action() {
        Supplier nuevo = new Supplier();
        nuevo.setEnable(true);
        nuevo.setDistributor_name(getTextFieldSupplierName().getText().toString());
        nuevo.setWeb_site(getTextFieldSupplierWeb().getText().toString());
        nuevo.setContact_email(getTextFieldSupplierMail().getText().toString());
        getSessionBeanWeb().addSupplier(nuevo);
        getTextFieldSupplierName().setText("");
        getTextFieldSupplierWeb().setText("");
        getTextFieldSupplierMail().setText("");
        getSessionBeanWeb().updateSuppliers();
        return null;
    }

    protected SessionBeanWeb getSessionBeanWeb() {
        return (SessionBeanWeb) getBean("SessionBeanWeb");
    }

    public String tabHome_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String tabItems_action() {
        getSessionBeanWeb().updateCategories();
        return "";
    }

    public String buttonModFacturer_action() {
        if (getSessionBeanWeb().getFacturerEdit() != null && getTextFieldFacturerName().getText() != null) {
            Facturer fac = this.getSessionBeanWeb().getFacturerEdit();
            fac.setFacturer_name(getTextFieldFacturerName().getText().toString());
            fac.setWeb_site(getTextFieldFacturerWeb().getText().toString());
            fac.setContact_email(getTextFieldFacturerMail().getText().toString());
            getSessionBeanWeb().modFacturer(fac);
            getTextFieldFacturerName().setText("");
            getTextFieldFacturerWeb().setText("");
            getTextFieldFacturerMail().setText("");
            getSessionBeanWeb().updateFacturers();
        }
        return null;
    }

    public String buttonModSupplier_action() {
        if (getSessionBeanWeb().getSupplierEdit() != null && getTextFieldSupplierName().getText() != null) {
            Supplier sup = getSessionBeanWeb().getSupplierEdit();
            sup.setDistributor_name(getTextFieldSupplierName().getText().toString());
            sup.setWeb_site(getTextFieldSupplierWeb().getText().toString());
            sup.setContact_email(getTextFieldSupplierMail().getText().toString());
            getSessionBeanWeb().modSupplier(sup);
            getTextFieldSupplierName().setText("");
            getTextFieldSupplierWeb().setText("");
            getTextFieldSupplierMail().setText("");
            getSessionBeanWeb().updateSuppliers();
        }
        return null;
    }

    public String buttonlogin_action() {
        String user = getTextFieldUser().getText().toString();
        String pass = getTextFieldPass().getText().toString();

        if (getSessionBeanWeb().login(user, pass)) {
            layoutPanelLogin.setRendered(false);
        } else {
            getAlertLogin().setSummary("Datos Incorrectos");
            getAlertLogin().setDetail("Intente Nuevamente");
            getAlertLogin().setType("error");
        }
        return null;
    }

    public String buttonAddCategory_action() {
        /*
        if (getTextFieldCategoryName().getText() != null) {
            Category cat = new Category();
            cat.setCategory_name(getTextFieldCategoryName().getText().toString());
            cat.setEnable(true);
            getSessionBeanWeb().addCategory(cat);
            getSessionBeanWeb().updateCategories();
            return "";
        }*/
        return null;
    }

    public String staticTextFacturerWeb_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }


    public String buttonCategoryMod_action() {
        /*if (getTextFieldCategoryName().getText() != null && !getDropDownCats().getSelected().equals("all")) {
            Category cat = getSessionBeanWeb().findCategory(Long.valueOf(getDropDownCats().getSelected().toString()));
            cat.setCategory_name(getTextFieldCategoryName().getText().toString());
            getSessionBeanWeb().modCategory(cat);
            getSessionBeanWeb().updateCategories();
            return "";
        }*/
        return null;
    }

    public String tabAddItems_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String tabGetItems_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String tabPersons_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }


    public String buttonViewItems_action() {
        String texto ="Todas las Categorias";
        if(!getDropDownCats().getSelected().toString().equals("all")){
            String cat = getSessionBeanWeb().findCategory(Long.valueOf(getDropDownCats().getSelected().toString())).getCategory_name();
            this.getSessionBeanWeb().setCategorySelected(getDropDownCats().getSelected().toString());
            texto = "Categoría : "+ cat;
            this.getSessionBeanWeb().updateItems();
            //this.getTextFieldCategoryName().setText(cat);
        }
        else{
            this.getSessionBeanWeb().setCategorySelected(null);
            this.getSessionBeanWeb().updateItems();
            //this.getTextFieldCategoryName().setText("");
        }
        this.getTableItems().setTitle(texto);
        this.titleCategory.setText(texto);
        
        return null;
    }

    public String buttonAddItem_action() {
        
        if (getTextFieldItemName().getText() != null && !getDropDownCatsNewItem().getSelected().equals("all")) {
            Item item = new Item();
            item.setEnable(true);
            item.setItem_name(getTextFieldItemName().getText().toString());
            item.setCategory(getSessionBeanWeb().findCategory(
                    Long.valueOf(this.getDropDownCatsNewItem().getSelected().toString())));
            getSessionBeanWeb().addItem(item);
            getSessionBeanWeb().updateCategories();
             getSessionBeanWeb().updateItems();
            return "";
        }
        return null;
    }

    public String button1_action() {
       if (getTextFieldNewCat().getText() != null) {
            Category cat = new Category();
            cat.setCategory_name(getTextFieldNewCat().getText().toString());
            cat.setEnable(true);
            getSessionBeanWeb().addCategory(cat);
            getSessionBeanWeb().updateCategories();
            getSessionBeanWeb().updateItems();
            return "";
        }
        return null;
    }

    public String tabUser_action() {
            getSessionBeanWeb().updateUsuarios();
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String buttonAddUser_action() {
       if (getTextFieldUserLogin().getText() != null||
               getTextFieldUserPass().getText()!= null ||
               getTextFieldUserName().getText() != null) {
            Person nuevo = new Person();
            nuevo.setEnable(true);
            nuevo.setLogin(getTextFieldUserLogin().getText().toString());
            nuevo.setPassword(getTextFieldUserPass().getText().toString());
            nuevo.setFirstname(getTextFieldUserName().getText().toString());
            getSessionBeanWeb().addPerson(nuevo);
            getSessionBeanWeb().updateUsuarios();
            return "";
        }
        return null;
    }

    public String buttonSolAddItem_action() {
        if (!getTextFieldItemCant().getText().equals("")) {
            ListaItems item_add = new ListaItems();
            Item item = getSessionBeanWeb().findItem(Long.valueOf(getDropDownListaItems().getSelected().toString()));
            item_add.setItem(item);
            item_add.setItem_id(item.getId());
            item_add.setItem_name(item.getItem_name());
            item_add.setItem_cant(Integer.parseInt(getTextFieldItemCant().getText().toString()));
            getSessionBeanWeb().getListaItems().add(item_add);
        }
        return null;
    }

    public String buttonSolSend_action() {
        if(getSessionBeanWeb().getListaItems().size()>0){
            Outcome solicitud = new Outcome();
            solicitud.setTime_event("TODAY");
            solicitud.setOwner(getSessionBeanWeb().getPerson());
            solicitud.setQuantity(getSessionBeanWeb().getListaItems().size());
            for(int i = 0; i< getSessionBeanWeb().getListaItems().size() ; i++){
                solicitud.getOutcome_item_id().add(getSessionBeanWeb().getListaItems().get(i).getItem_id());
                solicitud.getOutcome_item_cant().add(getSessionBeanWeb().getListaItems().get(i).getItem_cant());
                solicitud.getOutcome_item_name().add(getSessionBeanWeb().getListaItems().get(i).getItem_name());
            }
            solicitud.setReady(false);
            getSessionBeanWeb().addOutcome(solicitud);
        }

        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page

        return null;
    }

    public String buttonSolClean_action() {
        getSessionBeanWeb().getListaItems().clear();
        return null;
    }



}

