(function ($, window, document, undefined) {

  $inviteButton = $('#sangomaconnect-invite-user'),
  $dynamicModal = $('#dynamic-modal'),
  $advancedNetwork = $('#advanced-network'),
  $advancedNetworkForm = $('#advanced-network-form'),
  $advancedNetworkEnableBtn = $('#sangomaconnect-adv-network1'),
  $sangomaconnectEnableBtn = $('#sangomaconnect1'),
  $sangomaconnectDisableBtn = $('#sangomaconnect2')
  $advancedNetworkDisableBtn = $('#sangomaconnect-adv-network2');
  $inviteButton.on('click', function () {
    if ($('#sangomaconnect_errors').val() !== '') {
      let $this = $(this),
        $icon = $(".fa-envelope-o", $this),
        id = $this.attr('data-id');
      $icon.addClass("animate");
      $this.attr('disabled', true);
      $.ajax({
        url: "ajax.php",
        type: "POST",
        data: { command: "generateTmpPwd", module: "sangomaconnect", user: id },
        success: (result) => {
          $icon.removeClass("animate");
          $this.attr('disabled', false);
          if (result.status) {
            $(".dynamic", $dynamicModal).hide();
            $(".modal-body.server-success p", $dynamicModal).html("Invitation has been sent to the user");
            $(".server-success", $dynamicModal).show();
            $dynamicModal.modal("show");
          } else {
            $(".dynamic", $dynamicModal).hide();
            $(".modal-body.server-error p", $dynamicModal).html(result.message);
            $(".server-error", $dynamicModal).show();
            $dynamicModal.modal("show");
          }
        },
        error: (error) => {
          $icon.removeClass("animate");
          $(".dynamic", $dynamicModal).hide();
          $(".modal-body.server-error p", $dynamicModal).html("Server Error");
          $(".server-error", $dynamicModal).show();
          $dynamicModal.modal("show");
          console.error(error)
        }

      })
    }
  })
  if($("#sangomaconnect1").prop("checked")) {
    $advancedNetwork.show();
  }
  $advancedNetworkEnableBtn.on('click', function () {
    $advancedNetworkForm.show();
  })

  $advancedNetworkDisableBtn.on('click', function () {
    $advancedNetworkForm.hide();
  })
  $sangomaconnectEnableBtn.on('click', function () {
    $advancedNetwork.show();
  })
  $sangomaconnectDisableBtn.on('click', function () {
    $advancedNetwork.hide();
  })

})(jQuery, window, document);
