import '@things-factory/grist-ui'

import gql from 'graphql-tag'
import { css, html, LitElement } from 'lit-element'

import { SingleColumnFormStyles } from '@things-factory/form-ui'
import { i18next, localize } from '@things-factory/i18n-base'
import { client } from '@things-factory/shell'

class OrderRemarkPopup extends localize(i18next)(LitElement) {
  static get properties() {
    return {
      orderRemark: String,
      releaseGoodNo: String,
      checkedRemarkAt: String
    }
  }

  static get styles() {
    return [
      SingleColumnFormStyles,
      css`
        :host {
          padding: 10px;
          display: flex;
          flex-direction: column;
          overflow-x: overlay;
          background-color: var(--main-section-background-color);
        }
        .button-container {
          padding: var(--button-container-padding);
          margin: var(--button-container-margin);
          text-align: var(--button-container-align);
          background-color: var(--button-container-background);
          height: var(--button-container-height);
        }
        .button-container button {
          background-color: var(--button-container-button-background-color);
          border-radius: var(--button-container-button-border-radius);
          height: var(--button-container-button-height);
          border: var(--button-container-button-border);
          margin: var(--button-container-button-margin);

          padding: var(--button-padding);
          color: var(--theme-white-color);
          font: var(--button-font);
          text-transform: var(--button-text-transform);
        }
        .button-container button:hover,
        .button-container button:active {
          background-color: var(--button-background-focus-color);
        }
      `
    ]
  }

  render() {
    const remarkValue = this.orderRemark || ''
    const checkedRemarkAt = this.checkedRemarkAt || ''

    return html`
      <form id="input-form" class="single-column-form">
        <fieldset>
          <label>${i18next.t('label.order_remark')}</label>
          <textarea name="remark" row="10" readonly .value=${remarkValue}></textarea>
        </fieldset>
      </form>
      <div class="button-container">
        ${checkedRemarkAt
          ? html`
              <mwc-button
                danger
                raised
                @click="${e => history.back()}"
                label="${i18next.t('button.back')}"
              ></mwc-button>
            `
          : html`
              <mwc-button
                danger
                raised
                @click="${e => history.back()}"
                label="${i18next.t('button.back')}"
              ></mwc-button>
              <mwc-button
                raised
                @click="${this._executeOrderRemark.bind(this)}"
                label="${i18next.t('button.check')}"
              ></mwc-button>
            `}
      </div>
    `
  }

  async _executeOrderRemark() {
    const response = await client.mutate({
      mutation: gql`
        mutation executeOrderRemark($releaseGoodNo: String!) {
          executeOrderRemark(releaseGoodNo: $releaseGoodNo)
        }
      `,
      variables: { releaseGoodNo: this.releaseGoodNo }
    })

    if (!response.errors) {
      this.dispatchEvent(new CustomEvent('remark-checked'))
      history.back()
    }
  }
}

window.customElements.define('order-remark-popup', OrderRemarkPopup)
