﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WebControls");

WebControls.DetailsView = function(element) {
    WebControls.DetailsView.initializeBase(this, [element]);

}

WebControls.DetailsView.prototype = {
    initialize: function() {
        WebControls.DetailsView.callBaseMethod(this, 'initialize');

        var that = this;

//        var fields = this.get_fields();
//        this._viewControls = {};
//        this._editControls = {};
//        this._deleteConfirmControls = {};
//        for (var i in fields) {
//            var field = fields[i];
//            if (field.ViewControlID) {
//                this._viewControls[field.FieldName] = $find(field.ViewControlID);
//            }
//            if (field.EditControlID) {
//                this._editControls[field.FieldName] = $find(field.EditControlID);
//            }
//            if (field.DeleteConfirmControlID) {
//                this._deleteConfirmControls[field.FieldName] = $find(field.DeleteConfirmControlID);
//            }
//        }

//        if (this._editButton) {
//            this._editButton.add_buttonClicked(
//                function(sender, e) {
//                    that.showEdit();
//                }
//            );
//        }
//        if (this._deleteButton) {
//            this._deleteButton.add_buttonClicked(
//                function(sender, e) {
//                    that.showDeleteConfirm();
//                }
//            );
//        }
//        if (this._saveButton) {
//            this._saveButton.add_buttonClicked(
//                function(sender, e) {
//                    that.saveItem();
//                }
//            );
//        }
//        if (this._cancelButton) {
//            this._cancelButton.add_buttonClicked(
//                function(sender, e) {
//                    that.showDefaultView();
//                }
//            );
//        }
//        if (this._deleteYesButton) {
//            this._deleteYesButton.add_buttonClicked(
//                function(sender, e) {
//                    // that.showDefaultView();
//                    that.deleteItem();
//                }
//            );
//        }
//        if (this._deleteNoButton) {
//            this._deleteNoButton.add_buttonClicked(
//                function(sender, e) {
//                    that.showDefaultView();
//                }
//            );
//        }

        if (this._grid) {
            // Load details when grid item selected
            this._grid.add_itemClicked(
                function(sender, e) {
                    // alert("grid item " + e.id + " selected");                
                    that.set_itemID(e.id);
                    that.loadItem();                    
                }
            );                                    
            
            // Reload grid when details item saved
            this.add_itemSaved(
                function(sender, e) {
                    that._grid.loadGrid(e.id);
                }
            );
    
            this.add_itemDeleted(
                function(sender, e) {
                    that._grid.loadGrid();
                }
            );
            
            // Handle add button click in grid
            this._grid.add_addButtonClicked(
                function(sender, e) {
                    that.setupNewItem();  
                }            
            );            
            
            // Handle delete button click in grid
            this._grid.add_deleteButtonClicked(
                function(sender, e) {
                    if (that._grid.get_selectedID()) {
                        that.showDeleteConfirm();  
                    } else {
                        that.showStartView();
                        
                        alert("Please select an item first");
                    }
                }            
            );                        
            
        }

        this._formView.add_editClicked(
            function(sender, e) {
                // that.hideAddIndicator();
                that._raiseEvent("editClicked", {});
            }        
        );
        this._formView.add_cancelClicked(
            function(sender, e) {
                // that.hideAddIndicator();
                that._raiseEvent("cancelClicked", e);
            }
        );

        this._formView.add_itemSave(
            function(sender, e) {
                // Should automatically get item from the FormView
                that.saveItem();
            }        
        );

        this._formView.add_itemDelete(
            function(sender, e) {
                // Should automatically get item from the FormView
                that.deleteItem();
            }        
        );

        if (this._loadOnInit) {
            this.loadItem();
        }

    },
    dispose: function() {
        //Add custom dispose actions here
        WebControls.DetailsView.callBaseMethod(this, 'dispose');
    },
    
//    showDefaultView: function() {
//        // Shows the appropriate view depending on
//        // which templates have been set up
//        if (this._viewPanelID) {
//            this.showView();
//        } else {
//            this.showEdit();
//        }    
//    },
//
//    MAYBE NEED TO HAVE THESE METHODS AND THEY JUST CALL THE SAME METHOD ON FORMVIEW?
//    
//    showView : function() {
//        // this._panelSet.showPanel("ViewPanel");
//        this._panelSet.showPanel(this._viewPanelID);
//    },
//    showEdit : function() {
//        // this._panelSet.showPanel("EditPanel");
//        this._panelSet.showPanel(this._editPanelID);
//    },
//    showDeleteConfirm : function() {
//        this._panelSet.showPanel(this._deleteConfirmPanelID);
//    },
//    
//    showMessage: function(message) {
//        alert(message);  // FOR NOW - Can optionally show in message label later
//    },
    
    get_item: function() {    
        return this._formView.get_item();

    },
    
    set_item: function(item) {    
        this._formView.set_item(item);
    },
    
    get_itemID : function() {
        return this._formView.get_itemID();
    },

    set_itemID : function(id) {
        this._formView.set_itemID(id);
    },
    
    get_foreignID : function() {
        return this._formView.get_foreignID();
    },

    set_foreignID : function(id) {
        this._formView.set_foreignID(id);
    },

    showStartView: function() {
        this._formView.showStartView();
    },      
    showDefaultView: function() {
        this._formView.showDefaultView();
    },      
    showNotSelected : function() {
        this._formView.showNotSelected();
    },
    showLoading : function() {
        this._formView.showLoading();
    },
    showView : function() {
        // this._panelSet.showPanel("ViewPanel");
        this._formView.showView();
    },
    showEdit : function() {
        // this._panelSet.showPanel("EditPanel");
        this._formView.showEdit();
    },
    showDeleteConfirm : function() {
        this._formView.showDeleteConfirm();
    },

    showAddIndicator: function() {
        this._formView.showAddIndicator();
    },
    hideAddIndicator: function() {
        this._formView.hideAddIndicator();
    },
    
//    // This triggers the loading of the record
//    // (should show a loading panel while it's loading)
//    set_itemID: function(id) {
//        // alert("id: " + id);    
//        this._itemID = id;
//        this.loadItem();
//    },
    
    setupNewItem : function() {
        this._formView.setupNewItem();    
        
//        if (this._itemClass) {
//            var newItem;
//            newItem = eval("new " + this._itemClass + "()");
//            if (this._foreignID) {
//                newItem[this._foreignFieldName] = this._foreignID;
//            }

//            this._raiseEvent("constructingNewItem", { item: newItem });    
//            this.set_item(newItem);    
//            this.showEdit();    
//        
//        } else {
//            programError("No item class specified");
//        }    
//    
    },
    
    loadItem: function() {

        var that = this;

        if (this._showLoadingPanel) {
            this.showLoading();
        }
    
        if (this._serviceProxy) {
                   
            var serviceProxy = eval(this._serviceProxy);
        
            if (this.get_itemID() > 0) {
                serviceProxy.Get(
                    this._daoName,
                    this.get_itemID(),
                    function(result) {
                        var item = result;
                        
                        that.set_item(item);
                        
                        that._raiseEvent('itemLoaded', {item: item});

                        if (that._showLoadingPanel) {
                            setTimeout(function() {
                                that.showDefaultView();
                            }, 600);                                        
                        } else {
                            that.showDefaultView();
                        }
                    },
                    webServiceError
                );
            } else {
                programError("PK is not > 0");
            }
        } else {
            programError("ServiceProxy not specified");
        }

    },

    validateEditControls: function() {
        return this._formView.validateEditControls();
    },
    
    saveItem: function() {

        var that = this;
    
        var item = this.get_item();
        
        if (this.validateEditControls()) {        
            this._raiseEvent("itemSave", {item: item});

            if (this._serviceProxy) {
                       
                var serviceProxy = eval(this._serviceProxy);
            
                // if (item.ID > 0) {
                if (item[this._pkFieldName] > 0) {
                    serviceProxy.Update(
                        this._daoName,
                        item,
                        function(result) {
                            // var item = result;                            
                            // that.set_item(item);
                            that.loadItem();
                            that._raiseEvent("itemSaved", {id: that.get_itemID(), item: item});
                            if (that._formView.get_messageLabel()) {
                                that._formView.get_messageLabel().flashMessage(that._saveMessage);
                            }
                        },
                        webServiceError
                    );
                } else {
                    serviceProxy.Insert(
                        this._daoName,
                        item,
                        function(result) {
                            that.set_itemID(result);
                            // item.ID = result;
                            item[this._pkFieldName] = result;
                            that.loadItem();
                            that._raiseEvent("itemSaved", {id: that.get_itemID(), item: item});
                            if (that._formView.get_messageLabel()) {
                                that._formView.get_messageLabel().flashMessage(that._saveMessage);
                            }
                        },
                        webServiceError
                    );   
                }
            }
            
            // Not sure if we should go back to the view mode
            // here automatically or rely on the caller to do that
            // Could be optional based on a parameter?
            // (Also we are not necessarily going back to 
            // view mode anyway, eg if it's being used a a filter panel etc)
            
        } else {
            this.showMessage("One or more fields are invalid");
        }        
    },
    
    deleteItem: function() {

        var that = this;
    
        var item = this.get_item();
        
//        // Is this the right place to raise this or should
//        // it be done after the validateControls?
//        this._raiseEvent("saveClicked", {item: item});
        
        if (this.validateEditControls()) {        
            this._raiseEvent("itemDelete", {item: item});

            if (this._serviceProxy) {
                       
                var serviceProxy = eval(this._serviceProxy);
            
                serviceProxy.Delete(
                    this._daoName,
                    item,
                    function(result) {
                        // var item = result;                            
                        // that.set_item(item);
                        that.set_itemID(null);
                        that.set_item({});
                        // that.loadItem();
                        that.showStartView();
                        that._raiseEvent("itemDeleted", {});
                        if (that._formView.get_messageLabel()) {
                            // that._formView.get_messageLabel().flashMessage("Item Deleted");
                            that._formView.get_messageLabel().flashMessage(that._deleteMessage);
                        }
                    },
                    webServiceError
                );
            }
            
            // Not sure if we should go back to the view mode
            // here automatically or rely on the caller to do that
            // Could be optional based on a parameter?
            // (Also we are not necessarily going back to 
            // view mode anyway, eg if it's being used a a filter panel etc)
            
        } else {
            this._formView.showMessage("One or more fields are invalid");
        }        
    }
    
}
WebControls.DetailsView.createProperty("formView");

// WebControls.DetailsView.createProperty("fields");
// WebControls.DetailsView.createProperty("panelSet");

//// These may or may not exist...
//WebControls.DetailsView.createProperty("editButton");
//WebControls.DetailsView.createProperty("deleteButton");
//WebControls.DetailsView.createProperty("saveButton");
//WebControls.DetailsView.createProperty("cancelButton");
//WebControls.DetailsView.createProperty("deleteYesButton");
//WebControls.DetailsView.createProperty("deleteNoButton");
//WebControls.DetailsView.createProperty("messageLabel");
WebControls.DetailsView.createProperty("saveMessage");
WebControls.DetailsView.createProperty("deleteMessage");

WebControls.DetailsView.createProperty("showLoadingPanel");

//WebControls.DetailsView.createProperty("viewPanelID");
//WebControls.DetailsView.createProperty("editPanelID");
//WebControls.DetailsView.createProperty("deleteConfirmPanelID");

WebControls.DetailsView.createProperty("grid");

WebControls.DetailsView.createProperty("serviceProxy");
WebControls.DetailsView.createProperty("daoName");
// WebControls.DetailsView.createProperty("itemClass");
WebControls.DetailsView.createProperty("loadOnInit");
// WebControls.DetailsView.createProperty("itemID"); /* We don't want to be using a special setter */
// WebControls.DetailsView.createProperty("foreignID");
WebControls.DetailsView.createProperty("pkFieldName");
WebControls.DetailsView.createProperty("foreignFieldName");

// WebControls.DetailsView.createEvent("saveClicked");
WebControls.DetailsView.createEvent("constructingNewItem");
WebControls.DetailsView.createEvent("editClicked");
WebControls.DetailsView.createEvent("cancelClicked");
WebControls.DetailsView.createEvent("itemSave");
WebControls.DetailsView.createEvent("itemSaved");
WebControls.DetailsView.createEvent("itemDelete");
WebControls.DetailsView.createEvent("itemDeleted");
WebControls.DetailsView.createEvent("itemLoaded");

WebControls.DetailsView.registerClass('WebControls.DetailsView', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();