<template>
  <div>
    <van-pull-refresh
      v-model="isLoading"
      success-text="刷新成功"
      @refresh="onRefresh"
    >
    <div class="message-wrap">
      <!-- 搜索 -->
      <div class="search-wrap">
        <van-search v-model="searchVal" placeholder="搜索" @input="onSearch" />
      </div>
       <!-- 标签 -->
        <div class="category-wrap">
          <span
            class="my-tag-style"
            :class="index == activeIndex ? 'active' : ''"
            v-for="(item, index) in messageCategory"
            :key="item.key"
            @click="clickCategory(index, item.category)"
            >{{ item.category }}</span
          >
        </div>
      <!-- 内容列表 -->
      <div class="con-list" @touchmove="showIndex = null">
        <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="getList()"
          >
        <van-cell-group
          inset
          v-for="(item, index) in messageList"
          :key="index"
          @click="touchstart(index, item)"
        >
         <van-badge :dot="item.status == 0 ? true : false">
              <div style="padding: 10px; min-width: calc(100vw - 1.6rem)">
                <div class="messgae-title">
                  <span>{{ item.title }}</span>
                  <span>{{ item.createTime }}</span>
                </div>
                <div class="mse_title">
                  <!-- {{ item.title }} -->
                </div>
                <div class="message-content">
                  {{ item.content }}
                </div>
              </div>
            </van-badge>
            <van-overlay :show="showIndex == index">
              <div class="wrapper" @click.stop="closeOverlay">
                <van-button round color="#4ABD85" @click="readme(item)">
                  详情
                </van-button>
              </div>
            </van-overlay>
        </van-cell-group>
        <van-image
            v-if="messageList.length <= 0"
            fit="contain"
            :src="require('../../../assets/noMessage.png')"
          />
      </van-list>
      </div>
    </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import {
  notificationList,
  removeAllNotification,
  readNotification,
  removeNotification,
  postMessgaelist
} from "@/service/message";
import { getFun,postFun } from '@/service/table'
import {debounce} from '@/utils/common.js'
export default {
  data() {
    return {
      searchVal: "",
      isLoading: false,
      showIndex: null, // 是否显示遮罩层
      activeIndex: 0,
      activeVal: "全部",
      messageList: [],
      completeList: [],
      messageCategory: [
        {
          key: "0",
          category: "未读"
        },
        {
          key: "1",
          category: "已读"
        },
        {
          key: "2",
          category: "全部"
        },
      ],
      loading: false, // 列表滚动到底部会触发load事件
      finished: false, // 列表数据全部加载完成
      pageSize: 10,
      pageNum: 1,
    };
  },
  mounted() {
  },
  methods: {
    //下拉刷新
    onRefresh() {
      setTimeout(() => {
        this.$toast("刷新成功");
        this.isLoading = false;
        this.reset()
        this.getList();
        this.$emit('getList')
      }, 1000);
    },
    // 搜索事件
    onSearch:debounce(function(){
        this.messageList=[]
        this.pageNum=1
        this.finished = false
        this.loading = true
        if (this.activeIndex == 0) {
          let formData = new FormData();
          formData.append("status", 0);
          formData.append("keyword", this.searchVal);
          this.getList(formData);
        } else if (this.activeIndex == 1) {
          let formData = new FormData();
          formData.append("status", 1);
          formData.append("keyword", this.searchVal);
          this.getList(formData);
        } else if (this.activeIndex == 2) {
          let formData = new FormData();
          formData.append("keyword", this.searchVal);
          this.getList(formData);
        }
     },800),
    getList(Data) {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0
      });
      var obj={
            pageSize:this.pageSize,
            pageNum:this.pageNum,
        }
      let formData
      if (this.activeIndex == 0) {
        formData = new FormData();
        formData.append("status", 0);     
      } else if (this.activeIndex == 1) {
        formData = new FormData();
        formData.append("status", 1);
      } else if (this.activeIndex == 2) {
        formData = new FormData()
      }
      if(this.searchVal){
        formData.append("keyword", this.searchVal);
      }
      postMessgaelist(Data?Data:formData,obj).then((res) => {
          this.$toast.clear();
          // this.messageList = res.rows.filter(item => {
          //   return item.status == "未读";
          // });

          this.messageList.push(...res.rows)
          this.loading = false;
          if (this.messageList.length >= res.total) {
                  this.finished = true;
          }
          if(!(this.pageNum*this.pageSize-res.total>=0)){
                  this.pageNum++;
          }
        })
        .catch(() => {
          this.$toast.clear();
          this.$toast.fail('加载失败,请稍后再试');
        });
    },
    readme(item){
      let formData=new FormData()
      formData.append('id',item.id)
      postFun("messageInfo/changeRead", formData)
                .then(res => {
                  this.showIndex = null;
                  this.$router.push({
                    path: item.appUrl,
                  });
      })
    },
    touchstart(index, item) {
      if (this.showIndex != null) {
        this.showIndex = null;
        return;
      }
      this.showIndex = index;
    },
    closeOverlay() {
      this.showIndex = null;
    },
    // 点击类别
    clickCategory(index, data) {
      this.showIndex = null;
      this.activeIndex = index;
      this.reset()
      this.getList()
    },
    reset(){
        this.messageList=[]
        this.pageNum=1
        this.finished = false
        this.loading = true
    },
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
.message-wrap {
  padding: 10px 10px 0;
  background-color: #f0f1f5;
  // 标签

  // 搜索
  .search-wrap {
    margin: 0 0 0.26667rem 0;
    .van-search {
      border-radius: 5px;
      padding: 5px 12px;
    }
    .van-search__content {
      background-color: #ffffff;
    }
  }
    .category-wrap {
    margin-bottom: 10px;
    .my-tag-style {
      width: 75px;
      height: 21px;
      font-size: 13px;
      border: 1px solid #1989fa;
      border-radius: 4px;
      color: #1989fa;
      display: inline-block;
      text-align: center;
      line-height: 21px;
      margin-right: 10px;
    }
    .active {
      color: white;
      background-color: #1989fa;
    }
  }
  //  内容
  .con-list {
    .van-cell-group--inset {
      margin: 0;
      margin-bottom: 0.266667rem;
      padding: 0.266667rem;
      position: relative;
      .approval-title{
        font-size: 0.4rem;
        font-weight: bold;
        line-height: 0.7rem;
        color: #000;
        .title-icon{
            text-align: center !important;
            font-weight: normal;
            background-color: #2980f7;
            color: white;
            font-size: .32rem;
            padding: .026667rem .08rem;
            height: -webkit-fill-available;
            display: inline;
            margin-left: .266667rem;
            position: relative;
            top: -0.026667rem;
        }
      }
      .row{
          line-height: 0.7rem;
          font-size: 0.4rem;
          display: flex;
          flex: none;
          .row-title {
          width: 1.733333rem;
          display: inline-block;
          text-align-last: justify;
          flex: inherit;
          }
      }
      .status {
        display: inline-block;
        padding: 3px 8px;
        position: absolute;
        top: 0.4rem;
        right: 0.586667rem;
      }
      .van-overlay {
        position: absolute;
        .wrapper {
          display: flex;
          align-items: center;
          justify-content: space-evenly;
          height: 100%;
          .van-button__text {
            width: initial;
          }
        }
      }
    }
  }
}
</style>
