<template>
  <q-dialog
    persistent
    v-model="show_dialog"
    @before-hide="resetData"
    @keydown="
      (evt) => {
        if (!loading && evt.keyCode == 27) {
          show_dialog = false;
        }
      }
    "
  >
    <q-card class="overflow-hidden" style="overflow-y: scroll; max-width: 35vw">
      <q-form @submit="onSubmit">
        <q-card-section class="q-ma-none q-pa-sm">
          <div class="row">
            <div class="col-auto text-h6">
              {{ $t("grid setting") }}
            </div>
            <q-space />
            <div>
              <q-btn
                :loading="loading"
                flat
                round
                icon="close"
                :disable="loading"
                color="red"
                v-close-popup
              >
                <q-tooltip>
                  {{ $t("close") }}
                </q-tooltip>
              </q-btn>
            </div>
          </div>
        </q-card-section>

        <q-separator />

        <q-card-section style="max-height: 50vh; width: 35vw" class="scroll">
          <q-list>
            <q-item>
              <q-item-section>
                <q-select
                  :label="$t('wall row')"
                  :hint="$t('please input ') + $t('wall row') + ':'"
                  :options="select_options"
                  v-model="rows"
                >
                </q-select>
              </q-item-section>
            </q-item>
            <q-item>
              <q-item-section>
                <q-select
                  :label="$t('wall col')"
                  :hint="$t('please input ') + $t('wall col') + ':'"
                  :options="select_options"
                  v-model="cols"
                >
                </q-select>
              </q-item-section>
            </q-item>
          </q-list>
        </q-card-section>

        <q-separator />

        <q-card-actions align="right">
          <q-btn
            :loading="loading"
            flat
            :label="$t('Cancel')"
            no-caps
            color="primary"
            v-close-popup
          />
          <q-btn
            ref="accept"
            flat
            :label="$t('Accept')"
            no-caps
            :loading="loading"
            type="submit"
            color="primary"
          />
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>

<script lang="ts">
import { defineComponent, ref, watch, computed } from "vue";
import { useStore } from "src/store";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
import GlobalData from "src/common/GlobalData";

export default defineComponent({
  name: "ComponentGridSettingDialog",

  setup() {
    let $store = useStore();
    let $q = useQuasar();
    let $t = useI18n();

    let show_dialog = ref(false);
    let loading = ref(false);
    let rows = ref(1);
    let cols = ref(1);

    let select_options = [1, 2, 3, 4, 5, 6, 7, 8, 9];

    return {
      show_dialog,
      loading,
      rows,
      cols,
      select_options,
      showDialog() {
        show_dialog.value = true;

        rows.value = parseInt(
          (GlobalData.getInstance().applicationConfig?.wall_row ?? 1).toString()
        );
        cols.value = parseInt(
          (GlobalData.getInstance().applicationConfig?.wall_col ?? 1).toString()
        );
      },
      resetData() {
        loading.value = false;
      },

      async onSubmit() {
        loading.value = true;
        try {
          GlobalData.getInstance()
            .getCurrentClient()
            ?.setWallRowCol(rows.value, cols.value);
          show_dialog.value = false;
        } catch {}
        loading.value = false;
      },
    };
  },
});
</script>
