<template>
  <div class="search">
    <div class="search-group-wrap">
      <div class="search-group" v-for="(field, index) in search" :key="index">
        <input type="text" v-model.trim="field.query" placeholder="Meklēt...">
        <select v-model="field.property" v-if="items.length > 0">
          <option v-for="(key, index) in Object.keys(items[0])" :key="index" :value="key">{{ properties[key] }}</option>
        </select>
        <button class="btn-round-big btn-red" v-show="search.length > 1" @click="removeQuery(index)">-</button>
      </div>
    </div>
    <button class="btn-round-big btn-blue" @click="addQuery">+</button>
    <div class="sort-group">
      <div>
        <label for="sort">Kārtot pēc:</label>
        <select v-model="sort.property" v-if="items.length > 0" id="sort">
          <option v-for="(key, index) in Object.keys(items[0])" :key="index" :value="key">{{ properties[key] }}</option>
        </select>
      </div>
      <div>
        <label for="asc">Secība:</label>
        <button id="asc" @click="sort.asc = !sort.asc">
          {{ sort.asc ? 'Aug.' : 'Dilst.' }}
          <span>{{ sort.asc ? '&#9650;' : '&#9660;' }}</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    items: Array,
    filtered: Array,
    properties: Object,
    extra: Array,
    searchOperation: Object
  },
  data() {
    return {
      search: [{ query: '', property: 'name' }],
      sort: {
        property: 'name',
        asc: true
      }
    }
  },
  computed: {
    itemsForSearch() {
      const items = JSON.parse(JSON.stringify(this.items));
      items.forEach(x => {
        if (this.extra) {
          this.extra.forEach(y => {
            x[y] = x[y]?.length || 0;
          });
        }
        x.createdAt = x.createdAt.substr(0, 10);
        x.updatedAt = x.updatedAt.substr(0, 10);
      });
      return items;
    },
    itemsSorted() {
      const { property, asc } = this.sort;
      return [...this.itemsForSearch].sort((a, b) => {
        if (asc) {
          if (a[property] < b[property]) return -1;
          else if (a[property] > b[property]) return 1;
          else return 0;
        } else {
          if (a[property] > b[property]) return -1;
          else if (a[property] < b[property]) return 1;
          else return 0;
        }
      });
    },
    itemsFound() {
      return this.itemsSorted.filter(x => this.search.every(field => field.query.split(',').some(keyword => x[field.property].toString().toLowerCase().includes(keyword.toLowerCase().trim()))));
    }
  },
  watch: {
    items: function () {
      this.sort.property = 'name';
      this.search = [{ query: '', property: 'name' }];
    },
    itemsFound: function (newItems) {
      this.$emit('update:filtered', newItems)
    },
    searchOperation: function (operation) {
      if (operation) this.search = [{ query: operation.query, property: operation.property }];
    }
  },
  methods: {
    addQuery() {
      this.search.push({ query: '', property: 'name' });
    },
    removeQuery(index) {
      this.search.splice(index, 1);
    }
  }
}
</script>

<style scoped>
.search {
  background: #2c3e50;
  padding: 18px 0;
}
.search select, .search button, .search label {
  padding: 4px;
  margin: 4px;
  font-family: inherit;
}
.search input {
  padding: 5px;
  margin: 4px;
  font-family: inherit;
  background: #384c61;
  border: 1px solid #5d758e;
  color: #fff;
}
.search input::placeholder {
  color: #91a5b9;
}
.search select {
  background: #384c61;
  border: 1px solid #5d758e;
  color: #fff;
}
.search-group-wrap {
  display: inline-block;
}
.search label {
  font-size: 14px;
  color: #fff;
}
.sort-group > div {
  display: inline-block;
}
#asc {
  border-radius: 5px;
  border: none;
  color: #fff;
  background: #5d758e;
  align-items: center;
  cursor: pointer;
}
#asc span {
  font-size: 9px;
  margin: 2px;
}
@media screen and (max-width: 400px) {
  .search-group {
    display: flex;
    flex-direction: column;
  }
  .search-group-wrap {
    display: block;
  }
  .search-group input, .search-group select {
    flex: 1;
    margin: 4px 10px;
  }
  .search-group button {
    margin: 4px auto;
  }
  .sort-group {
    display: flex;
    flex-direction: column;
  }
  .sort-group > div {
    display: flex;
    margin: 0 5px;
    flex-wrap: wrap;
  }
  .sort-group > div * {
    flex: 1;
  }
}
</style>
