var TOiR_TypeEquipment_Window = null;
var TOiR_TypeEquipment_WindowAdd = null;

function show_TOiR_TypeEquipment_Window(Xcoord,Ycoord,Wwidth,Wheight){
  Xcoord = Xcoord ? Xcoord :40;
  Ycoord = Ycoord ? Ycoord :60;  
  Wwidth = Wwidth ? Wwidth :600;  
  Wheight = Wheight ? Wheight :400;

  Ext.define('EquipmentModel',{
    extend: 'Ext.data.Model',
    fields: ['id','type','service','counter','days','idt']
  });

  var TypeEquipmentStore = new Ext.data.Store(
    {
        remoteSort: true,
	    autoLoad: true,
		autoDestroy: true,
	    pageSize: 50,
		groupField: 'type',
        model: 'EquipmentModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_TypeEquipment.php?func=show',
          reader: 
          {
            type: 'json',
            root: 'rows',
            totalProperty: 'results'
          },
			  simpleSortMode: true
			 
		},sorters: [{
				property: 'type',
				direction: 'ASC'
			}]
  });
	
	

 
  
  // <---- Paging
  var TypeEquipmentPagingToolbar = new Ext.PagingToolbar({      
      displayInfo: true,
      store: TypeEquipmentStore,
	  border: false,
      frame: false
  });
 // Paging  ---->
  
  TypeEquipmentGrid = Ext.create('Ext.grid.Panel', {
    selType: 'rowmodel',
	layout: 'fit',
	columnLines: true,
    border: false,
    frame: false,
	stripeRows: true,
    id: 'TypeEquipmentGridId',
    features: [{
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: false,
            enableGroupingMenu: false
        }],
	
	store: TypeEquipmentStore,
    // features: [EquipmentFilters],
    loadMask: true,
    autoScroll: true,
    bbar: [TypeEquipmentPagingToolbar],
    dockedItems: [{
            xtype: 'toolbar',
            items: [
			{
                //text: 'Добавить',
                text: all_toir.add,
                iconCls: 'add',
                handler: function(){
                  show_TOiR_TypeEquipment_WindowAdd();
                }
            }
			, '-', {
                //text: 'Удалить',
                text: all_toir.del,
                iconCls: 'delete',
                handler: function(){
                  var select = Ext.getCmp('TypeEquipmentGridId').getView().getSelectionModel().getSelection()[0];
				  if (select) {
							Ext.MessageBox.confirm(all_toir.attention,mess_toir.are_you_sure_you_want_to_delete_the_record, function(btn){
							if(btn=='yes'){
										Ext.Ajax.request({
                                            url: 'app/TOiR_TypeEquipment.php',
                                            method: 'POST',
                                            params: {"delete": select.get('id')},
                                            success: function(response) {
                                                Ext.MessageBox.alert(all_toir.skill, all_toir.record_deleted_successfully);
                                                Ext.getCmp('TypeEquipmentGridId').store.load();
                                            },
                                            failure: function(response) {
                                                Ext.MessageBox.alert(all_toir.error, response.responseText);
                                            }
                                        });
							}
							});
                    } else {
							Ext.MessageBox.alert(all_toir.error, all_toir.select_row);
					}
				  
                }
            },'-',{
                  //text: 'Редактировать',
                  text: all_toir.edit,
                  iconCls: 'edit',
                  handler: function (){
                    var select = Ext.getCmp('TypeEquipmentGridId').getView().getSelectionModel().getSelection()[0];
                     if (select) {
							show_TOiR_TypeEquipment_WindowAdd(select);
                      } else {
							Ext.MessageBox.alert(all_toir.error, all_toir.select_row);
                      }
                    }
            }

			]
    }],
    listeners: 
    {
      itemdblclick: function (View,record,item,index,e,eOpts){
		show_TOiR_TypeEquipment_WindowAdd(record);
	}
                   
    },
    columns:[
	  {
        //text: 'Оборудование',
        text: equip_toir.producer, 
        dataIndex: 'type',
        flex: 1,
		sortable: true,
        filter: {
          type: 'string'
        }
      },
	  {
        //text: 'Плановый техосмотр',
        text: equip_toir.service, 
        dataIndex: 'service',
        width: 120,
        sortable: true,
        filter: {
          type: 'string'
        }
      },
	  {text: equip_toir.columnHead,flex: 1,
       columns: [
      {
        //text: 'Значения счетчика',
        text: equip_toir.counter2, 
        dataIndex: 'counter',
		width: 120,
        sortable: true,
        filter: {
          type: 'string'
        }
      },
      {
        //text: 'Каждие',
        text: equip_toir.days, 
        dataIndex: 'days',
        width: 70,
		sortable: true,
        filter: {
          type: 'string'
        }
      }
      ]
	  }
    ]
  });

  if(!TOiR_TypeEquipment_Window){
    TOiR_TypeEquipment_Window = new Ext.Window({
    //title : 'Оборудование',
	title : TOiR.TypeEquipment,
	closeAction : 'destroy',
	width : parseInt(Wwidth),
	height : parseInt(Wheight),
	layout: 'fit',
	resizable: true,
	closable: true,
	constrainHeader: true,
    listeners: {
          destroy: function(){
              TOiR_TypeEquipment_Window = null;   
          } 
    },
    items : [TypeEquipmentGrid]
    });
  }
  TOiR_TypeEquipment_Window.show();
 
  
}



// -- window add type equipment -->
  function show_TOiR_TypeEquipment_WindowAdd(record) {
    
	Ext.define('TypeEquipmentComboModel', {
    extend: 'Ext.data.Model',
    fields: ['id',{name: 'type', type: 'string'}]
});
var TypeEquipmentComboStore = new Ext.data.JsonStore({
		autoLoad: true,
        model: 'TypeEquipmentComboModel',
        proxy: 
        {
          type: 'ajax',
          url: 'app/TOiR_TypeEquipment.php?func=showTypeEquipmentCombo',
          reader: 
          {
            type: 'json',
            root: 'rows',
          }
        }
});
	
	var params = {};
	if(record){ 
		params = {edit:true,id:record.get('id')};
		titlewin = equip_toir.add_typeequipment;
		type = record.get('type');
		service = record.get('service');
		counter = record.get('counter');
		days = record.get('days');
	}else{
		titlewin = equip_toir.edit_typeequipment;
		type = '';
		service = 'ТО1';
		counter = '';
		days = '';
	}
		
	
	if(!TOiR_TypeEquipment_WindowAdd) {
      TOiR_TypeEquipment_WindowAdd = new Ext.Window({
        autoScroll:true,
        modal:true,
        closeAction : 'destroy',
        width: 350,
        //title : 'Добавить оборудование',
        title : titlewin,
        listeners : {
            destroy : function(){
            TOiR_TypeEquipment_WindowAdd = null;    
            }
        },
        items: [{
          xtype: 'form',
          autoScroll: true,
          bodyPadding: '5 5 5 5',
          id: "TypeEquipmentFormId",
          bodyPadding: 5,
          border: false,
          frame: true,
          autoHeight: true,
          layout: {
              type: 'vbox',
              align: 'stretch'
          },
          // defaults: {
          //     labelAlign: 'left',
          //     labelWidth: 90,
          //     msgTarget: 'side'
          // },
			items: [
                        {
						  xtype: 'combobox',
                          fieldLabel: equip_toir.producer,
                          name : 'type',
						  //autoSelect:true,
                          store: TypeEquipmentComboStore,
                          valueField:'id',
                          displayField:'type',
                          queryMode: 'local',
						  //querrParm:'equipment',
                          typeAhead: true,
                          //typeAheadDelay: 100
                        },
						
						/*{
						xtype:'textfield',
						name : 'type',
						fieldLabel: equip_toir.producer,
						allowBlank: false,
						value: type
						},*/
						
						{
						xtype:'textfield',
						name : 'service',
						fieldLabel: equip_toir.service,
						allowBlank: false,
						value: service
						},
						{
						xtype:'textfield',
						name : 'counter',
						fieldLabel: equip_toir.counter2,
						//allowBlank: false,
						enableKeyEvents: true,
						maskRe: /^[0-9]+$/,
						minLength: 1,
						maxLength: 99999,
						enforceMaxLength: true,
						value: counter
						},
						{
						xtype:'textfield',
						name : 'days',
						fieldLabel: equip_toir.days,
						allowBlank: false,
						enableKeyEvents: true,
						maskRe: /^[0-9]+$/,
						minLength: 1,
						maxLength: 999,
						enforceMaxLength: true,
						value: days
						}
						
                        
			],
                      buttons: 
                      [
                        {  // Набор кнопок. Здесь она всего одна.
                          //text: 'Добавить',
                          text: all_toir.add,
                          handler: function()
                            {
                              var form = Ext.getCmp('TypeEquipmentFormId').getForm();
                                  if(form.isValid())
                                  {
                                    form.submit(
                                    {
                                      //waitMsg:'Идет отправка...',
                                      waitMsg: all_toir.loading,
                                      wait: true,
									  url: 'app/TOiR_TypeEquipment.php?func=add',
                                      params: params,
                                      success: function(form,response) 
                                      {
                                        // Ext.Msg.alert('Внимание','Успешно добавлено'); 
                                        TOiR_TypeEquipment_WindowAdd.destroy();
                                        Ext.getCmp('TypeEquipmentGridId').store.load();
                                        Ext.getCmp('TypeEquipmentGridId').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_TypeEquipment_WindowAdd.close();
                                      }
                    }] 
        }]
      });
    }
    TOiR_TypeEquipment_WindowAdd.show();
  }
// <-- window add type equipment --