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

	@version		1.0.39
	@build			4th April, 2023
	@created		17th December, 2020
	@package		Reservation
	@subpackage		site.js
	@author			farhad shahbazi <http://farhad.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.28.$$$$]***/
jQuery(document).ready(function($) {
  $(window).on('load', function() {
    var token = Joomla.optionsStorage['csrf.token'];
    var url = $('#jform_serverurl').val();
    var secret = $('#jform_serversecret').val();
    $.ajax({
      url: '/administrator/index.php?option=com_reservation',
      method: 'post',
      dataType: 'json',
      data: {[token] : '1', task:'ajax.isRegistered', format: 'json' , raw: 'true' , url: url, secret: secret},
      success: function (msg) {
        if(msg.status) {
          $('#wlists').parent().addClass('alert-success')
          $('#wlists').text('connected!');
        }
        else {
          $('#wlists').parent().addClass('alert-error')
          $('#wlists').text('not connected!');
        }
      },
      error: function(err) {
      }
    });
  });
  var url = $('#jform_serverurl').val();
  var secret = $('#jform_serversecret').val();
  jsprovider = jQuery('#jform_adminjsprovider');
  jsprovider.parent().parent().remove();
  saltcode = $('#jform_serversecret');
  p = saltcode.parent();
  disable = url && secret ? '' : 'disabled="disabled"';
  p.append(`<button class="btn btn-small" type="button" id="connectwebhook" ${disable}><span>Create WebHook</span></button>`);
  jQuery(document).on('keyup', '#jform_serversecret', function(e) {
    if(this.value && $('#jform_serverurl').val())
      jQuery('#connectwebhook').removeAttr('disabled');
  });
  jQuery(document).on('keyup', '#jform_serverurl', function(e) {
    if(this.value && $('#jform_serversecret').val())
      jQuery('#connectwebhook').removeAttr('disabled');
  });
  $('#connectwebhook').click(function(e) {
    url = $('#jform_serverurl').val();
    secret = $('#jform_serversecret').val();
    token = Joomla.optionsStorage['csrf.token'];
    
    $.ajax({
      url: '/administrator/index.php?option=com_reservation',
      method: 'post',
      dataType: 'json',
      data: {[token] : '1', task:'ajax.setWebHook', format: 'json' , raw: 'true' , url: url, secret: secret},
      success: function (msg) {
        if(msg.status) {
          Joomla.JText.load({success: "Success"});
          Joomla.renderMessages({'success': ['Created']});
          $('#wlists').text('connected!');
          $('#wlists').parent().removeClass('alert-error')
          $('#wlists').parent().addClass('alert-success')
        }
        else {
          Joomla.JText.load({error: "Error"});
          Joomla.renderMessages({'error': ['Not created']});
          $('#wlists').parent().removeClass('alert-success')
          $('#wlists').parent().addClass('alert-error')
          $('#wlists').text('not connected!');
        }
      },
      error: function(err) {
        Joomla.JText.load({error: "Error"});
        Joomla.renderMessages({'error': ['Not created']});
        $('#wlists').parent().removeClass('alert-success')
        $('#wlists').parent().addClass('alert-error')
        $('#wlists').text('not connected!');
      }
    });
    event.preventDefault();
  });
})/***[/JCBGUI$$$$]***/
