<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%">Restaurant Users</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="form-group row">
        <div class="col-md-12">
          <ListView
            :items="restaurantUsers"
            :showColumnChooser="false"
            :editable="true"
            :hideSearch="true"
            :deleteable="true"
            :displayColumns="columns"
            @onEdit="Edit"
            @onDelete="Delete"
            @onNew="New"
          />
        </div>
        <modal name="User" :width="600" :height="470">
          <User @UpdateUser="UpdateUser" :editUser="CurrentUser" />
        </modal>
      </div>
      <div v-if="wait" id="preloader"></div>
    </div>
  </div>
</template>

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

export default {
  name: "RestaurantCategoryList",
  components: {
    ListView,
    User
  },
  data() {
    return {
      wait: true,
      columns: ["emailAddress", "firstName", "surname", "cellphone"],
      restaurantId: Log.restaurantId()
    };
  },
  methods: {
    ...mapActions("restaurantUser", [
      "getUsers",
      "getUser",
      "insertUser",
      "updateUser",
      "deleteUser"
    ]),
    New() {
      this.getUser(0);
      this.$modal.show("User");
    },
    Edit(item) {
      this.getUser(item.id);
      this.$modal.show("User");
    },
    Delete(item) {
      this.deleteUser(item);
    },
    UpdateUser(item) {
      const restUser = {
        restaurantId: this.restaurantId,
        user: item
      };

      if (item.id == 0) {
        this.insertUser(restUser);
      } else {
        this.updateUser(restUser);
      }
      this.$modal.hide("User");
    }
  },
  computed: {
    ...mapState("restaurantUser", ["restaurantUsers", "restaurantUser"]),
    CurrentUser() {
      return this.restaurantUser.user;
    }
  },
  mounted() {
    this.getUsers(this.restaurantId).then(() => {
      this.wait = false;
    });
  }
};
</script>