// Student Major Tab: Functions ------------------------------------------------

// -------------------------------------------------------------
// grid data store
var majorDataArr = new Ext.data.Store({
    // url: 'sampleData/majorCodes.json',
    url: '/mvc6c/majorGetAll',
    reader: new Ext.data.JsonReader({
        root:'theArray',
        id:'id'
    }, [
    'id',
    'name'
    ])
});
majorDataArr.load();
var rowMajor = Ext.data.Record.create([
    'id',
    'name'
    ]);

// Student Major Grid ------------------------------------------------
var editorMajor = new Ext.form.TextField({
    allowBlank: false,
    maxLength: 100
});
var majorGrid = new Ext.FormPanel({
    title: 'Bill Of Lading',
    height: 300,
    width : 880,        // hard code for MS IE browser
    activeTab: 0,
    frame: true,
    items: [{
        xtype: 'editorgrid',
        store: majorDataArr,
        id: 'gridMajors',
        height: 260,
        clicksToEdit: 1,
        stripeRows: true,
        selModel: new Ext.grid.RowSelectionModel(),
        columns: [
        {
            header: "BOL ID",
            dataIndex: 'id',
            sortable: true,
            width: 60
        },
        {
            header: "Vessel/Airline",
            dataIndex: 'name',
            sortable: true,
            width: 200,
            editor: editorMajor
        }
        ]
    }]
});

// Menu Functions --------------------------------------------------------------

function doRetriveMajors(){
    dh.overwrite( 'theMsgtext0', 'Menu: Refresh, Retrieve: List Major data' );
    majorDataArr.load();
}

function doCreateMajor(){
    dh.overwrite( 'theMsgtext0', 'Menu: Create Vessel/Airline' );
    var grid = Ext.ComponentMgr.get('gridMajors');
    grid.getStore().insert(
        0,  // Insert new record on first line
        new rowMajor({
            id: '0',    // default values
            name: ''
        })
        );
    grid.startEditing(0,1); // row, column
}

function doDeleteMajor(){
    dh.overwrite( 'theMsgtext0', 'Menu: Delete Vessel/Airline' );
    var grid = Ext.ComponentMgr.get('gridMajors');  // Get the id of the EditorGridPanel
    var sm = grid.getSelectionModel();
    var theSelected = sm.getSelected();
    if (!sm.hasSelection()){
        Ext.Msg.show({
            title: 'No row Selected.',
            icon: Ext.MessageBox.INFO,
            buttons: Ext.MessageBox.OK,
            msg: 'Select a row from the grid to be Deleted.',
            width: 325
        });
        dh.overwrite( 'theMsgtext0', 'No row Selected.' );
        return;
    }
    dh.overwrite( 'theMsgtext0', 'Delete: '+theSelected.data.name );
    Ext.Msg.show({
        title: 'Delete',
        icon: Ext.MessageBox.QUESTION,
        buttons: Ext.MessageBox.YESNO,
        msg: 'Confirm Delete: '+theSelected.data.name+'?',
        fn: function(btn){
            if (btn == 'yes'){
                majorDelete(theSelected);
                grid.getStore().remove(theSelected);
                dh.overwrite( 'theMsgtext0', 'Menu: Deleted Vessel/Airline' );
            }
        }
    });
}

function doUpdateMajors(){
    dh.overwrite( 'theMsgtext0', 'Menu: Update: Save New and Edits' );
    var aGrid = Ext.ComponentMgr.get('gridMajors');
    var counter = 0;
    Ext.each( aGrid.getStore().getModifiedRecords(), function(record){
        counter++;
        alert("doUpdateMajors: counter = "+counter+": "+record.data.id+", "+record.data.name);
        if (record.data.id=="0") {
            majorInsert(record);
        }
        else {
            majorUpdate(record);
        }
    } );
    if (counter>0){
        aGrid.getStore().commitChanges();   // commit grid updates
        majorDataArr.load();                // re-load the grid
    }
    dh.overwrite( 'theMsgtext0', 'Menu: Update: Save New and Edits, number of updates = '+counter );
}

// Database Functions --------------------------------------------------------------

function majorInsert(record){
    dh.overwrite( 'theMsgtext1', 'majorInsert '+record.data.name );
    alert('Student Majors, majorInsert: '+record.data.name);
    var conn = new Ext.data.Connection();
    conn.request({
        // http://localhost:8080/mvc6c/majorAdd?name=Math
        url: '/mvc6c/majorAdd',
        // url: 'sampleData/success.json',
        params: {
            name: record.data.name
        },
        success: function(resp,opt) {
            dh.overwrite( 'theMsgtext1', 'Vessel/Airline Insert, success: '+record.data.id+", "+record.data.name );
        },
        failure: function(resp,opt) {
            dh.overwrite( 'theMsgtext1', 'Vessel/Airline Insert: failure' );
        }
    });
}

function majorUpdate(record){
    dh.overwrite( 'theMsgtext1', 'Vessel/Airline Update: '+record.data.name );
    alert('Vessel/Airline, Vessel/Airline Update: '+record.data.name);
    var conn = new Ext.data.Connection();
    conn.request({
        // url: 'sampleData/success.json',
        url: '/mvc6c/majorEdit',
        // http://localhost:8080/mvc6c/majorEdit?id=702&name=Math12
        params: {
            id: record.data.id,
            name: record.data.name
        },
        success: function(resp,opt) {
            dh.overwrite( 'theMsgtext1', 'Vessel/Airline Updated: '+record.data.name );
        },
        failure: function(resp,opt) {
            dh.overwrite( 'theMsgtext1', 'Vessel/Airline Updated: failure' );
        }
    });
}


function majorDelete(record){
    dh.overwrite( 'theMsgtext1', 'Vessel/Airline Delete: '+record.data.name );
    alert('Vessel/Airline, Vessel/Airline Update: '+record.data.name);
    var conn = new Ext.data.Connection();
    conn.request({
        // http://localhost:8080/mvc6c/majorAdd?name=Math
        // http://localhost:8080/mvc6c/majorDelete?delId=Math
        url: '/mvc6c/majorDelete',
        // url: 'sampleData/success.json',
        params: {
            delId: record.data.id
        },
        success: function(resp,opt) {
            dh.overwrite( 'theMsgtext1', 'Deleted: '+record.data.id+", "+record.data.name );
        },
        failure: function(resp,opt) {
            Ext.Msg.alert('Error', '*** Delete failed, could be someone else has already deleted it. Data has been refreshed.');
        }
    });
}

// eof
