var TOiR_EvaluationCriteria_Window = null;
var TOiR_EvaluationCriteria_WindowAdd = null;
var TOiR_EvaluationCriteria_WindowEdit = null;

 Ext.define('EvaluationCriteriaModel', {
    extend: 'Ext.data.Model',
    fields: ['id_evaluation_criteria', 'name_evaluation_criteria']
  });

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


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

 
	EvaluationCriteriaGrid = Ext.create('Ext.grid.Panel', {
		selType: 'rowmodel',
		columnLines: true,
		id: 'TOiR_EvaluationCriteriaGridId',
		store: TOiR_EvaluationCriteriaStore,
		loadMask: true,
    border: false,
    autoScroll: true,
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            //text: 'Добавить',
            text: all_toir.add,
            iconCls: 'add',
            handler: function(){
                show_TOiR_EvaluationCriteria_WindowAdd();
              }
            },
            , '-', {
            //text: 'Удалить',
            text: all_toir.del,
            iconCls: 'delete',
            handler: function() {
                show_TOiR_EvaluationCriteria_WindowRemove();
              }
            }
        ],
    }],
    columns: [
    	{
        //text: 'Id Оценочного критерия',
        text: eval_crit_toir.id_evaluation_criteria, 
        dataIndex: 'id_evaluation_criteria',
        hideable: false,
        sortable: true,
        hidden:true
      },
      {
        //text: 'Оценочные критерии',
        text: eval_crit_toir.evaluation_criteria, 
        dataIndex: 'name_evaluation_criteria',
        flex:1,
        hideable: false,
        sortable: true
      }
    ],
    listeners: {
    	itemdblclick: function (View,record,item,index,e,eOpts) {
        var name = record.get('name_evaluation_criteria');
        var id = record.get('id_evaluation_criteria');
    		show_TOiR_EvaluationCriteria_WindowEdit(id, name);
    	}
    }
	});

  if (!TOiR_EvaluationCriteria_Window){
        Ext.getBody().mask(all.loading);
        TOiR_EvaluationCriteria_Window = new Ext.Window({
            layout : 'fit',
            //title : 'Критерии',
            title : TOiR.EvaluationCriteria,
            closeAction : 'destroy',
            width : parseInt(Wwidth),
            height : parseInt(Wheight),
            //minimizable: true,
            // constrainHeader: true,
            maximizable:true,
            // x: parseInt(Xcoord),
            // y: parseInt(Ycoord),
            listeners : {
                destroy : function(){
                    TOiR_EvaluationCriteria_Window = null;   
                }
            },
            items : [EvaluationCriteriaGrid]
        });  
    }

	function show_TOiR_EvaluationCriteria_WindowAdd() {
		if (!TOiR_EvaluationCriteria_WindowAdd) {
            TOiR_EvaluationCriteria_WindowAdd = new Ext.Window({
                width: 300,
                height: 150,
                //title: 'Новый критерий',
                title: eval_crit_toir.new_criterion,
                closeAction: 'destroy',
                layout: 'fit',
                resizable: true,
                modal:true,
                listeners : {
                    destroy : function(){
                    TOiR_EvaluationCriteria_WindowAdd = null;    
                    }
                },
                items: [{
                    xtype: 'form',
                    autoScroll: true,
                    bodyPadding: '5 5 5 5',
                    id: "TOiR_EvaluationCriteriaFormId",
                    border: false,
                    frame: true,
                    autoHeight: true,
                    layout: {
                        type: 'vbox',
                        align: 'stretch'
                    },
                    // defaults: {
                    //     labelAlign: 'left',
                    //     labelWidth: 90,
                    //     msgTarget: 'side'
                    // },
                    items: [
                	{
                    xtype:'textfield',
                    //fieldLabel: 'Критерий',
                    fieldLabel: TOiR.EvaluationCriteria,
                    name : 'name_evaluation_criteria',
                    allowBlank: false
                	}],
                	buttons: [
                    	{
                      	//text: 'Добавить',
                      	text: all_toir.add,
                      	handler: function()
                        	{
                          	var form = this.up('form').getForm();
                              if(form.isValid())
                              {
                                form.submit(
                                {
                                  //waitMsg:'Идет отправка...',
                                  waitMsg: all_toir.loading,
                                  url: 'app/TOiR_EvaluationCriteria.php?func=add',
                                  params : {key : 'val', key2 : 'val2'},
                                  success: function(form,response) 
                                  {
                                    // Ext.Msg.alert('Внимание','Оценочный критерий успешно добавлен'); 
                                    TOiR_EvaluationCriteria_WindowAdd.destroy();
                                    Ext.getCmp('TOiR_EvaluationCriteriaGridId').store.load();
                                    Ext.getCmp('TOiR_EvaluationCriteriaGridId').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_EvaluationCriteria_WindowAdd.destroy();
                                  }
                      }
                    ] 
                }]
                 
            });
            
        }
        TOiR_EvaluationCriteria_WindowAdd.show();
	}
	function show_TOiR_EvaluationCriteria_WindowRemove() {
		if (EvaluationCriteriaGrid) {
      var sm = EvaluationCriteriaGrid.getSelectionModel();
      var rs = sm.getSelection();
      if(!rs) return false;
      var id_evaluation_criteria = rs[0].get('id_evaluation_criteria');
      if (!rs.length) {
          Ext.Msg.alert(all_toir.attention, eval_crit_toir.the_evaluation_criteria_are_not_selected);
          return;
          }
            Ext.Msg.confirm(all_toir.attention, eval_crit_toir.are_you_sure_you_want_to_delete_the_selected_evaluation_criteria, function (button) {
              if (button == 'yes') {
                          Ext.Ajax.request({  
                              url : 'app/TOiR_EvaluationCriteria.php?func=del',
                              method: 'GET',
                              params: {id_evaluation_criteria: id_evaluation_criteria}, 
                              success: function(objServerResponse){  
                                   }
                              });
                           EvaluationCriteriaGrid.store.remove(rs[0]);
                                    }
              });
            }   
	}

	function show_TOiR_EvaluationCriteria_WindowEdit(id, name) {
		if (!TOiR_EvaluationCriteria_WindowEdit) {
			TOiR_EvaluationCriteria_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_EvaluationCriteria_WindowEdit = null;	
					}
				},
                items:[
				{
					xtype: 'form',
					autoScroll: true,
					bodyPadding: '5 5 5 5',
					id: "TOiR_EvaluationCriteriaFormId",
					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_evaluation_criteria',
                        value: id
                      },
                      {
                        xtype:'textfield',
                        //fieldLabel: 'Оценочный критерий',
                        fieldLabel: eval_crit_toir.evaluation_criteria,
                        name : 'name_evaluation_criteria',
                        value: name
                      }
                    ],
                    buttons: [{  
                        //text: 'Сохранить',
                        text: all_toir.save,
                        handler: function() {
                            var form = Ext.getCmp('TOiR_EvaluationCriteriaFormId').getForm();
                            if(form.isValid()) {
                            	form.submit({
                              		//waitMsg:'Идет отправка...',
                              		waitMsg: all_toir.loading,
                              		url: 'app/TOiR_EvaluationCriteria.php?func=update',
                              		//params : {key : 'val', key2 : 'val2'},
                              		success: function(form,response) {
                                        // Ext.Msg.alert('Внимание','Оценочный критерий успешно изменен'); 
                                        TOiR_EvaluationCriteria_WindowEdit.destroy();
                                        Ext.getCmp('TOiR_EvaluationCriteriaGridId').store.load();
                                        Ext.getCmp('TOiR_EvaluationCriteriaGridId').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_EvaluationCriteria_WindowEdit.destroy();
                        }
                  	}] 
                }]
			});
		}
        TOiR_EvaluationCriteria_WindowEdit.show();     
	}
  // console.log(TOiR_EvaluationCriteria_Window);
  TOiR_EvaluationCriteria_Window.show();
  Ext.getBody().unmask(all.loading);
}