<template>
  <Page @loaded="onPageLoad" actionBarHidden="true">
    <GridLayout rows="*, auto" columns="auto, *">
      <ListView
        colSpan="2"
        rowSpan="2"
        class="options-list"
        for="item in items"
      >
        <v-template if="$index == 0">
          <Label class="pageTitle" :text="'rest' | L" />
        </v-template>
        <v-template if="$index == 5">
          <Label class="group-info sub tw" :text="'restInfo' | L" />
        </v-template>
        <v-template if="$index == 6">
          <StackLayout class="listSpace"> </StackLayout>
        </v-template>
        <v-template>
          <GridLayout
            columns="auto, *"
            class="option"
            @touch="touch($event, item.type)"
          >
            <Label class="ico" :text="icon.reset" />
            <Label col="1" :text="item.title | L" class="info" />
          </GridLayout>
        </v-template>
      </ListView>
      <GridLayout
        v-show="!toast"
        row="1"
        class="appbar"
        @loaded="onAppBarLoad"
        columns="auto, *"
      >
        <Button class="ico" :text="icon.back" @tap="$navigateBack()" />
      </GridLayout>
      <GridLayout
        v-show="toast"
        row="1"
        colSpan="2"
        class="appbar snackBar"
        columns="*"
        @swipe="hideToast"
      >
        <FlexboxLayout minHeight="48" alignItems="center">
          <Label class="title msg" :text="toast" />
        </FlexboxLayout>
      </GridLayout>
    </GridLayout>
  </Page>
</template>

<script>
import { Observable, CoreTypes } from "@nativescript/core";
import { localize } from "@nativescript/localize";
import { mapState, mapActions } from "vuex";
import * as utils from "~/shared/utils";
export default {
  data() {
    return {
      toast: null,
      appbar: null,
    };
  },
  computed: {
    ...mapState(["icon"]),
    items() {
      return [
        {},
        {
          type: "cuisines",
          title: "restCuiL",
        },
        {
          type: "categories",
          title: "restCatL",
        },
        {
          type: "yieldUnits",
          title: "restYUL",
        },
        {
          type: "units",
          title: "restUL",
        },
        {},
        {},
      ];
    },
  },
  methods: {
    ...mapActions(["resetListItemsAction"]),
    onPageLoad(args) {
      const page = args.object;
      page.bindingContext = new Observable();
    },
    onAppBarLoad({ object }) {
      this.appbar = object;
    },
    // RESET
    resetListItems(listName) {
      this.resetListItemsAction(listName);
      this.showToast();
    },
    showToast() {
      this.toast = localize("restDone");
      utils.timer(5, (val) => {
        if (!val) this.toast = val;
      });
    },
    hideToast({ object }) {
      object
        .animate({
          opacity: 0,
          translate: { x: 0, y: 64 },
          duration: 250,
          curve: CoreTypes.AnimationCurve.ease,
        })
        .then(() => {
          this.showUndo = false;
          this.appbar.translateY = 64;
          this.appbar.animate({
            translate: { x: 0, y: 0 },
            duration: 250,
            curve: CoreTypes.AnimationCurve.ease,
          });
          object.opacity = 1;
          object.translateY = 0;
          this.toast = null;
        });
    },
    touch({ object, action }, type) {
      object.className = action.match(/down|move/) ? "option fade" : "option";
      if (action == "up") this.resetListItems(type);
    },
  },
};
</script>
