'use strict';

var vm = new Vue({
  el: '#addInstance',
  methods : {

  	displayFormAddInstance : function (event) {
      document.getElementById('modal-add-instance').style.display = 'block';
    },

  	cancelAddInstance : function (event) {
      location.reload();
  	},

  	addInstance : function (event) {
  		this.longName = document.getElementById('inputLongName').value;
  		this.project = document.getElementById('inputProject').value;
  		this.study = document.getElementById('inputStudy').value;
  		this.version = document.getElementById('inputVersion').value;

			document.getElementById('save').style.display = 'none';
			document.getElementById('close_modal').style.display = 'none';
			document.getElementById('loader').style.display = 'block';

      if (this.longName == '') { this.longName = 'Free comment of '+this.technicalName; }
  		
      var data = {
  			'longName' : this.longName,
  			'project' : this.project,
  			'version' : this.version,
  			'study': this.study,
  			'technicalName' :  this.technicalName,
  			'app' : document.getElementById('app').value
  		}
  		this.$http.post('/-/v1/instances', data).then(function (result) {
  			if (result.status == 200) { location.reload(); }
  		}, function (error) {
        if (error.status == 400) {
          this.codeErrorPull = error.status;
          this.messageErrorPull = error.data;
          document.getElementById('loader').style.display = 'none';
          document.getElementById('errorLoader').style.display = 'block';
        }
      });
  	},

    tryAgain : function (event) {
      location.reload();
    }
  },
  data : {
  	longName : '',
  	project: '',
  	version : '',
  	study : '',
  	technicalName : '',
    urlPreview : '',
    messageErrorPull : '',
    codeErrorPull : ''
  }
});

module.exports = vm;


vm.$watch('longName', function(data) {

      var value = document.forms["Form"]["inputLongName"].value;

      if (value == "")
        document.getElementById('inputLongName').style.backgroundColor='#FFCDD2';
      else
        document.getElementById('inputLongName').style.backgroundColor='#C5E1A5';

});




vm.$watch('study', function(data) {

      var value = document.forms["Form"]["inputStudy"].value;

      if (/^[a-z0-9]+$/.test(value)==false || value == "")
        document.getElementById('inputStudy').style.backgroundColor='#FFCDD2';
      else
        document.getElementById('inputStudy').style.backgroundColor='#C5E1A5';

});


vm.$watch('project', function(data) {

      var value = document.forms["Form"]["inputProject"].value;

      if (/^[a-z0-9]+$/.test(value)==false || value == "")
        document.getElementById('inputProject').style.backgroundColor='#FFCDD2';
      else
        document.getElementById('inputProject').style.backgroundColor='#C5E1A5';

});


vm.$watch('project', function(data) {
  this.project = data;

  if (this.version == '') { this.technicalName = this.project + '-' + this.study; }
  else { this.technicalName = this.project + '-' + this.study + '-' + this.version; }

  this.urlPreview = 'http://'+this.technicalName;
  verif(this.technicalName);
});

vm.$watch('study', function (data) {
  this.study = data;

  if (this.version == '') { this.technicalName = this.project + '-' + this.study; }
  else { this.technicalName = this.project + '-' + this.study + '-' + this.version; }

  this.urlPreview = 'http://'+this.technicalName;
  verif(this.technicalName);
});

vm.$watch('version', function (data) {
  this.version = data;

  if (this.version == '') { this.technicalName = this.project + '-' + this.study; }
  else { this.technicalName = this.project + '-' + this.study + '-' + this.version; }

  this.urlPreview = 'http://'+this.technicalName;
  verif(this.technicalName);
});

function verif (tn) {
  var data = {
    'technicalName' : tn
  }
  vm.$http.get('/-/v1/instances/verif', data).then(function (result) {
    if (result.status == 200) {  
      document.getElementById('technicalNameExists').style.display = 'none';
      document.getElementById('save').style.display = 'block';
      document.getElementById('saveTechnicalExists').style.display = 'none';
    }
  }, function (error) {
    if (error.status == 409) { 
      document.getElementById('technicalNameExists').style.display = 'block';
      document.getElementById('save').style.display = 'none';
      document.getElementById('saveTechnicalExists').style.display = 'block';
    }
  });
}
@@ -66,17 +66,18 @@
  : %7B%0A%0A  
-%09
+  
 displayF
@@ -199,17 +199,18 @@
   %7D,%0A%0A  
-%09
+  
 cancelAd
@@ -849,20 +849,16 @@
 Name; %7D%0A
-  %09%09
 %0A      v
@@ -1089,16 +1089,17 @@
 ue%0A  %09%09%7D
+;
 %0A  %09%09thi
@@ -1878,33 +1878,33 @@
 ument.forms%5B
-%22
+'
 Form
-%22%5D%5B%22
+'%5D%5B'
 inputLongNam
@@ -1904,17 +1904,17 @@
 LongName
-%22
+'
 %5D.value;
@@ -1926,34 +1926,34 @@
    if (value == 
-%22%22
+''
 )%0A        docume
@@ -2188,33 +2188,33 @@
 ument.forms%5B
-%22
+'
 Form
-%22%5D%5B%22
+'%5D%5B'
 inputStudy%22%5D
@@ -2211,17 +2211,17 @@
 putStudy
-%22
+'
 %5D.value;
@@ -2269,34 +2269,34 @@
 lse %7C%7C value == 
-%22%22
+''
 )%0A        docume
@@ -2533,17 +2533,17 @@
 rms%5B
-%22
+'
 Form
-%22%5D%5B%22
+'%5D%5B'
 inpu
@@ -2550,17 +2550,17 @@
 tProject
-%22
+'
 %5D.value;
@@ -2620,10 +2620,10 @@
  == 
-%22%22
+''
 )%0A  

fix bithound