<template>
  <div
    class="general"
  >
    <h1
      v-if="activeCorpInfo"
      :title="activeCorpInfo.name"
    >
      {{ activeCorpInfo.name }}
    </h1>
    <form @submit.prevent="onSearch">
      <input
        v-model="filterKeyword"
        type="text"
        placeholder="请输入企业名称"
      >
      <button type="submit">
        <img
          class=""
          src="@/assets/images/icon_search.png"
          alt=""
          draggable="false"
        >
      </button>
    </form>
    <ul>
      <li
        v-for="(decade) in corpListMap.keys()"
        :key="decade"
      >
        <h2>
          <img
            class=""
            src="@/assets/images/decade-decorator-left.png"
            alt=""
            draggable="false"
          >
          <span>{{ decade }}</span>
          <img
            class=""
            src="@/assets/images/decade-decorator-right.png"
            alt=""
            draggable="false"
          >
        </h2>
        <div
          v-for="(corpItem) in corpListMap.get(decade)"
          :id="`corp-item-${corpItem.id}`"
          :key="corpItem.id"
          class="corp-item"
          :class="{
            active: activeCorpId === corpItem.id
          }"
          @click="onClickCorpItem(corpItem.id)"
        >
          <div class="item-icon" />
          <div class="verticle-line" />
          <span
            class="corp-name"
            :title="corpItem.name"
          >
            {{ corpItem.name }}
          </span>
          <span class="corp-time">
            {{ corpItem.createTime }}
          </span>
        </div>
      </li>
    </ul>
    <article v-if="activeCorpInfo && isShowDesc">
      <button
        class="close"
        @click="isShowDesc = false"
      />
      <h2>{{ activeCorpInfo.name }}</h2>
      <img
        class="splitter"
        src="@/assets/images/splitter.png"
        alt=""
        draggable="false"
      >
      <img
        class="banner"
        src="@/assets/images/for-dev.jpg"
        alt=""
        draggable="false"
      >
      <div
        class="txt"
        v-html="activeCorpInfo.content || ''"
      />
    </article>
  </div>
</template>

<script>
import {
  computed,
  onMounted,
  watch,
  reactive,
  ref,
} from 'vue'
import deepClone from 'lodash/cloneDeep'
import corpInfo from '@/assets/mock/corp-info.json'

export default {
  name: 'GeneralView',
  components: {
  },

  setup () {

    const filterKeyword = ref('')

    const corpListRaw = reactive(corpInfo.data)
    const corpListMap = computed(() => {
      const corpListFiltered = corpListRaw.filter((item) => {
        return !filterKeyword.value || item.name.includes(filterKeyword.value)
      })
      const afterSort = deepClone(corpListFiltered)
      afterSort.sort((a, b) => {
        return Date.parse(b.createTime) - Date.parse(a.createTime)
      })
      const ret = new Map()
      afterSort.forEach(element => {
        const t = element.createTime[2]
        const decade = Number(t) < 3 ? `本世纪${t}0年代` : `上世纪${t}0年代`
        if (!ret.get(decade)) {
          ret.set(decade, [])
        }
        ret.get(decade).push(element)
      })
      return ret
    })
    const activeCorpId = ref(null)
    const activeCorpInfo = computed(() => {
      return corpListRaw.find((item) => {
        return item.id === activeCorpId.value
      })
    })
    function onClickCorpItem(id) {
      if ((typeof activeCorpId.value === 'number')) {
        gUnityInst.SendMessage('Panel1', 'SetEnterpriseUnSelected', activeCorpId.value) //设置id为1的企业为未选中状态(此id需要是已显示的)
      }
      gUnityInst.SendMessage('Panel1', 'SetEnterpriseSelected', id) //设置id为1的企业为选中状态(此id需要是已显示的)
      activeCorpId.value = id
      isShowDesc.value = true

      console.log(`corp-item-${id}`)
      const clickedElement = document.querySelector(`#corp-item-${id}`)
      if (clickedElement) {
        clickedElement.scrollIntoView()
      }
    }
    window.onCorpOnMapClicked = onClickCorpItem

    const isShowDesc = ref(true)

    return {
      filterKeyword,

      corpListMap,
      activeCorpId,
      activeCorpInfo,
      onClickCorpItem,

      isShowDesc,
    }
  },
  data() {
    return {
    }
  },
  computed: {
    ...mapState([
    ]),
  },
  mounted() {
    // this.$mitt.emit('test', { msg: 'home mounted' })
  },
  beforeUnmount() {
  },
  unmounted() {
  },
  methods: {
    ...mapMutations([
    ]),
    onSearch() {
      console.log('search!')
    }
  },
}
</script>

<style lang="less" scoped>
.general {
  >h1 {
    position: absolute;
    top: 51px;
    left: 81px;
    max-width: 50%;
    overflow: hidden;
    white-space: pre;
    text-overflow: ellipsis;
    font-size: 48px;
    font-family: Source Han Sans CN-Heavy, Source Han Sans CN;
    font-weight: 800;
    color: #FFFFFF;
    padding-top: 20px;
    padding-bottom: 20px;
    border-top: 1px solid rgba(217, 217, 217, 0.2);
    border-bottom: 1px solid rgba(217, 217, 217, 0.2);
  }
  >form {
    position: absolute;
    top: 196px;
    left: 81px;
    display: flex;
    align-items: center;
    >input {
      background: rgba(255,255,255,0.1);
      border-radius: 3px 3px 3px 3px;
      border: 1px solid rgba(255, 255, 255, 0.5);
      width: 220px;
      height: 40px;
      padding-left: 13px;
      padding-right: 13px;
      font-size: 16px;
      font-family: Source Han Sans CN-Regular, Source Han Sans CN;
      font-weight: 400;
      color: #FFFFFF;
      &:focus {
        border: 1px solid rgba(255, 255, 255, 1);
      }
      &::placeholder {
        font-size: 16px;
        font-family: Source Han Sans CN-Regular, Source Han Sans CN;
        font-weight: 400;
        color: rgba(255, 255, 255, 0.5);
      }
    }
    >button {
      margin-left: 8px;
      width: 40px;
      height: 40px;
      background: rgba(255,255,255,0.3);
      border-radius: 3px 3px 3px 3px;
      opacity: 1;
      border: 1px solid #FFFFFF;
      &:hover {
        background: rgba(255,255,255,0.5);
      }
      >img {
        width: 100%;
        height: 100%;
      }
    }
  }
  >ul {
    position: absolute;
    top: 272px;
    left: 81px;
    max-height: 520px;
    overflow: auto;
    user-select: none;
    >li {
      display: block;
      color: #fff;
      >h2 {
        width: 323px;
        height: 47px;
        background: linear-gradient(92deg, rgba(176,161,121,0) 0%, rgba(176,161,121,0.3) 50%, rgba(176,161,121,0) 100%);
        // border-radius: 3px;
        font-size: 16px;
        font-family: Source Han Sans CN-Bold, Source Han Sans CN;
        font-weight: bold;
        color: #FFFFFF;
        text-shadow: 0px 0px 5px #FFD15B;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 17px;
        >span {
          margin-left: 13px;
          margin-right: 13px;
        }
        >img {
          width: 60px;
          height: 15px;
        }
      }
      >.corp-item {
        position: relative;
        width: 363px;
        height: 50px;
        background: linear-gradient(90deg, #3A454F 0%, rgba(22,28,33,0) 100%);
        backdrop-filter: blur(3px);
        border-radius: 3px 3px 3px 3px;
        opacity: 1;
        border: 1px solid;
        border-right: none;
        border-image: linear-gradient(98deg, rgba(78, 96, 112, 1), rgba(78, 96, 112, 0)) 1 1;
        padding-left: 72px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: flex-start;
        margin-bottom: 24px;
        cursor: pointer;
        >.item-icon {
          position: absolute;
          border-radius: 50%;
          left: 35px;
          top: 50%;
          transform: translateY(-50%);
          width: 8px;
          height: 8px;
          background: #6D9DC6;
          z-index: 2;
        }
        >.verticle-line {
          position: absolute;
          top: -1px;
          left: 38px;
          width: 2px;
          height: 75px;
          background: #B0A179 50%;
          z-index: 1;
        }
        &:first-of-type {
          >.verticle-line {
            top: 50%;
          }
        }
        &:last-of-type {
          >.verticle-line {
            top: initial;
            bottom: 50%;
          }
        }
        &:first-of-type:last-of-type {
          >.verticle-line {
            display: none;
          }
        }
        >span.corp-name {
          display: block;
          font-size: 16px;
          font-family: Source Han Sans CN-Bold, Source Han Sans CN;
          font-weight: bold;
          color: #FFFFFF;
          overflow: hidden;
          white-space: pre;
          text-overflow: ellipsis;
        }
        &.active {
          >span.corp-name {
            font-size: 20px;
            font-family: Source Han Sans CN-Bold, Source Han Sans CN;
            text-shadow: 0px 0px 16px #BD9D48;
          }
        }
        >span.corp-time {
          display: block;
          font-size: 16px;
          font-family: Source Han Sans CN-Regular, Source Han Sans CN;
          font-weight: 400;
          color: #FFFFFF;
        }
        &:hover {
          background: linear-gradient(90deg, #B0A179 0%, rgba(255,209,91,0) 100%);
          border-image: linear-gradient(98deg, rgba(176, 161, 121, 1), rgba(176, 161, 121, 0)) 1 1;
        }
        &.active {
          background: linear-gradient(90deg, #B0A179 0%, rgba(255,209,91,0) 100%);
          border-image: linear-gradient(98deg, rgba(176, 161, 121, 1), rgba(176, 161, 121, 0)) 1 1;
          >.item-icon {
            background: #FFFFFF;
            box-shadow: 0px 0px 12px 0px #FFD15B, 0px 0px 8px 0px #FFD15B, 0px 0px 10px 0px #FFD15B, 0px 0px 5px 0px #FFD15B;
          }
        }
      }
    }
    &::-webkit-scrollbar { background: transparent; width: 4px; } /*宽度是对垂直滚动条而言，高度是对水平滚动条而言*/
    &::-webkit-scrollbar-thumb {
      background: transparent;
      border-radius: 2px;
    }
    &:hover {
      &::-webkit-scrollbar-thumb {
        background: rgba(220, 231, 240, 0.2);
      }
    }
  }
  >article {
    position: absolute;
    top: 74px;
    right: 0;
    width: 653px;
    height: calc(100% - 74px - 112px - 50px);
    backdrop-filter: blur(5px);
    background-image: url(@/assets/images/general-article-bg.png);
    background-size: 100% auto;
    background-repeat: no-repeat;
    background-position: left top;
    padding: 32px 50px 50px 50px;
    display: flex;
    flex-direction: column;
    >button.close {
      position: absolute;
      top: 30px;
      right: 50px;
      width: 32px;
      height: 32px;
      background-image: url(@/assets/images/icon-close.png);
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center center;
    }
    >h2 {
      flex: 0 0 auto;
      font-size: 24px;
      font-family: Source Han Sans CN-Bold, Source Han Sans CN;
      font-weight: bold;
      color: #FFFFFF;
      margin-bottom: 21px;
    }
    >img.splitter {
      flex: 0 0 auto;
      width: 100%;
      margin-bottom: 37px;
    }
    >img.banner {
      flex: 0 0 auto;
      width: 100%;
      height: 34.8%;
      object-fit: contain;
      margin-bottom: 20px;
    }
    >.txt {
      flex: 1 0 1px;
      font-size: 20px;
      font-family: Source Han Sans CN-Regular, Source Han Sans CN;
      font-weight: 400;
      color: rgba(255, 255, 255, 0.8);
      line-height: 23px;
      overflow: auto;
      padding-right: 10px;
      margin-right: -10px;
      &::-webkit-scrollbar { background: transparent; width: 4px; } /*宽度是对垂直滚动条而言，高度是对水平滚动条而言*/
      &::-webkit-scrollbar-thumb {
        background: rgba(220, 231, 240, 0.2);
        border-radius: 2px;
      }
    }
  }
}
</style>