/*
 * Copyright (c) 2018, FusionAuth, All Rights Reserved
 */
'use strict';

var FusionAuth = FusionAuth || {};
FusionAuth.Admin = FusionAuth.Admin || {};

/**
 * @constructor
 */
FusionAuth.Admin.TenantForm = function() {
  Prime.Utils.bindAll(this);

  this.form =  Prime.Document.queryById('tenant-form');
  this.addTenant = this.form.getAttribute('action') === '/admin/tenant/add';

  var tabs = Prime.Document.queryFirst('.tabs');
  if (tabs !== null) {
    new Prime.Widgets.Tabs(Prime.Document.queryFirst('.tabs'))
        .withErrorClassHandling('error')
        .withLocalStorageKey(this.addTenant ? null : 'tenant-configuration')
        .initialize();
  }

  this.childRegistrationCheckbox = this.form.queryFirst('input[name="tenant.familyConfiguration.allowChildRegistrations"]')
      .addEventListener('change', this._handleChildRegistrationChange);
  this.noChildRegistrationDiv = new Prime.Effects.SlideOpen(Prime.Document.queryById('no-child-registration'));
  this.childRegistrationDiv = new Prime.Effects.SlideOpen(Prime.Document.queryById('child-registration'));
};

FusionAuth.Admin.TenantForm.prototype = {

  /* ===================================================================================================================
   * Private Methods
   * ===================================================================================================================*/

  _handleChildRegistrationChange: function() {
    if (this.childRegistrationCheckbox.isChecked()) {
      this.noChildRegistrationDiv.close();
      this.childRegistrationDiv.open();
    } else {
      this.noChildRegistrationDiv.open();
      this.childRegistrationDiv.close();
    }
  }
};
