<!--
<chat-log-list :brandId="brandId" :showFlag='showFlag' @hideDetail="hideDetail"></chat-log-list>
import chatLogList from '@/components/app/chat-log-list.vue';
components: {
  chatLogList
}
-->

<template>
  <div :class="['chat-log-detail', !showFlag ? 'hide-detail' : '']">
    <div class="chat-log-detail_top border-box" @click="hideLog">
      <i class="iconfont icon-zhankai"></i>
    </div>
    <ul class="chat-log-list border-box" v-infinite-scroll="load">
      <li v-for="(item, index) in chatDetailData" class="infinite-list-item" :key="index">
        <div v-if="item.type == 1" class="chat-list-cell flex">
          <div class="chat-list-cell_img"><img :src="item.headPic" alt="" /></div>
          <div class="chat-list-right flex flex-column flex-space-between">
            <div class="chat-list-user font-12 color-909399">
              <span>{{ item.name }} </span> <span>{{ item.createTime | formatTimeYmdHms }}</span>
            </div>
            <div class="chat-list-msg font-14 color-303133"></div>
          </div>
        </div>
        <div v-if="item.type == 1" class="chat-list-cell flex">
          <div class="chat-list-cell_img"><img :src="item.headPic" alt="" /></div>
          <div class="chat-list-right flex flex-column flex-space-between">
            <div class="chat-list-user font-12 color-909399">
              <span>{{ item.name }} </span> <span>{{ item.createTime | formatTimeYmdHms }}</span>
            </div>
            <div class="chat-list-msg font-14 color-303133"></div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import { postRequest } from '@/api/api';
import errMsg from '@/common/js/error';
export default {
  name: 'chat-log-list',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    },
    showFlag: {
      type: Boolean,
      default() {
        return false;
      }
    },
    msgId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      chatDetailData: [],
      pageSize: 20,
      pageNum: 1,
      total: 0
    };
  },
  methods: {
    /**
     * 隐藏详情
     */
    hideLog() {
      const that = this;
      that.chatDetailData = [];
      that.pageNum = 1;
      that.total = 0;
      that.$emit('hideDetail');
    },
    load() {
      const that = this;
      if (that.chatDetailData.length == that.total) {
        return false;
      }
      that.getData(that.chatDetailData[that.chatDetailData.length - 1].userId);
    },
    /**
     * 获取消息详情
     */
    getData(lastId) {
      const that = this;
      let para = {
        brandId: that.brandId,
        pageSize: that.pageSize,
        pageNum: that.pageNum
      };
      postRequest('/haoban-app-member-web/customer/find_message_detail', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (resData.result.list && resData.result.list.length) {
              resData.result.list.forEach(ele => {
                that.chatDetailData.push(ele);
              });
            } else {
              that.chatDetailData = [];
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeBrand = newData;
        that.getData();
      }
    },
    activeGroupId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    },
    showFlag: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.getData();
      }
    }
  }
};
</script>
<style lang="less" scoped>
.chat-log-detail {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 399px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -ms-transition: all 0.3s;
  transition: all 0.3s;
  overflow: hidden;
  z-index: 100;
  background: #fff;
  &.hide-detail {
    right: -399px;
  }
  .chat-log-detail_top {
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding-left: 15px;
    border-bottom: 1px solid #e4e7ed;
    cursor: pointer;
    i {
      font-size: 13px;
      color: #c0c4cc;
    }
  }
  .chat-log-list {
    height: calc(100vh - 50px);
    padding: 0 10px 0 12px;
    li {
      margin-bottom: 40px;
      .chat-list-cell {
        .chat-list-cell_img {
          width: 36px;
          height: 36px;
          border-radius: 50%;
          img {
            width: 36px;
            height: 36px;
            border-radius: 50%;
          }
        }
      }
    }
  }
}
</style>
