<template>
  <div>
    <div v-if="loaded" class="container">
      <div class="row">
        <div class="col">
          <div class="section-header">
            <h2>Search {{ propertyType === "res" ? "Residential" : "Commercial" }}</h2>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="container">
          <ListView
            :items="filteredProperties"
            :showNew="false"
            :showColumnChooser="false"
            @onRowClick="onRowClick"
          />
        </div>
      </div>
    </div>
    <div v-else class="text-center mt-5">
      <vue-loaders
        class="my-auto"
        name="line-spin-fade-loader"
        color="black"
        scale="1"
      ></vue-loaders>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import { mapState, mapActions } from "vuex";
import ListView from "../../shared/listView.vue";
import Log from "../../../assets/Log";

export default {
  name: "CarouselSearch",
  props: {
    name: String,
    selectProperty: String
  },
  components: {
    ListView
  },
  data() {
    return {
      loaded: false,
      user: Log.getUser(),
      propertyType: ""
    };
  },
  mounted() {
    this.loadData().then(() => {
      setTimeout(() => {
        this.loaded = true;
        this.propertyType = this.selectProperty;
        console.log(this.properties);
      }, 100);
    });
  },
  computed: {
    ...mapState("propertyList", ["properties"]),
    filteredProperties() {
      var propertyList = [];
      this.properties.forEach(property => {
        if (this.propertyType === "comm") {
          if (property.type === "Commercial") {
            propertyList.push(property);
          }
        } else {
          if (property.type === "Residential") {
            propertyList.push(property);
          }
        }
      });
      return propertyList;
    }
  },
  methods: {
    ...mapActions("propertyList", ["getAdminProperties"]),
    onRowClick(item) {
      console.log(item[0]);
      this.$emit("onSelected", item[0]);
    },
    async loadData() {
      await this.getAdminProperties(this.user.id);
    }
  }
};
</script>
