<template>
  <div class="c-panel">
    <div class="c-panel-header">
      <div class="avatar-wrap" @click="$showUserInfo" v-if="userInfo">
        <img v-if="userInfo.cover_photo" :src="userInfo.cover_photo" alt="">
        <div v-else class="user-avatar"
          :class="'avatar_bg' + userInfo.user_id % 9"
          :data-name="userInfo.nick_name && userInfo.nick_name.slice(0,2).toUpperCase()"></div>
      </div>
      <ul class="panel-ctrls">
        <li class="current">
          <i class="mee-icon-chat"></i>
        </li>
        <!-- <li>
          <i class="friend-icon-chat"></i>
        </li> -->
      </ul>
      <div class="panel-setting">
        <div :class="['panel-nav-icon',{'active':isShowSetting}]" @click.stop="toggleLoginOut()"></div>
        <ul :class="['menu',{'active':isShowSetting}]">
          <!-- <li class="item"  @click.stop="handleLoginOut">
            <i class="icon-mute icon-mute0"></i>关闭声音
          </li> -->
          <li class="item"  @click.stop="handleGlobalMute">
            <i :class="['icon-msg','icon-msg'+openGlobalNotice]"></i>{{openGlobalNotice==0?$t('public.closeMsg'):$t('public.openMsg')}}
          </li>
          <li class="item"  @click.stop="handleFeedback">
            <i class="icon-feedback"></i>{{$t('public.feedback')}}
          </li>
          <li class="item"  @click.stop="changeLang">
            <i class="icon-lang"></i>{{curLang=="en"?$t('public.chinese'):$t('public.english')}}
          </li>
          <li class="item"  @click.stop="handleLoginOut">
            <i class="icon-logout"></i>{{$t('public.quit')}}
          </li>
        </ul>
      </div>
    </div>

    <div class="c-panel-nav">
      <div class="panel-searbar">
        <div class="input-con">
          <i class="el-icon-search"></i>
          <input type="text" @input="searchUser($event, sessionList)" :placeholder="$t('public.search')">
        </div>
        <i class="el-icon-plus" @click="$showInvite(1)"></i>
      </div>
      <router-link to="/hotGroup">
        <div :class="['group-recommand',{'current':curSession=='0'}]">
          <i class="group-icon"></i>
          <p class="title">{{ $t('chat.hotGroupRecommend') }}<i class="el-icon-arrow-right"></i></p>
        </div>
      </router-link>
      <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>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import sessionItem from './sessionItem'
import { searchUserMixin, addPanelSessionMixin, changeLangMixin } from '@/mixins'
import { noticeManager } from '@/util/util'
import API from '@/api'

export default {
  name: 'panel',
  mixins: [searchUserMixin, addPanelSessionMixin, changeLangMixin],
  data () {
    return {
      isShowSetting: false,
      openGlobalNotice: noticeManager.getGlobalNotice()
    }
  },
  computed: {
    ...mapState({
      curSession: state => state.curSession,
      userId: state => state.userId,
      sessionList: state => state.chat.sessionList,
      userInfo: state => state.userInfo
    })
  },
  components: {
    sessionItem
  },
  methods: {
    toggleLoginOut (flag = true) {
      if (flag) this.isShowSetting = !this.isShowSetting
      else this.isShowSetting = false
    },
    handleLoginOut () {
      // 初始vuex数据
      this.$store.commit('setUserInfo', null)
      this.$store.commit('initChatData')
      this.$store.commit('initGroupData')

      this.$store.dispatch('doScatterLogout')
      this.$store.commit('chatAppLogin', false)
      this.$store.commit('toApp', false)
      this.isShowSetting = false
    },
    handleFeedback () {
      API.base.feedback().then(({ data }) => {
        let serverId = data.data.serverId
        let sessionId = this.userId > serverId
          ? `${serverId}-${this.userId}`
          : `${this.userId}-${serverId}`

        this.addPanelSession(serverId, sessionId)
        this.$store.commit('changeSessionId', sessionId)
        this.$router.push({ path: `/pm/${sessionId}` })
        this.isShowSetting = false
      })
    },
    handleGlobalMute () {
      let flag = this.openGlobalNotice == 0 ? 1 : 0
      noticeManager.setGlobalNotice(flag, this)
    }
  },
  async created () {
    this.$store.dispatch('getSessionList')
    await this.$store.dispatch('getUserInfo')
    if (location.hash.match('user')) {
      this.$showUserInfo()
    }
  },
  mounted () {
    document.addEventListener('click', () => {
      this.toggleLoginOut(false)
    })
  }
}
</script>

<style lang="scss" scoped>
@import './panel.scss';
</style>
