var LogistMobile_Reasons_Window = null;
var LogistMobile_Reasons_WindowAdd = null;
var LogistMobile_Reasons_WindowEdit = null;


Ext.define('ReasonsModel', {
	    extend: 'Ext.data.Model',
	    fields: ['id', 'name']
	});

	var ReasonsStore = new Ext.data.Store(
	  {
	  	//autoLoad: true,
	    // autoDestroy: true,
	    model: 'ReasonsModel',
	    proxy: 
	    {
	      type: 'ajax',
	      url: 'app/LogistMobile_reasons.php?func=show',
	      reader: 
	      {
	        type: 'json',
	        root: 'rows'
	      }
	    }
	});

function show_LogistMobile_reasons_Window(Xcoord,Ycoord,Wwidth,Wheight){
	
	ReasonsStore.load();
	
	Xcoord = Xcoord ? Xcoord :40;
    Ycoord = Ycoord ? Ycoord :60;  
    Wwidth = Wwidth ? Wwidth :700;  
    Wheight = Wheight ? Wheight :400;

	
	ReasonsGrid = Ext.create('Ext.grid.Panel', {
		selType: 'rowmodel',
		columnLines: true,
		id: 'LogistMobile_Reasons_GridId',
		store: ReasonsStore,
		loadMask: true,
	    border: false,
	    autoScroll: true,
	    dockedItems: 
	    [
		    {
		        xtype: 'toolbar',
		        items: 
		        [
					{
			            //text: 'Добавить',
						text: all_toir.add,
			            iconCls: 'add',
			            handler: function(){
			                show_LogistMobile_reasons_WindowAdd();
			            }
		            },'-',
		            {
			            //text: 'Удалить',
						text: all_toir.del,
			            iconCls: 'delete',
			            handler: function() {
			                show_LogistMobile_reasons_WindowRemove();
			            }
		            },'-',
		            {
	                  //text: 'Редактировать',
	                  text: all_toir.edit,
	                  iconCls: 'edit',
	                  handler: function (){
						var select = Ext.getCmp('LogistMobile_Reasons_GridId').getView().getSelectionModel().getSelection()[0];
						if (select) {
							var name = select.get('name');
							var id = select.get('id');
							show_LogistMobile_reasons_WindowEdit(id, name);
						}
					  }
					}
		        ],
		    }
	    ],
	    columns: 
	    [
	    	{
		        //text: 'Название', 
				text : reports.name,
		        dataIndex: 'name',
		        hideable: false,
		        sortable: true,
		        width:'95%'
		    }
	    ],
	    listeners: {
	    	itemdblclick: function (View,record,item,index,e,eOpts) {
				var name = record.get('name');
				var id = record.get('id');
	    		show_LogistMobile_reasons_WindowEdit(id, name);
	    	}
	    }
	});

	if (!LogistMobile_Reasons_Window) 
	{
		LogistMobile_Reasons_Window = new Ext.Window({
			layout : 'fit',
            //title : 'Причины отказа',
			title : reasons_for_refusal.reasons_for_refusal,
            closeAction : 'destroy',
            width : parseInt(Wwidth),
            height : parseInt(Wheight),
            // constrainHeader: true,
            maximizable:true,
            listeners : {
                destroy : function(){
                    LogistMobile_Reasons_Window = null;   
                }
            },
            items : [ReasonsGrid]
		});
	}

	function show_LogistMobile_reasons_WindowAdd() {

    	if (!LogistMobile_Reasons_WindowAdd) 
    	{
			LogistMobile_Reasons_WindowAdd = new Ext.Window({
				width: 300,
                height: 120,
                modal:true,
                //title: 'Новая причина',
				title: reasons_for_refusal.new_reason,
                closeAction: 'destroy',
                layout: 'fit',
                resizable: false,
                listeners : {
                    destroy : function(){
                    	LogistMobile_Reasons_WindowAdd = null;    
                    }
                },
                items: 
                [
	                {
	                    xtype: 'form',
	                    autoScroll: true,
	                    bodyPadding: '5 5 5 5',
	                    id: "LogistMobile_Reasons_FormId",
	                    border: false,
	                    frame: true,
	                    autoHeight: true,
	                    layout: {
	                        type: 'vbox',
	                        align: 'stretch'
	                    },
	                    // defaults: {
	                    //     labelAlign: 'left',
	                    //     labelWidth: 90,
	                    //     msgTarget: 'side'
	                    // },
	                    items: 
	                    [
		                	{
		           			    xtype:'textfield',
		                        //fieldLabel: 'Название',
								fieldLabel: reports.name,
		                        name : 'name',
		                        allowBlank: false
		                	}
	                	],
	                	buttons: 
	                	[
		                	{  
				              //text: 'Добавить',
				              text: all_toir.add,
				              handler: function()
				                {
				                  var form = Ext.getCmp('LogistMobile_Reasons_FormId').getForm();
				                      if(form.isValid())
				                      {
				                        form.submit({
					                          //waitMsg:'Идет отправка...',
											  waitMsg: all_toir.loading,
					                          url: 'app/LogistMobile_reasons.php?func=add',
					                          //params : {key : 'val', key2 : 'val2'},
					                          success: function(form,response) 
					                          {
					                            // Ext.Msg.alert('Внимание','Успешно добавлено'); 
					                            LogistMobile_Reasons_WindowAdd.destroy();
					                            Ext.getCmp('LogistMobile_Reasons_GridId').store.load();
					                            Ext.getCmp('LogistMobile_Reasons_GridId').getView().refresh();
					                          },
					                          failure : function()
					                          {
					                            Ext.Msg.alert(all_toir.attention, all_toir.error); 
					                          }
				                        });
				                      }else
				                        {
				                          Ext.Msg.alert(all_toir.attention , all_toir.required_fields); 
				                        }
				                }
				            },
				            {
				                //text: 'Отмена',
								text: all_toir.cancel,
				                handler: function()
				                          {
				                            LogistMobile_Reasons_WindowAdd.destroy();
				                          }
				            }
		            	]
		        	}
           		]
			});
		}
		LogistMobile_Reasons_WindowAdd.show();
		
	}
	function show_LogistMobile_reasons_WindowRemove() {
		if (ReasonsGrid) 
		{
    		var sm = ReasonsGrid.getSelectionModel();
    		var rs = sm.getSelection();
        	if(!rs) return false;
        	var id = rs[0].get('id');
	        if (!rs.length) 
	        {
	            Ext.Msg.alert(all_toir.attention, 'Выберите строку для удаления');
	            return;
	        }
        	Ext.Msg.confirm(all_toir.attention, 'Вы действительно хотите удалить?', function (button) {
		            if (button == 'yes') 
		            {
				          Ext.Ajax.request({  
				              url : 'app/LogistMobile_reasons.php?func=del',
				              method: 'GET',
				              params: {id: id}, 
				              success: function(objServerResponse){  
				                   }
				              });
				           ReasonsGrid.store.remove(rs[0]);
		            }
              });
        }
	}
	function show_LogistMobile_reasons_WindowEdit(id, name, unit) {
		if (!LogistMobile_Reasons_WindowEdit) 
		{
			LogistMobile_Reasons_WindowEdit = new Ext.Window({
				width:300,
                height:120,
                //title: 'Редактировать',
				title: all_toir.edit,
                closeAction : 'destroy',
                layout:'fit',
                resizable: true,
				constrainHeader: true,
                modal:true,
				listeners : {
				destroy : function(){
					LogistMobile_Reasons_WindowEdit = null;	
					}
				},
                items:
                [
					{
						xtype: 'form',
						autoScroll: true,
						bodyPadding: '5 5 5 5',
						id: "LogistMobile_Reasons_FormId",
						border: false,
						frame: true,
						autoHeight: true,
						//fieldwidth:'width:150px',
						//layout: 'anchor',
						layout: {
							type: 'vbox',
							align: 'stretch'
						},
						// defaults: {
						// 	//fileUpload: true,
						// 	labelAlign: 'left',
						// 	labelWidth: 90,
						// 	//fieldWidth: '90%',
						// 	msgTarget: 'side'
						// },
						items: 
						[
							{
		                      xtype:'hiddenfield',
		                      name : 'id',
		                      value: id
		                    },
		                    {
		                      xtype:'textfield',
		                      fieldLabel: 'Название',
							  //fieldLabel: job_toir.type_of_work,
		                      name : 'name',
		                      value: name
		                    }
						],
	                    buttons: 
	                    [
		                    {  
		                        //text: 'Сохранить',
								text: all_toir.save,
		                        handler: function() {
		                            var form = Ext.getCmp('LogistMobile_Reasons_FormId').getForm();
		                            if(form.isValid()) 
		                            {
		                            	form.submit({
		                              		//waitMsg:'Идет отправка...',
											waitMsg: all_toir.loading,
		                              		url: 'app/LogistMobile_reasons.php?func=update',
		                              		// params : {key : 'val', key2 : 'val2'},
		                              		success: function(form,response) {
		                                        // Ext.Msg.alert('Внимание','Тип работы успешно изменен'); 
		                                        LogistMobile_Reasons_WindowEdit.destroy();
		                                        Ext.getCmp('LogistMobile_Reasons_GridId').store.load();
		                                        Ext.getCmp('LogistMobile_Reasons_GridId').getView().refresh();
		                             		},
		                                    failure : function() {
		                                        Ext.Msg.alert(all_toir.attention, all_toir.error); 
		                                    }
		                            	});
		                          	} else 
		                          	{
		                              Ext.Msg.alert(all_toir.attention, all_toir.required_fields); 
		                            }
		                        }
		                    },
		                    {
		                    	//text: 'Отмена',
								text: all_toir.cancel,
		                    	handler: function() {
		                            LogistMobile_Reasons_WindowEdit.destroy();
		                        }
		                  	}
	                  	] 
	                }
                ]
			});
		}
		LogistMobile_Reasons_WindowEdit.show();
	}
LogistMobile_Reasons_Window.show();
}