var mainMobileSettingsWindow;
var MobileSettingsGrid;

Ext.define('MobileSettingsModel', {
    extend: 'Ext.data.Model',
    fields: 
    [ 
    	{name: 'id'},
	    {name: 'name'},
	    {name: 'description'},
	    {name:'check',type: 'bool',defaultValue:'false'},
	]
});

var MobileSettingsStore = new Ext.data.Store({
    model: 'MobileSettingsModel',
    autoLoad: true,
    proxy: {
      type: 'ajax',
      url: 'app/LogistMobile_mobile_settings.php?func=showMobileSettings',
      reader: {
          type: 'json',
          root: 'rows'
      }
    }
});

function show_LogistMobile_mobile_settings_Window() {
	MobileSettingsStore.load();
	//rigthsGrid_sm = Ext.create('Ext.selection.CheckboxModel');
	MobileSettingsGrid = new Ext.grid.GridPanel({

		store: MobileSettingsStore,
		autoHeight: true,
		layout: 'fit',
		columnLines: true,
		border: false,
	    //width: 400,
	    //height : 400,
        frame: false,
        autoScroll: true,
        viewConfig: {
            stripeRows: true,
        },

		
		columns : [
	        {   
	            xtype: 'checkcolumn',
	        	dataIndex : 'check',
	            width: 50,
	            sortable: false,
	            hideable: false	
	        }, {
	        	//text : 'Название',
	        	text : reports.name,
	            dataIndex : 'description',
	            width: 300,
	            sortable: false,
	            hideable: false
	        }
	    ]
	});
	
	if (!mainMobileSettingsWindow) 
	{
		mainMobileSettingsWindow = new Ext.Window(
        {
            layout : 'fit', 
        	title : LogistMobile.mobile_settings,
        	//title : rights.permissions,
            closeAction : 'destroy',
            width : 400,
            height : 400,
            resizable : true,
            autoScroll: true,
            items : [MobileSettingsGrid],
            listeners : 
            {
                destroy : function()
                {
                	mainMobileSettingsWindow = null;
                }
            },
            buttons: [{
    	    	//text: 'Сохранить',
    	    	text: all.save,
    	    	handler: function() {
    	    		saveMobileSettings(MobileSettingsStore);
    	    		mainMobileSettingsWindow.destroy();
    	    	}
    	    }, {
    	    	//text: 'Отмена',
    	    	text: all.cancel,
    	    	handler: function () {
    	    		mainMobileSettingsWindow.destroy();
    	    	}
    	    }]
        });
		mainMobileSettingsWindow.show();
	}
}

function saveMobileSettings(store){	

	var arrOfSettingsID = [];
	var arrOfStates = []; 

	store.each(function(record) {
		arrOfSettingsID.push(record.get('id'));
		arrOfStates.push(record.get('check'));			
	});

	$.post('app/LogistMobile_mobile_settings.php', {'func': 'saveMobileSettings','arrOfStates[]': arrOfStates, 'arrOfSettingsID[]': arrOfSettingsID}, function(data) {
		if(!data || data==""){
			Ext.Msg.alert('Сообщение', 'Данные успешно сохранены');
		}else{
			Ext.Msg.alert('Ошибка', data);
		}
	});
}
