/*----------------------------------------------------------------------------------|  www.vdm.io  |----/
				Lmskaran 
/-------------------------------------------------------------------------------------------------------/

	@version		1.0.77
	@build			6th April, 2022
	@created		22nd July, 2020
	@package		Moojla
	@subpackage		site.js
	@author			Lmskaran <http://Lmskaran.com>	
	@copyright		Copyright (C) 2015. All Rights Reserved
	@license		GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
  ____  _____  _____  __  __  __      __       ___  _____  __  __  ____  _____  _  _  ____  _  _  ____ 
 (_  _)(  _  )(  _  )(  \/  )(  )    /__\     / __)(  _  )(  \/  )(  _ \(  _  )( \( )( ___)( \( )(_  _)
.-_)(   )(_)(  )(_)(  )    (  )(__  /(__)\   ( (__  )(_)(  )    (  )___/ )(_)(  )  (  )__)  )  (   )(  
\____) (_____)(_____)(_/\/\_)(____)(__)(__)   \___)(_____)(_/\/\_)(__)  (_____)(_)\_)(____)(_)\_) (__) 

/------------------------------------------------------------------------------------------------------*/

/* JS Document */

/***[JCBGUI.joomla_component.javascript.29.$$$$]***/
jQuery(document).ready(function($){
  adsfield = $('#jform_additional_data_source');
  p = adsfield.parent();
  p.append('<button class="btn btn-small" type="button" id="setuesrmap"><span>Set User maps</span></button>');

  $('#setuesrmap').click(function(e) {
    var ads = $('#jform_additional_data_source').val();
    question = 'Do you want to apply these settings? Note that all of user field mapping have been deleted after this process and you can\'t rollback this process';
    if(ads == 'cb') {
      question += '\nIn This option:\n\"Fistname\" and "Lastname" Mappings will be add to \"User fields map\"!';
    }
    else if(ads == 'jf')
    {
      question += '\nIn This option:\n Name in joomla maps to concat of firstname and lastname of moodle';
    }
    question += '\nother user maping must add manualy';
    if (confirm(question)) {
      var token = Joomla.optionsStorage['csrf.token'];

      $.ajax({
        url: '/administrator/index.php?option=com_moojla',
        method: 'post',
        dataType: 'json',
        data: {[token] : '1', task:'ajax.addMapperRows', format: 'json' , raw: 'true' , ads: ads},
        success: function (msg) {
          // Joomla.submitbutton('config.save.component.apply');
          Joomla.JText.load({custom_alert: "Notice"});
          Joomla.renderMessages({'custom_alert': ['Old Mappings removed from "User fields map"']});
          if(ads == 'cb') {
            // Joomla.JText.load({success: "Success"});
            // Joomla.renderMessages({'success': ['"Fistname" and "Lastname" Mappings added to "User fields map"!']});
          }
          else if(ads == 'jf')
          {
            // Joomla.JText.load({custom_alert: "Notice"});
            // Joomla.renderMessages({'custom_alert': ['In this option (Joomla fields) name in joomla maps to concat of firstname and lastname of moodle']});
          }
          Joomla.submitbutton('config.save.component.apply');
        },
        error: function(err) {
        }
      });
    } else {
    }
    //event.preventDefault();
  });

  prefix = 'jform_params_quiz_'; // our naming convension for input fields in plugin that gets quizzes ids is -> quiz_###quiztype###
  // other convension is this field must be custom type and this code snippet should be added in its php area:
  // code snippet:
  // $document = JFactory::getDocument();
  // $document->addScript('components/com_moojla/assets/js/admin.js');

  quizzes = $(`select[id^=${prefix}]`);
  if(quizzes[0]) {
    quiz_type = quizzes[0].id.substr(prefix.length);

    // console.log(quizzes);
    p = quizzes.parent();
    p.append(`<button class="btn btn-small" type="button" id="fetch${quiz_type}attempts"><span>Fetch attempts</span></button>`);
    $(`#fetch${quiz_type}attempts`).click(function(e) {
      var quizids = quizzes.val();
      // console.log(quizids);
      $.ajax({
        url: '/administrator/index.php?option=com_moojla&view=courses',
        method: 'post',
        dataType: 'json',
        data: {task:'courses.fetchQuizAttempts', quizzes_list: quizids, quizType: quiz_type},
        success: function (msg) {
          // console.log('success');
          // console.log(msg);
        }
      });
    });
  }
});/***[/JCBGUI$$$$]***/
