<template>
  <div>
    <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-cell-group
          inset
          v-for="(item, index) in messageList"
          :key="index"
          @click="touchstart(index, item)"
        >
         <div class="approval-title">{{ item.noticeTitle }} <span class="title-icon">待处理</span></div> 
         <!-- <div class="row"><span class="row-title">作业类型</span>：<span>{{ item.id }}</span></div> -->
          <div class="row"><span class="row-title">作业类型</span>：<span>{{ item.jobType }}</span></div>
          <div class="row"><span class="row-title">作业单号</span>：<span>{{ item.pid }}</span></div>
          <div class="row"><span class="row-title">发送人</span>：<span>{{ item.createName }}</span></div>
          <div class="row"><span class="row-title">消息内容</span>：<span>{{ item.noticeContent }}</span></div>
          <div class="row"><span class="row-title">发送时间</span>：<span>{{ item.sendTime }}</span></div>
           <van-tag
              class="status"
              :color="item.status == '已读' ? '#07c160' : '#ee0a24'"
              plain
            >
              {{ item.status}}
            </van-tag>
          <van-overlay :show="showIndex == index">
            <div class="wrapper" @click.stop="closeOverlay">
              <van-button round type="info" @click="onClick(item)">
                查看
              </van-button>
            </div>
          </van-overlay>
        </van-cell-group>
      </div>
    </div>
  </div>
</template>

<script>
import { getFun,postFun } from '@/service/table'
import {debounce} from '@/utils/common.js'
export default {
  data() {
    return {
      searchVal: "",
      showIndex: null, // 是否显示遮罩层
      activeIndex: 0,
      activeVal: "全部",
      messageList: [],
      completeList: [],
      messageCategory: [
        {
          key: "0",
          category: "未读"
        },
        {
          key: "1",
          category: "已读"
        },
        {
          key: "2",
          category: "全部"
        },
      ],
    };
  },
  mounted() {
    this.getList(this.searchVal);
  },
  methods: {
    // 搜索事件
    // 搜索事件
    onSearch:debounce(function(){
      if(this.searchVal){
        this.getList(this.searchVal)
      }else{
        this.getList("")
      }
     },800),
    getList(val) {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0
      });
      let formdata = new FormData()
      formdata.append("search", val);
      postFun("mobile/notice", formdata)
        .then(res => {
          this.$toast.clear();
          this.messageList = res.rows.filter(item => {
            return item.status == "未读";
          });
          this.completeList = res.rows;
        })
        .catch(() => {
          this.$toast.clear();
          this.$toast.fail('加载失败,请稍后再试');
        });
    },
    touchstart(index, item) {
      if (this.showIndex != null) {
        this.showIndex = null;
        return;
      }
      this.showIndex = index;
    },
    closeOverlay() {
      this.showIndex = null;
    },
    onClick(data) {
      let updateId = data.id
      this.showIndex = null;
      if (data.jobType == "" || data.pid == "" || data.noticeTitle == "") return;
      // 进入对应的操作页面
      switch (data.jobType + "-" + data.noticeTitle) {
        // 作业审批的情况
        case "动火作业-审批通过":  case "动火作业-开始作业": case "动火作业-结束作业":
          this.$router.push({
            name: "detail-work",
            params: {
              id: data.pid
            }
          });
          break;
        case "有限空间作业-审批通过":  case "有限空间作业-开始作业": case "有限空间作业-结束作业":
          this.$router.push({
            name: "limit-detail",
            params: {
              id: data.pid
            }
          });
          break;
        case "动火作业-问题上报":  case "动火作业-问题处置": case "动火作业-督导提醒":
            // 把 data.中的pid 改为 id 在跳转  
            data.id = data.proId 
            this.$router.push({
                name: "prorec-detail",
                params: {
                data: data
                }
            });
             break;
        default:
          break;
      }
      // 把未读的消息改为已读状态
      if(data.status == "未读"){
        let formdata = new FormData()
        formdata.append("id", updateId)
        postFun("mobile/updateStatus", formdata )
        .then(res => {})
        .catch(() => {
          this.$toast.fail('状态更新失败');
        });
      }
    },
    // 点击类别
    clickCategory(index, data) {
      // 关闭遮罩层
      this.showIndex = null;
      this.activeIndex = index;
      this.activeVal = this.messageCategory[index].category;
      // 过滤数组
      switch (this.activeVal) {
        case "未读":
          this.messageList = this.completeList.filter(item => {
            return item.status == "未读";
          });
          break;
        case "已读":
          this.messageList = this.completeList.filter(item => {
            return item.status == "已读";
          });
          console.log(this.messageList);
          break;
        default:
          this.messageList = this.completeList;
          break;
      }
    }
  }
};
</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>
