<template>
  <!-- eslint-disable max-len -->
  <div>
    <div class="container">
      <section class="intro-single">
        <div class="container">
          <div class="row">
            <div class="col-md-12 col-lg-8">
              <div class="title-single-box">
                <h1 v-if="user !== 'All'" class="title-single">My {{ propertyType }} Properties</h1>
                <h1 v-else class="title-single">All {{ propertyType }} Properties</h1>
              </div>
            </div>
          </div>
        </div>
      </section>
    </div>
    <div class="container">
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>Name</th>
            <th>Property ID</th>
            <th>Size</th>
            <th>Price</th>
            <th>Type</th>
            <th>Publish</th>
            <th>Status</th>
            <th>Edit</th>
            <th>Delete</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, i) in properties" :key="i">
            <td v-if="item.propertyType.usageType === 1">{{item.propertyName}}</td>
            <td v-else>{{ item.shortDescription }}</td>
            <td>{{item.id}}</td>
            <td></td>
            <td>{{ item.price }}</td>
            <td>{{ item.propertyType.description }}</td>
            <td>{{ item.publish }}</td>
            <td>Publish</td>
            <td>
              <router-link :to="`/property/${item.id}`">Edit</router-link>
            </td>
            <td>Delete</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  name: 'PropertyList',
  data() {
    return {
      propertyType: '',
      user: '',
    };
  },
  methods: {
    ...mapActions('propertyList', ['getProperties']),
  },
  mounted() {
    this.propertyType = this.$route.params.propertyType;
    this.user = this.$route.params.user;

    this.getProperties(
      Object.assign(
        {},
        {
          propertyType: this.$route.params.propertyType,
          user: this.$route.params.user,
        },
      ),
    );
  },
  computed: {
    ...mapState('propertyList', ['properties']),
    TypeChanged() {
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      this.propertyType = this.$route.params.propertyType;
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      this.user = this.$route.params.user;
      this.getProperties(
        Object.assign(
          {},
          {
            propertyType: this.$route.params.propertyType,
            user: this.$route.params.user,
          },
        ),
      );
      return this.propertyType;
    },
  },
  watch: {
    TypeChanged() {
      console.log(this.propertyType);
    },
  },
};
</script>
