<template>
  <div class="chat-list">
    <div class="top-search">
      <a class="meechat-icon" href="https://www.mee.chat" target="_blank"></a>
      <!-- <input type="text" class="search-wrap" @input="searchUser($event, sessionList)" @blur="handleBlur" v-focus v-if="searchShow"> -->
      <!-- <input type="text" class="search-wrap" @focus="toSearch" @click="toSearch" v-if="searchShow"> -->
      <div class="search-tips search-wrap" @click="toSearch">
        <i class="el-icon-search"></i>{{$t('public.searchHotGroup')}}
      </div>
      <router-link class="add-icon" to="/invite/1"></router-link>
    </div>
    <div class="pwa-guide" id="btnPwa" style="display:none">
      <span>{{$t('public.installPwa')}}</span>
      <div class="btn-close">
        <i class="el-icon-close" @click.stop="closePwaGuide()"></i>
      </div>
    </div>
    <div class="chat-list pub-scroll-box">
      <template v-if="!isSearch">
        <session-item
          v-for="(item, index) in sessionList"
          :key="index"
          :item="item">
        </session-item>
      </template>
      <template v-else>
        <session-item
          v-for="(item, index) in searchList"
          :key="index"
          :item="item">
        </session-item>
      </template>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import sessionItem from '@/components/panel/sessionItem'
import { searchUserMixin } from '@/mixins'

export default {
  name: 'chatList',
  mixins: [searchUserMixin],
  components: {
    sessionItem
  },
  data () {
    return {
      searchShow: false
    }
  },
  computed: {
    ...mapState({
      sessionList: state => state.chat.sessionList,
      userInfo: state => state.userInfo
    })
  },
  methods: {
    ...mapMutations(['clearHash']),
    handleBlur (e) {
      let val = e.target.value
      if (!val) {
        this.searchShow = false
      }
    },
    toSearch () {
      this.$router.push({ path: `/search` })
    },
    closePwaGuide () {
      let btnPwa = document.getElementById('btnPwa')
      btnPwa.style.display = 'none'
    }
  },
  created () {
    this.$store.dispatch('getSessionList')
    if (this.userInfo && this.userInfo.user_id) this.$store.dispatch('getUserInfo')
    this.$store.commit('setUserId', localStorage.getItem('user_id'))
  },
  mounted () {
    this.clearHash()
  }
}
</script>

<style lang="scss" scoped>
.chat-list{
  background-color: #ffffff;
  overflow-x: hidden;
}
.top-search{
  height: px2rem(88);
  background-color: #f2f2f2;
  display: flex;
  align-items: center;
  > i{
    margin: 0 px2rem(20);
  }
  .search-wrap{
    flex: 1;
    background-color: #ffffff;
    border-radius: 4px;
    border: 1px solid #eeeeee;
    height: px2rem(60);
    line-height: px2rem(60);
    box-sizing: border-box;
    font-size: px2rem(28);
  }
  input{
    padding-left: px2rem(10);
    font-size: px2rem(28);
    color: #666666;
  }
}
.pwa-guide{
  position: relative;
  height: px2rem(81);
  line-height: px2rem(81);
  color: #438aff;
  font-size: px2rem(30);
  text-align: center;
  &::after{
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    border-bottom: 1px solid #d8d8d8;
    transform: scaleY(0.5);
  }
  .btn-close{
    position: absolute;
    top: 0;
    right: 0;
    width: px2rem(75);
    height: 100%;
    color: #999;
  }
}
.meechat-icon{
  display: inline-block;
  margin: 0 px2rem(20);
  background: url('../../../assets/h5/meechat-logo.png') 0 0 / 100% no-repeat;
  width: px2rem(42);
  height: px2rem(39);
}
.add-icon{
  background: url('../../../assets/more-icon.png');
  width: px2rem(42);
  height: px2rem(42);
  margin: 0 px2rem(20);
  background-size: 100%;
}
.search-tips{
  color: #8e8e93;
  i{
    display: inline-block;
    vertical-align: middle;
    margin: 0 px2rem(6) 0 px2rem(27);
    font-size: px2rem(26);
  }
}
</style>
