<template>
  <div :id="rowId" :class="rowClasses" :style="backgroundImageStyle">
    <div class="meet_the_team">
      <div class="title-subtitle-paragraph">
        <h1 class="title" v-html="properties['Title'].value" v-if="properties['Title'].value" />
        <h4 class="subtitle" v-html="properties['Subtitle'].value" v-if="properties['Subtitle'].value" />
        <p class="paragraph" v-html="properties['Paragraph Text'].value" v-if="properties['Paragraph Text'].value" />
      </div>
      <div class="members-container">
        <div
          class="member"
          @click="selectedMember = member"
          v-for="member in teamMembers"
        >
          <div class="image-icon-link">
            <div class="image-container">
              <div
                class="image"
                :style="`background-image: url('${member['Image'].url}')`"
                v-if="member['Image'] && member['Image'].url"
              />
              <i class="fas fa-user" v-else />
            </div>
            <div class="icon-link-container" v-if="member['Icon Classes'] && member['Icon URL']">
              <div class="relative-container">
                <a :href="member['Icon URL']" @click.stop>
                  <i :class="member['Icon Classes']" />
                </a>
              </div>
            </div>
          </div>
          <div class="name-position" v-if="member['Name / Title'] || member['Job / Subtitle']">
            <h6 class="name" v-html="member['Name / Title']" />
            <div class="position" v-html="member['Job / Subtitle']" />
          </div>
        </div>
      </div>
    </div>
    <div class="member-modal-container" v-if="selectedMember">
      <div class="member-modal">
        <div class="close-icon-container" @click="selectedMember = null">
          <i class="fal fa-times" />
        </div>
        <div class="image-container">
          <img
            :alt="`Image of ${selectedMember['Name / Title']}`"
            :src="selectedMember['Image'].url"
            v-if="selectedMember['Image'] && selectedMember['Image'].url"
          />
          <i class="fas fa-user" v-else />
        </div>
        <div
          class="content-container"
          v-if="selectedMember['Name / Title'] || selectedMember['Job / Subtitle'] || selectedMember['Bio']"
        >
          <h4
            class="name"
            v-html="selectedMember['Name / Title']"
            v-if="selectedMember['Name / Title']"
          />
          <div
            class="position"
            v-html="selectedMember['Job / Subtitle']"
            v-if="selectedMember['Job / Subtitle']"
          />
          <div
            class="bio"
            v-html="selectedMember['Bio']"
            v-if="selectedMember['Bio']"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "BuildableMeetTheTeam",
  props: ['componentData', 'rowClasses', 'rowIndex', 'rowId', 'backgroundImageStyle'],
  data() {
    return {
      ...JSON.parse(this.componentData),
      selectedMember: null
    };
  },
  computed: {
    teamMembers() {
      return this.properties['Team Member'].value
        ? this.properties['Team Member'].value.map(member => {
          return member.properties.reduce((acc, prop) => {
            return {
              ...acc,
              [prop.name]: prop.value
            };
          }, {});
        })
        : [];
    }
  },
  watch: {
    selectedMember(newVal, oldVal) {
      if (newVal !== oldVal) {
        if (newVal) {
          document.body.classList.add("modal-open");
        } else {
          document.body.classList.remove("modal-open");
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
