var LogistMobile_cargo_Window = null;
var LogistMobile_cargo_WindowAddEdit = null;
	
/* store & model --> */	
	Ext.define('cargoLogistMobileModel', {
	    extend: 'Ext.data.Model',
		fields: ['id','NameGroup','NameCargo', 'barcode','Comments','Cost1','Cost2','Cost3',{name: 'indoor', type: 'bool'}]
	});
	var cargoLogistMobileStore = new  Ext.data.Store({   
			//autoLoad: true,	
	        model: 'cargoLogistMobileModel',
			groupField: 'NameGroup',
	        proxy: 
	        {
	          type: 'ajax',
	          url: 'app/LogistMobile_CRM_cargo.php?func=show',
	          reader: 
	          {
	            type: 'json',
	            root: 'rows'
	          }
	        },listeners: {
				load: function(store, records, successful, operation, eOpts) {
					store.each(function(record){
					});
				}
			}
	});
/* store & model --> */


Ext.define('groupLogistMobileModel', {
	    extend: 'Ext.data.Model',
		fields: ['id','value']
	});
var groupLogistMobileStore = new Ext.data.Store(
{     
        model: 'groupLogistMobileModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/LogistMobile_CRM_cargo.php?func=group',
          reader: 
          {
            type: 'json',
            root: 'rows'
          }
        }
});

function show_LogistMobile_CRM_cargo_Window() {
	
	cargoLogistMobileStore.load();
	
	if (!LogistMobile_cargo_Window) {

	    Xcoord = 40;
	    Ycoord = 60;  
	    Wwidth = 700;  
	    Wheight = 400;
			
/* grid --> */			
	    var cargoLogistMobileGrid = new Ext.grid.Panel({
	    	selType: 'rowmodel',
	        columnLines: true,
	        layout: 'fit',
	        store:  cargoLogistMobileStore,
	        loadMask: true,
	        border: false,
	        autoScroll: true,
			id: 'LogistMobile_cargoGridId',
	        viewConfig: {
	        	preserveScrollOnRefresh: true,
	        },
			features: [{
	  					  ftype: 'groupingsummary',
	  					  groupHeaderTpl: '{name}',
	  					  hideGroupedHeader: false,
	  					  enableGroupingMenu: false
	  				  }],
	        columns:[
			{
				xtype: 'checkcolumn', 
				width: 30, 
				dataIndex: 'indoor', 
				hideable: false, 
				sortable: false,
				listeners:
                {
                    checkchange:function(CheckColumn, rowIndex, checked, eOpts )
                    {
						
					}
				}
			},
			{
	        	xtype: 'rownumberer',
				width: 30,	
	        },
			{
	        	text: 'Название',
	            dataIndex: 'NameCargo',            
	            flex: 1,
	            sortable: true,
	        },
			{
	        	text: 'Штрих-код',
	            dataIndex: 'barcode',
	            width: 70,
				sortable: true,
	        },
			{        	
	        	text: 'Стандарт',
	            dataIndex: 'Cost1',
	            width: 70,
				sortable: true,
	        },
			{
	        	text: 'Мастер',
	            dataIndex: 'Cost2',
	            width: 70,
				sortable: true,
	        },
			{
	        	text: 'Премиум',
	            dataIndex: 'Cost3',
	            width: 70,
				sortable: true,
	        }
			
			], 
	        listeners: {
	        	itemclick: function(cmp, records) {
					
	        	},
				itemdblclick: function(cmp, records) {
		    		show_LogistMobile_cargo_WindowAddEdit(records.get('id'),records);
		    	}
	        },
			tbar:[
				{
	    		xtype: 'button',
	    		//text: 'Создать',
				iconCls: 'add',
				tooltip: 'Создать',
	    		handler: function() {
					show_LogistMobile_cargo_WindowAddEdit();
				}
				},
				{
	    		xtype: 'button',
	    		//text: 'Редактировать',
				iconCls: 'edit',
				tooltip: 'Редактировать',
	    		handler: function() {
					var select = Ext.getCmp('LogistMobile_cargoGridId').getView().getSelectionModel().getSelection()[0];
						if(select){
							show_LogistMobile_cargo_WindowAddEdit(select.get('id'),select);
						}else {
							Ext.Msg.alert(all_toir.attention, 'Выберите строку'); 	
						}
					
				}
				},
				{
		        xtype: 'button',
			    //text: 'Удалить',
				iconCls: 'delete',
				tooltip: 'Удалить',
		        handler: function(){
							var arrayId=[];									
							cargoLogistMobileStore.each(function(record)   
							{
								if(record.get('indoor') == true){
									arrayId.push(record.get('id'));
								}
								
							});
							
							if (arrayId.length==0){
								Ext.Msg.alert(all_toir.error, 'Выберите строку(и)');
							} else {
								Ext.MessageBox.confirm(all_toir.attention, 'Вы действительно хотите удалить выбранную строку(и)',  function(btn){
								if(btn=='yes'){
									
								var myMask = new Ext.LoadMask(LogistMobile_cargo_Window, {msg: all.loading});
								myMask.show();
								$.post("app/LogistMobile_CRM_cargo.php",{'delete':true, 'arrayId[]':arrayId}, function(data){
										if(data){
											var obj = Ext.decode(data);
											if(obj.success == true){
												Ext.MessageBox.alert(all_toir.attention, 'Удалено');
												cargoLogistMobileStore.load();
												myMask.hide();
											}else{
												myMask.hide();
												Ext.MessageBox.alert(all_toir.error, obj.message);
											}
				
										}else{
											Ext.MessageBox.alert(all_toir.error, '');
											myMask.hide();
										}
								});
								}
								});
							}
				}
				}
			]
	    	
	    });
/* <-- grid */		    
	    
		
		
	        Ext.getBody().mask(all.loading);
	        LogistMobile_cargo_Window = new Ext.Window({
	            layout : 'fit',
	            title: 'Товары',
	            closeAction : 'destroy',
	            width : parseInt(Wwidth),
	            height : parseInt(Wheight),
	            maximizable:true,
				constrain : true,
				resizable: true,
	            defaults: {
	            },
				listeners : {
	            	destroy : function(){
	            		LogistMobile_cargo_Window = null;	
	            	}
	            },
	            items: [cargoLogistMobileGrid]
			})      
		LogistMobile_cargo_Window.show(); 
		Ext.getBody().unmask(all.loading);

	}


}





function show_LogistMobile_cargo_WindowAddEdit(id,records) {

	if (!id){
        var title = 'Добавление товара';
        var isTTLock = false;
    }else{
        var title = "Редактирование товара";
		var isTTLock = true;
    }
    var date = new Date();

    if(!LogistMobile_cargo_WindowAddEdit){
        LogistMobile_cargo_WindowAddEdit = new Ext.Window({
            width:400,
            height:320,
            title: title,
            closeAction : 'destroy',
            listeners : {
                destroy : function()
				{
                    LogistMobile_cargo_WindowAddEdit = null;
                },
				afterrender: function( eOpts )
				{
					
				}
            },
            layout:'fit',
            resizable: true,
            constrainHeader: true,
            modal:true,
            items:[
                {
                    xtype: 'form',
                    autoScroll: true,
                    id: "LogistMobile_cargo_RequestFormId",
                    border: false,
                    frame: true,
                    autoHeight: true,
                    layout:'anchor',
                    items:
                    [
                    	
			                	
										
											
										
										
										{
				                            xtype: 'combobox',
											fieldLabel: 'Группа',
											name: 'Group',
											store: groupLogistMobileStore,
											minChars: 0,
											queryMode: 'remote',
											valueField: 'value',
											displayField: 'value',
											hideTrigger: true, 
											typeAhead: true,
											triggerAction: 'query',
											lazyRender: true,
											editable: true,
											anchor: '100%',
				                            allowBlank: false
										},
										{
				                            xtype: 'textfield',
											anchor: '100%',
				                            fieldLabel: request_toir.name,
				                            allowBlank: false,
				                            name: 'Name'
				                        },
				                        {
				                            xtype: 'textfield',
											anchor: '100%',
				                            fieldLabel: 'Стандарт',
				                            name : 'Cost1'
				                        },
										{
				                            xtype: 'textfield',
											anchor: '100%',
				                            fieldLabel: 'Мастер',
				                            name : 'Cost2'
				                        },
										{
				                            xtype: 'textfield',
											anchor: '100%',
				                            fieldLabel: 'Премиум',
				                            name : 'Cost3'
				                        },
										{
				                            xtype: 'textfield',
											anchor: '100%',
				                            fieldLabel: 'Штрих-код',
				                            name : 'barcode'
				                        }

                    ],
                    buttons:[
                    {
                        //text:'Cохранить',
                        text: all_toir.save,
                        iconCls:'save',
                        handler: function() {

                            var form = this.up('form').getForm();
                           // var time = Ext.getCmp('date_execute_id').getRawValue();
                            if(form.isValid()){
                                
									form.submit({
                                    waitMsg: all_toir.loading,
                                    url: 'app/LogistMobile_CRM_cargo.php',
                                    wait: true,
                                    scope: this,
                                    method: 'post',
                                    params: {func:'addedit',id:id},
                                    success: function(fp, o){
									if(o.result.success == true){
										Ext.MessageBox.alert(all_toir.attention, 'Сохранено');
										LogistMobile_cargo_WindowAddEdit.destroy();
										cargoLogistMobileStore.load();
									}else{
										Ext.MessageBox.alert(all_toir.error, o.result.message);
									}
									
									
                                    },
                                    failure: function(fp, o){
										Ext.MessageBox.alert(all_toir.error, o.result.message);
									}
                                });

                            }
                        }
                    },
                    {
                        //text: 'Отмена',
                        text: all_toir.cancel,
                        iconCls:'cancel',
                        handler: function(){
                            LogistMobile_cargo_WindowAddEdit.destroy();
                        }
                    }
                ]
                }
            ]
        });
    }
    LogistMobile_cargo_WindowAddEdit.show();
    var form = Ext.getCmp("LogistMobile_cargo_RequestFormId").getForm();
    if (id) {
		form.findField("Group").setValue( records.get('NameGroup') );
		form.findField("Name").setValue( records.get('NameCargo') );
		form.findField("Cost1").setValue( records.get('Cost1') );
		form.findField("Cost2").setValue( records.get('Cost2') );
		form.findField("Cost3").setValue( records.get('Cost3') );
		form.findField("barcode").setValue( records.get('barcode') );
    }
}