(function($){
  $(document).on('submit', '#wpgc-settings-form', function(e){
    e.preventDefault();
    var data = {
      rules: $('#wpgc_rules').val(),
      default_room: $('#wpgc_default_room').val(),
      turn_urls: $('#wpgc_turn_urls').val(),
      turn_username: $('#wpgc_turn_username').val(),
      turn_credential: $('#wpgc_turn_credential').val(),
      license_api_base: $('#wpgc_license_api_base').val()
    };
    $.ajax({ url: WPGCAdmin.rest + 'admin/settings', method: 'POST', headers: {'X-WP-Nonce': WPGCAdmin.nonce}, data: JSON.stringify(data), contentType: 'application/json' })
      .done(function(){ alert('Saved.'); })
      .fail(function(){ alert('Save failed'); });
  });
  $(document).on('submit', '#wpgc-license-form', function(e){
    e.preventDefault();
    var data = { license_key: $('#wpgc_license_key').val() };
    $.ajax({ url: WPGCAdmin.rest + 'admin/license', method: 'POST', headers: {'X-WP-Nonce': WPGCAdmin.nonce}, data: JSON.stringify(data), contentType: 'application/json' })
      .done(function(resp){ alert('License activated: ' + (resp.expires || 'OK')); location.reload(); })
      .fail(function(xhr){ alert('License error: ' + (xhr.responseJSON && xhr.responseJSON.message ? xhr.responseJSON.message : '')); });
  });
})(jQuery);