/*

 ----------------------------------------------------------------------------
 | admin-ui: SB2-Admin UI Theme WebComponents Library                        |
 |                                                                           |
 | Copyright (c) 2020 M/Gateway Developments Ltd,                            |
 | Redhill, Surrey UK.                                                       |
 | All rights reserved.                                                      |
 |                                                                           |
 | http://www.mgateway.com                                                   |
 | Email: rtweed@mgateway.com                                                |
 |                                                                           |
 |                                                                           |
 | Licensed under the Apache License, Version 2.0 (the "License");           |
 | you may not use this file except in compliance with the License.          |
 | You may obtain a copy of the License at                                   |
 |                                                                           |
 |     http://www.apache.org/licenses/LICENSE-2.0                            |
 |                                                                           |
 | Unless required by applicable law or agreed to in writing, software       |
 | distributed under the License is distributed on an "AS IS" BASIS,         |
 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  |
 | See the License for the specific language governing permissions and       |
 |  limitations under the License.                                           |
 ----------------------------------------------------------------------------

 5 May 2020

*/

export function load() {

  let componentName = 'adminui-form';

  class adminui_form extends HTMLElement {
    constructor() {
      super();

      const html = `
<form></form>
      `;
      this.html = `${html}`;
    }

    addClass(cls) {
      this.rootElement.classList.add(cls);
    }

    removeClass(cls) {
      this.rootElement.classList.remove(cls);
    }

    setState(state) {
      if (state.name) {
        this.name = state.name;
      }
      if (state.cls) {
        let _this = this;
        state.cls.split(' ').forEach(function(cls) {
          _this.addClass(cls);
        });
      }
    }

    show() {
      this.rootElement.style = 'display:';
    }

    hide() {
      this.rootElement.style = 'display: none';
    }

    setFieldValue(name, value, checked) {
      if (typeof checked !== 'undefined') {
        if (this.fieldValues[name]) {
          this.fieldValues[name][value] = checked;
          //console.log(this.fieldValues);
        }
      }
      else {
        this.fieldValues[name] = value;
        //console.log('*** ' + name + ' field set to ' + value);
      }
    }

    getFieldValue(name) {
      return this.fieldValues[name];
    }

    removeField(name) {
      delete this.fieldValues[name];
    }

    connectedCallback() {
      this.innerHTML = this.html;
      this.rootElement = this.getElementsByTagName('form')[0];
      this.childrenTarget = this.rootElement;
      this.field = {};
      this.fieldValues = {};
    }

    disconnectedCallback() {
      console.log('*** form component was removed!');
      if (this.onUnload) this.onUnload();
    }
  }

  customElements.define(componentName, adminui_form);

}
