'use strict'
var md = md || {};
var $ = $ || jQuery;
md.u = 'http://maildigit.ndda.fr';
if (location.href.indexOf('https:')>-1){
  md.u='https://maildigit.ndda.fr';
}
//survey.js doit être installé sur la page qui heberge les declencheurs
md.questio = function(quest,iduser){

  function sendDataToServer(survey) {
    logger.info(survey)
    $.ajax({url:md.u+'/questiodone',type: "POST", data:{'questio':survey.data,'quest':quest,'iduser':iduser}, cache: false,
           success:function(e){
             logger.info(e.msg)
           }
    });
  }
  $.ajax({ url: md.u+"/questiotodo", type: "GET",
           data:{quest:quest,iduser:iduser}, cache: false,
           success: function(e) {
              logger.info(e);
              if (e.surveyjson) {
                logger.info('survey')
                logger.info(e.surveyjson)
                var survey = new Survey.Model(e.surveyjson);
                $("#modalsurvey .survey").Survey({
                       model:survey,
                       onComplete:sendDataToServer
                });
                $('#modalsurvey').modal('show');
              }
           },
            error: function(e) {
              logger.info(e);
           },
  });
}
$(document).ready(function(){
   $('[data-tgsurvey][data-tgid]').on('click',function(){
      logger.info($(this).data('tgsurvey')+"-"+$(this).data('tgid'))
      //pour tester enlever cette ligne pour la prod
      var idt = $(this).data('tgid')
      idt = $("input.testid").val()
      md.questio($(this).data('tgsurvey'),idt);
   });
   $('[data-tgsurvey][data-tgid]').on('load',function(){
      md.questio($(this).data('tgsurvey'),$(this).data('tgid'));
   });
});
