<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 Categories</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="form-group row">
        <div class="col-md-12">
          <ListView
            :items="categories"
            :showColumnChooser="false"
            :editable="true"
            :deleteable="true"
            :displayColumns="columns"
            @onEdit="Edit"
            @onDelete="Delete"
            @onNew="New"
          />
        </div>
        <modal name="Category" :width="600" :height="220">
          <Category @CategoryUpdate="CategoryUpdate" :editCategory="CurrentCategory" />
        </modal>
      </div>
      <div v-if="wait" id="preloader"></div>
    </div>
  </div>
</template>

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

export default {
  name: "RestaurantCategoryList",
  components: {
    ListView,
    Category
  },
  data() {
    return {
      wait: true,
      category: {},
      columns: ["description"],
      restaurantId: Log.restaurantId()
    };
  },
  methods: {
    ...mapActions("menuCategories", [
      "getCategories",
      "addCategory",
      "updateCategory",
      "deleteCategory"
    ]),
    New() {
      this.category = null;
      this.$modal.show("Category");
    },
    Edit(item) {
      this.category = item;
      this.$modal.show("Category");
    },
    Delete(item) {
      this.deleteCategory(item.id);
    },
    CategoryUpdate(item) {
      item.restaurantId = this.restaurantId;
      if (item.id == 0) {
        this.addCategory(item);
      } else {
        this.updateCategory(item);
      }
      this.$modal.hide("Category");
    }
  },
  computed: {
    ...mapState("menuCategories", ["categories"]),
    CurrentCategory() {
      return this.category;
    }
  },
  mounted() {
    this.getCategories(this.restaurantId).then(() => {
      this.wait = false;
    });
  }
};
</script>