<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?'关闭消息':'开启消息'}}
          </li>
          <li class="item"  @click.stop="handleFeedback">
            <i class="icon-feedback"></i>意见反馈
          </li>
          <li class="item"  @click.stop="handleLoginOut">
            <i class="icon-logout"></i>退出
          </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="搜索">
        </div>
        <i class="el-icon-plus" @click="$showInvite(1)"></i>
      </div>
      <!-- <div class="group-recommand">
        <i class="group-icon"></i>
        <p class="title">熱門群組推薦<i class="el-icon-arrow-right"></i></p>
      </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>
  </div>
</template>

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

export default {
  name: 'panel',
  mixins: [searchUserMixin, addPanelSessionMixin],
  data () {
    return {
      isShowSetting: false,
      openGlobalNotice: noticeManager.getGlobalNotice()
    }
  },
  computed: {
    ...mapState({
      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 () {
      this.$store.dispatch('doScatterLogout')
      this.$store.commit('chatAppLogin', false)
      this.$store.commit('toApp', false)
      this.isShowSetting = false
    },
    handleFeedback () {
      API.base.feedback({
        group_id: this.curGroupId
      }).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 () {
      this.openGlobalNotice = this.openGlobalNotice == 0 ? 1 : 0
      noticeManager.setGlobalNotice(this.openGlobalNotice)
    }
  },
  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>
