<template>
<div class="message">
  <div v-if="!show" class="page-container">
    <van-tabs v-model="query.type" @change="refreshData">
      <van-tab title="公告/通知" name="1"></van-tab>
      <van-tab title="业务提醒" name="2"></van-tab>
      <van-tab title="消息" name="3"></van-tab>
    </van-tabs>
    <div class="card-list">
      <Scroll
        ref="Scroll"
        @refresh="refreshData"
        @loadMore="getDataList"
        :pullup="pullup">
        <empty v-if="!dataList.length" />
        <card class="list-item" v-else v-for="(v,i) in dataList" :key="i">
          <van-cell :title="v.title" :label="v.content" @click="clickItem(v)">
            <template #right-icon>
              <i  class="point-icon" :class="{'active':!v.isRead}" />
            </template>
            <template #label>
              <div class="cell-label">
                <p class="text-style">{{v.content}}</p>
              </div>
              <p class="cell-time">{{v.publishTime}}</p>
            </template>
          </van-cell>
        </card>
      </Scroll>
    </div>
  </div>

  <!-- 消息详情 -->
  <div v-else class="message-detail">
    <van-nav-bar
      title="消息详情"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="detail-box" v-if="selected">
      <div class="msg-title">
        <p>{{selected.title}}</p>
        <p class="cell-time">{{selected.publishTime}}</p>
      </div>
      <div class="message-content">
        <div class="text-content">
          <p>{{selected.content}}</p>
          <div v-if="selected.fileList" class="file-box" >
            附件：
            <div v-for="(v,i) in selected.fileList">
              <van-icon color="#1989fa" name="guide-o" />
              <span style="color: #1989fa;" @click="preview(v)"  :key="v.name">{{v.name}}</span>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>

</div>
</template>

<script>
import Scroll from '@/components/scroll/scroll'
import {msgList, dataList, setRead, msgRead} from './api'
import {mapGetters} from "vuex"

export default {
  components: {
    Scroll,
  },
  data() {
    return {
      show:false,
      selected:null,
      dataList: [],
      query:{
        type:1,
        pageNum:1,
        pageSize:10,
      },
      pullup:true,
      message:null,
      total:0,
    };
  },
  computed:{
    ...mapGetters(['orgId','id','roleList'])
  },
  mounted(){
    this.getDataList();
  },
  methods:{
    preview(file){
      this.openFilePreview(file);
    },
    onClickLeft(){
      this.show = false;
    },
    clickItem(v){
      let queryMethod,data;
      if(this.query.type == 1){
        queryMethod = setRead;
        data = {
          announcementNotificationId:v.id,
          orgId:this.orgId,
          userId:this.id,
        }
      }else {
        queryMethod = msgRead;
        data = v.id
      }
      queryMethod(data).then(res=>{
        this.dataList.forEach(item=>{
          if(item.id === v.id){
            item.isRead = true;
          }
        })
      })
      this.selected = v;
      this.show = true;
    },
    refreshData(){
      this.pullup = true;
      this.query.pageNum = 1;
      this.total = 0;
      this.dataList = [];
      this.getDataList();
    },
    getDataList(){
      if( this.dataList.length !== 0 && this.dataList.length >= this.total) {
        this.pullup = false;
        this.$toast('已加载完毕');
        return;
      }
      let data = {};
      if(this.query.type == 1){
        data = {
          ...this.query,
          userId:this.id,
          orgId:this.orgId,
          tagRoleIds:this.roleList.map(v=>{return v.roleId}),
        }
        msgList(data).then(res=>{
          if(res.total === '0'){
            this.pullup = false;
            this.$toast('已加载完毕');
            return
          }
          this.total = res.total;
          if(this.dataList.length < res.total) {
            this.dataList = [...this.dataList,...res.rows] ;
            this.pullup = true;
            this.query.pageNum++;
            this.$refs.Scroll.refresh();
          }
        })
        return;
      }
      data = {
        ...this.query,
        userId:this.id,
      }
      dataList(data).then(res=>{
        if(res.total === '0'){
          this.pullup = false;
          this.$toast('已加载完毕');
          return
        }
        this.total = res.total;
        if(this.dataList.length < res.total) {
          this.dataList = [...this.dataList,...res.rows] ;
          this.pullup = true;
          this.query.pageNum++;
          this.$refs.Scroll.refresh();
        }
      })
    }
  }
}
</script>
<style lang="scss">
.message{
  //.van-empty{
  //  background-color: #fff;
  //}
  .list-item{
    .van-cell{
      padding: 0;
    }
  }
  .van-tab{
    color: #ccc;
  }
  .van-tabs__nav{
    background-color: #008cd6;
  }
  .van-tab--active{
    color: #fff;
  }
  .van-cell-group{
    background-color: #fff;
  }
  .van-tabs__line{
    background-color: #fff;
  }
}
</style>
<style lang="scss" scoped>
.message{

}
.card-list{
  padding:0 20px 20px 20px;
  height: calc(100vh - 190px);
  overflow:  auto;
}
.list-item{
  //margin-bottom: 20px;
  //&:last-child{
  //  margin-bottom: 0;
  //}
}
.cell-label{
  width: 100%;
  padding: 10px;
  min-height: 50px;
  line-height: 36px;
  max-height: 250px;
  overflow: hidden;
  >p{
    word-break: break-word;
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    line-height: 40px;
  }
}
.cell-time{
  text-align: right;
  height: 30px;
  font-size: 24px;
  color:#aaa;
}
.point-icon{
  width: 20px;
  height: 20px;
  background-color:#ccc;
  border-radius: 50%;
  margin-top: 10px;
  display: none;
  &.active{
    display: block;
    background-color: #03a5ff;
  }
}
.message-detail{
  height: calc(100vh - 102px);

}
.detail-box{
  height: calc(100% - 90px);
  overflow: auto;
  background-color: #fff;
}
.msg-title{
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  width: 100%;
  padding: 2.666667vw 4.266667vw;
  overflow: hidden;
  color: #323233;
  font-size: 3.733333vw;
  line-height: 6.4vw;
  border-bottom: 1px solid #f5f5f5;
}
.message-content{
  width: 100%;
  padding:20px 30px 20px 30px;
  height: 100%;
  overflow: auto;
  .text-content{
    width: 100%;
    min-height: 40px;
    overflow: auto;
    >p{
      text-indent: 2em;
      word-break: break-word;
      white-space: pre-wrap;
      text-align: justify;
      width: 100%;
      color: #777;
    }
  }
  .file-box{
    width: 100%;
    min-height: 40px;
    padding: 20px 0;
    >div{
      line-height: 32px;
      padding-left: 20px;
      margin: 10px 0;
    }
  }
}
</style>
