/**
 * Created by capote on 5/15/14.
 */

Ext.define('Admin.controller.FoodType', {
    extend: 'Ext.app.Controller',

    views: [
        'foodtype.list',
        'foodtype.edit'
    ],

    stores: ['FoodType'],

    models: [],

    init: function() {
        this.control({
            '#editFoodType': {
                click: this.editFoodType
            },
            'foodtypeedit button[action=editFood]': {
                click: this.saveFood
            },
            'foodtypelist button[action=adicionar]': {
                click: this.addFood
            },
            'foodtypelist button[action=delFood]': {
                click: this.deleteFood
            }
        });
    },

    deleteFood: function(button){
        var selected = button.up('foodtypelist').getSelectionModel().getSelection();
        if(selected.length > 0){
            Ext.MessageBox.confirm('Confirmaci&oacute;n', 'Est&aacute; seguro que desea eliminar el tipo de comida seleccionada?', this.deleteFoodType);
        }else{
            Ext.Msg.show({
                title:'Informaci&oacute;n',
                msg: 'Debe seleccionar un tipo de comida para eliminar.',
                buttons: Ext.Msg.YES,
                icon: Ext.Msg.INFO
            });
        }
    },

    deleteFoodType : function(mess){
        if(mess == 'yes'){
            var selected = Ext.ComponentQuery.query('foodtypelist')[0].getSelectionModel().getSelection();

            var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."});
            myMask.show();

            Ext.Ajax.request({
                url: '/admin/deleteFoodData',
                params: {
                    id: selected[0].data.id
                },
                success: function(response){
                    var resp = Ext.decode(response.responseText);
                    myMask.destroy();
                    if(resp['goodResponse'] == true){
                        Ext.ComponentQuery.query('foodtypelist')[0].getStore().reload();
                        Ext.ComponentQuery.query('foodtypelist')[0].getSelectionModel().deselectAll();
                        Ext.getElementById('south').innerHTML = '<p> <img src="/app/ext4/resources/check.png">' + resp['message'] +  '</p>';

                    }else{
                        Ext.MessageBox.alert('Atenci&oacute;n', resp['message']);
                    }
                }
            });
        }
    },

    addFood : function(button){
        Ext.widget('foodtypeedit');
    },

    saveFood : function(button){
        var win    = button.up('window'),
            form   = win.down('panel'),
            values = form.getValues();

        Ext.Ajax.method = 'POST';

        var myValues = {
            id : values.id,
            name : values.name
        };

        var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."});
        myMask.show();

        Ext.Ajax.request({
            url: '/admin/saveFoodData',
            params: myValues,
            success: function(response){
                var resp = Ext.decode(response.responseText);
                myMask.destroy();
                if(resp['goodResponse'] == true){
                    win.close();
                    Ext.ComponentQuery.query('foodtypelist')[0].getStore().reload();
                    Ext.ComponentQuery.query('foodtypelist')[0].getSelectionModel().deselectAll();
                    Ext.getElementById('south').innerHTML = '<p> <img src="/app/ext4/resources/check.png">' + resp['message'] +  '</p>';
                }else{
                    Ext.MessageBox.alert('Atenci&oacute;n', resp['message']);
                }

            }
        });
    },

    editFoodType : function(button){
        var selected = button.up('foodtypelist').getSelectionModel().getSelection();
        if(selected.length > 0){
            var view = Ext.widget('foodtypeedit');
            view.down('panel').loadRecord(selected[0]);
        }else{
            Ext.Msg.show({
                title:'Informaci&oacute;n',
                msg: 'Debe seleccionar un usuario para editar.',
                buttons: Ext.Msg.YES,
                icon: Ext.Msg.INFO
            });
        }
    }
});