<template>
  <div>
    <div class="container">
      <div class="container">
        <div class="row">
          <div class="col-md-12 col-lg-8">
            <div class="title-box-d">
              <br />
              <h1 class="title-d" style="text-align:left; font-size: 250%">Menu</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="form-group row">
        <div class="col-md-12">
          <ListView
            :items="menuItems"
            :showColumnChooser="false"
            :editable="true"
            :deleteable="true"
            :displayColumns="columns"
            :displayFormats="formats"
            @onEdit="Edit"
            @onDelete="Delete"
            @onNew="New"
          />
        </div>
      </div>
      <div v-if="wait" id="preloader"></div>
    </div>
  </div>
</template>

<script>
import MenuItem from "../restaurants/restaurantMenuItem";
import ListView from "../shared/listView";
import { mapState, mapActions } from "vuex";
import Log from "../../assets/Log";

export default {
  name: "RestaurantMenu",
  components: {
    ListView,
    MenuItem
  },
  data() {
    return {
      wait: true,
      menu: {},
      columns: ["category", "name", "description", "price", "image"],
      formats: ["", "", "", "money", "image"],
      restaurantId: Log.restaurantId()
    };
  },
  methods: {
    ...mapActions("menu", ["getMenuItems", "deleteMenuItem"]),
    New() {
      // this.menu = this.menuItem;
      // this.$modal.show("Menu");
      this.$router.push("/MenuItem/0");
    },
    Edit(item) {
      this.menu = item;
      this.$router.push("/MenuItem/" + item.id);
    },
    Delete(item) {
      this.wait = true;
      this.deleteMenuItem(item);

      this.getMenuItems(this.restaurantId).then(() => {
        this.wait = false;
      });
    }
  },
  computed: {
    ...mapState("menu", ["menuItem", "menuItems"]),
    CurrentMenu() {
      return this.menu;
    }
  },
  mounted() {
    this.getMenuItems(this.restaurantId).then(() => {
      this.wait = false;
    });
  }
};
</script>