import '@things-factory/grist-ui'

import { css, html, LitElement } from 'lit-element'
import { connect } from 'pwa-helpers/connect-mixin'

import { i18next, localize } from '@things-factory/i18n-base'
import { store } from '@things-factory/shell'
import { isMobileDevice } from '@things-factory/utils'

class UnloadedInventoriesPopup extends connect(store)(localize(i18next)(LitElement)) {
  static get properties() {
    return {
      config: Object,
      data: Object,
      unloadedInventories: Array
    }
  }

  static get styles() {
    return [
      css`
        data-grist {
          overflow-y: hidden;
          flex: 1;
        }
        .grist {
          background-color: var(--main-section-background-color);
          display: flex;
          flex-direction: column;
          flex: 1;
          overflow-y: auto;
          --grid-record-odd-background-color: var(--theme-white-color);
          --data-list-background-color: var(--theme-white-color);
        }
        :host {
          padding: 10px;
          display: flex;
          flex-direction: column;
          overflow-x: overlay;
          background-color: var(--main-section-background-color);
          --data-list-label-text-transform: capitalize;
        }
      `
    ]
  }

  get dataGrist() {
    return this.shadowRoot.querySelector('data-grist')
  }

  render() {
    return html`
      <div class="grist">
        <data-grist
          id="grist"
          .mode=${isMobileDevice() ? 'LIST' : 'GRID'}
          .config=${this.config}
          .data="${this.data}"
        ></data-grist>
      </div>
    `
  }

  updated(changedProps) {
    if (changedProps.has('unloadedInventories')) {
      this.data = {
        records: this.unloadedInventories
      }
    }
  }

  firstUpdated() {
    this.config = {
      rows: { appendable: false },
      list: {
        fields: ['palletId', 'cartonId', 'qty', 'expirationDate', 'manufactureDate', 'reusablePallet']
      },
      pagination: { infinite: true },
      columns: [
        { type: 'gutter', gutterName: 'sequence' },
        {
          type: 'string',
          name: 'palletId',
          header: i18next.t('field.lot_id'),
          label: true,
          width: 120
        },
        {
          type: 'string',
          name: 'cartonId',
          header: i18next.t('field.carton_id'),
          label: true,
          width: 120
        },
        {
          type: 'float',
          name: 'qty',
          header: i18next.t('field.actual_pack_qty'),
          label: true,
          width: 150
        },
        {
          type: 'date',
          name: 'expirationDate',
          header: i18next.t('field.exp_date'),
          label: true,
          width: 120
        },
        {
          type: 'date',
          name: 'manufactureDate',
          header: i18next.t('field.manufacture_date'),
          label: true,
          width: 120
        },
        {
          type: 'object',
          name: 'reusablePallet',
          header: i18next.t('field.reusable_pallet'),
          label: true,
          width: 120
        }
      ]
    }
  }

  _showToast({ type, message }) {
    document.dispatchEvent(
      new CustomEvent('notify', {
        detail: {
          type,
          message
        }
      })
    )
  }
}

window.customElements.define('unloaded-inventories-popup', UnloadedInventoriesPopup)
