var TOiR_TypeJob_Window = null;
var TOiR_TypeJob_WindowAdd = null;
var TOiR_TypeJob_WindowEdit = null;


Ext.define('TypeJobModel', {
	    extend: 'Ext.data.Model',
	    fields: ['id_type_job', 'name_job']
	});

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

function show_TOiR_TypeJob_Window(Xcoord,Ycoord,Wwidth,Wheight){

	Xcoord = Xcoord ? Xcoord :40;
    Ycoord = Ycoord ? Ycoord :60;  
    Wwidth = Wwidth ? Wwidth :700;  
    Wheight = Wheight ? Wheight :400;

	
	TypeJobGrid = Ext.create('Ext.grid.Panel', {
		selType: 'rowmodel',
		columnLines: true,
		id: 'TOiR_TypeJobGridId',
		store: TypeJobStore,
		loadMask: true,
	    border: false,
	    autoScroll: true,
	    dockedItems: [{
	        xtype: 'toolbar',
	        items: [
				{
	            //text: 'Добавить',
				text: all_toir.add,
	            iconCls: 'add',
	            handler: function(){
	                show_TOiR_TypeJob_WindowAdd();
	            }
	            },'-',{
	            //text: 'Удалить',
				text: all_toir.del,
	            iconCls: 'delete',
	            handler: function() {
	                show_TOiR_TypeJob_WindowRemove();
	            }
	            },'-',{
                  //text: 'Редактировать',
                  text: all_toir.edit,
                  iconCls: 'edit',
                  handler: function (){
					var select = Ext.getCmp('TOiR_TypeJobGridId').getView().getSelectionModel().getSelection()[0];
					if (select) {
						var name = select.get('name_job');
						var id = select.get('id_type_job');
						show_TOiR_TypeJob_WindowEdit(id,name);
					}
				  }
				}
	        ],
	    }],
	    columns: [
	    	{
		        //text: 'Id Типа работ', 
				text: job_toir.id_type_of_work, 
		        dataIndex: 'id_type_job',
		        sortable: true,
		        hidden:true
		    },
		    {
		        //text: 'Тип работ', 
				text: plan_toir.type_of_works,
		        dataIndex: 'name_job',
		        flex:1,
		        hideable: false,
		        sortable: true
		    }
	    ],
	    listeners: {
	    	itemdblclick: function (View,record,item,index,e,eOpts) {
				var name = record.get('name_job');
				var id = record.get('id_type_job');
	    		show_TOiR_TypeJob_WindowEdit(id,name);
	    	}
	    }
	});

	if (!TOiR_TypeJob_Window) {
		TOiR_TypeJob_Window = new Ext.Window({
			layout : 'fit',
            //title : 'Типы работ',
			title : TOiR.TypeJob,
            closeAction : 'destroy',
            width : parseInt(Wwidth),
            height : parseInt(Wheight),
            // constrainHeader: true,
            maximizable:true,
            listeners : {
                destroy : function(){
                    TOiR_TypeJob_Window = null;   
                }
            },
            items : [TypeJobGrid]
		});
	}

	function show_TOiR_TypeJob_WindowAdd() {

    	if (!TOiR_TypeJob_WindowAdd) {
			TOiR_TypeJob_WindowAdd = new Ext.Window({
				width: 300,
                height: 150,
                modal:true,
                //title: 'Новый тип работ',
				title: job_toir.a_new_type_of_work,
                closeAction: 'destroy',
                layout: 'fit',
                resizable: true,
                listeners : {
                    destroy : function(){
                    TOiR_TypeJob_WindowAdd = null;    
                    }
                },
                items: [{
                    xtype: 'form',
                    autoScroll: true,
                    bodyPadding: '5 5 5 5',
                    id: "TOiR_TypeJobFormId",
                    border: false,
                    frame: true,
                    autoHeight: true,
                    layout: {
                        type: 'vbox',
                        align: 'stretch'
                    },
                    // defaults: {
                    //     labelAlign: 'left',
                    //     labelWidth: 90,
                    //     msgTarget: 'side'
                    // },
                    items: [
                	{
           			    xtype:'textfield',
                        //fieldLabel: 'Тип работы',
						fieldLabel: job_toir.type_of_work,
                        name : 'name_job',
                        allowBlank: false
                	}],
                	buttons: [{  
		              //text: 'Добавить',
		              text: all_toir.add,
		              handler: function()
		                {
		                  var form = Ext.getCmp('TOiR_TypeJobFormId').getForm();
		                      if(form.isValid())
		                      {
		                        form.submit(
		                        {
		                          //waitMsg:'Идет отправка...',
								  waitMsg: all_toir.loading,
		                          url: 'app/TOiR_TypeJob.php?func=add',
		                          //params : {key : 'val', key2 : 'val2'},
		                          success: function(form,response) 
		                          {
		                            // Ext.Msg.alert('Внимание','Успешно добавлено'); 
		                            TOiR_TypeJob_WindowAdd.destroy();
		                            Ext.getCmp('TOiR_TypeJobGridId').store.load();
		                            Ext.getCmp('TOiR_TypeJobGridId').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()
		                          {
		                            TOiR_TypeJob_WindowAdd.close();
		                          }
		            }
            ]
           }]
			});
		}
		TOiR_TypeJob_WindowAdd.show();
		
	}
	function show_TOiR_TypeJob_WindowRemove() {
		 if (TypeJobGrid) {
          var sm = TypeJobGrid.getSelectionModel();
          var rs = sm.getSelection();
          if(!rs) return false;
          var id_type_job = rs[0].get('id_type_job');
          if (!rs.length) {
              Ext.Msg.alert(all_toir.attention, job_toir.job_type_is_not_selected);
              return;
              }
          Ext.Msg.confirm(all_toir.attention, job_toir.are_you_sure_you_want_to_delete_the_selected_type_of_work, function (button) {
              if (button == 'yes') {
                          Ext.Ajax.request({  
                              url : 'app/TOiR_TypeJob.php?func=del',
                              method: 'GET',
                              params: {id_type_job: id_type_job}, 
                              success: function(objServerResponse){  
                                   }
                              });
                           TypeJobGrid.store.remove(rs[0]);
                                    }
              });
          }
	}
	function show_TOiR_TypeJob_WindowEdit(id,name) {
		if (!TOiR_TypeJob_WindowEdit) {
			TOiR_TypeJob_WindowEdit = new Ext.Window({
				width:500,
                height:200,
                //title: 'Редактировать',
				title: all_toir.edit,
                closeAction : 'destroy',
                layout:'fit',
                resizable: true,
				constrainHeader: true,
                modal:true,
				listeners : {
				destroy : function(){
					TOiR_TypeJob_WindowEdit = null;	
					}
				},
                items:[
				{
					xtype: 'form',
					autoScroll: true,
					bodyPadding: '5 5 5 5',
					id: "TOiR_TypeJobFormId",
					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_type_job',
                      value: id
                    },
                    {
                      xtype:'textfield',
                      //fieldLabel: 'Тип работы',
					  fieldLabel: job_toir.type_of_work,
                      name : 'name_job',
                      value: name
                    }
					],
                    buttons: [{  
                        //text: 'Сохранить',
						text: all_toir.save,
                        handler: function() {
                            var form = Ext.getCmp('TOiR_TypeJobFormId').getForm();
                            if(form.isValid()) {
                            	form.submit({
                              		//waitMsg:'Идет отправка...',
									waitMsg: all_toir.loading,
                              		url: 'app/TOiR_TypeJob.php?func=update',
                              		// params : {key : 'val', key2 : 'val2'},
                              		success: function(form,response) {
                                        // Ext.Msg.alert('Внимание','Тип работы успешно изменен'); 
                                        TOiR_TypeJob_WindowEdit.destroy();
                                        Ext.getCmp('TOiR_TypeJobGridId').store.load();
                                        Ext.getCmp('TOiR_TypeJobGridId').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() {
                            TOiR_TypeJob_WindowEdit.destroy();
                        }
                  	}] 
                }]
			});
		}
		TOiR_TypeJob_WindowEdit.show();
	}
TOiR_TypeJob_Window.show();

   

}