{"size": 2381, "ext": "js", "max_stars_repo_path": "_src/js/OrderCustomer.js", "max_stars_repo_name": "dulagorefeicoes/order-create-alpha", "max_stars_repo_stars_event_min_datetime": "2018-10-31T20:39:16.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-31T20:39:16.000Z", "max_issues_repo_path": "_src/js/OrderCustomer.js", "max_issues_repo_name": "dulagorefeicoes/order-create-alpha", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "_src/js/OrderCustomer.js", "max_forks_repo_name": "dulagorefeicoes/order-create-alpha", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0505050505, "max_line_length": 120, "alphanum_fraction": 0.6509869803}
class OrderCustomer {

  constructor(order) {

    this.order = order;

    this.orderRef = this.order.orderRef;
    this.customerRef = this.orderRef.child('customer');

    this.element = document.createElement('div');

    this.build();

  }

  build() {

    this.element.className = 'OrderCustomer row';

    this.element.customerName = this.buildCustomerNameFieldElement();
    this.element.customerContact = this.buildCustomerContactFieldElement();

  }

  buildCustomerNameFieldElement() {

    const element = new CustomerAutoComplete(this.orderRef.key, 'customerName', orderApp.customerList, item => {
      Order.setCustomer(this.orderRef, item.customer);
    }).element;
    element.classList.add('OrderCustomer-customerNameField', 'col', 's7');
    this.element.appendChild(element);

    element.input.addEventListener('input', () => {

      this.customerRef.child('customerName').set(element.input.value);

    });
    this.customerRef.child('customerName').on('value', snap => {

      if (snap.val() !== element.input.value)
        element.input.value = snap.val();

    });

    element.label.innerHTML = 'Nome';
    this.customerRef.child('customerName').on('value', snap => {
      if (snap.val())
        element.label.classList = 'active';
    });

    M.updateTextFields();

    return element;

  }

  buildCustomerContactFieldElement() {

    const autocomplete = new CustomerAutoComplete(this.orderRef.key, 'customerContact', orderApp.customerList, item => {
      Order.setCustomer(this.orderRef, item.customer);
    });

    const element = autocomplete.element;
    element.classList.add('OrderCustomer-customerContact', 'input-field', 'col', 's5');
    this.element.appendChild(element);

    element.input.type = 'tel';
    element.input.addEventListener('input', () => {

      this.customerRef.child('customerContact').set(element.input.value);

    });
    this.customerRef.child('customerContact').on('value', snap => {

      if (snap.val() !== element.input.value)
        element.input.value = snap.val();

    });

    element.label.innerHTML = 'Telefone';
    this.customerRef.child('customerContact').on('value', snap => {
      if (snap.val()) element.label.classList = 'active';
    });

    return element;

  }

  focus() {

    try {
      this.element.customerName.input.focus();
    } catch (e) {
      console.log(e);
    }

  }

}