<template>
  <div class="q-pa-md q-col-gutter-y-md redesigned">
    <div class="row q-gutter-sm justify-between">
      <RouterView
        v-if="hasCustomHeader"
        name="header"
        :data="data"
        :loading="loading"
      />
      <div class="col-12 col-sm section-header" v-if="showTitle">
        {{ $t(title) || title }}
      </div>
      <RouterView
        name="listButtons"
        :selected="selected"
        :data="data"
        :loading="loading"
        @onReload="onReload"
      />
    </div>
    <div class="row">
      <div class="col-12">
        <RouterView
          ref="refreshTarget"
          @row-selected="rowSelected"
          :resetSelect="resetSelect"
          @data-loading="onDataLoading"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { callMethodOnComponent } from '@/recruits/utils';

export default {
  name: 'BaseListLayout',
  data() {
    return {
      selected: [],
      resetSelect: false,
      incomingComponents: [],
      data: [],
      loading: false,
    };
  },
  methods: {
    onDataLoading({ data, loading }) {
      this.data = data;
      this.loading = loading;
    },
    rowSelected(list) {
      this.selected = [...list];
      this.resetSelect = false;
    },
    onReload() {
      this.resetSelect = true;
      callMethodOnComponent(this.$refs.refreshTarget, 'refresh');
    },
    matchRouterComponent(name) {
      return this.incomingComponents.includes(name);
    },
  },
  computed: {
    hasCustomHeader() {
      return this.$route?.meta?.customHeader || false;
    },
    title() {
      return this.$route?.meta?.listTitle || this.$route?.meta?.label;
    },
    showTitle() {
      return (
        this.title &&
        (this.$route?.meta?.showTitle === true ||
          this.$route?.meta?.showTitle === undefined)
      );
    },
  },
  watch: {
    $route() {
      this.selected = [];
    },
  },
};
</script>

<style lang="scss" scoped>
.section-header {
  font-family: 'Golos Text', sans-serif;
  font-style: normal;
  font-weight: 500;
  font-size: 28px;
  line-height: 36px;
  letter-spacing: -0.02em;
  color: rgba(2, 12, 18, 0.9);
}
</style>
