<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="message-content">
                    {{ item.content }},{{ item.createBy }}
                  </div>
                  <div class="bot_but" v-if=" item.theme != '检查'">
                    <van-button color="#33CCFF" size="mini">{{
                      item.status == 0 ? "立即处理" : "待办事项"
                    }}</van-button>
                  </div>
                </div>
              </van-badge>
        </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 { getFun,postFun } from '@/service/table'
import { waitList, removeWaitList } from "@/service/message";
import {debounce} from '@/utils/common.js'
export default {
  data() {
    return {
      searchVal: "",
      isLoading: false,
      activeIndex: 0,
      showIndex: null, // 是否显示遮罩层
      messageList: [],
      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.$parent.$parent.$parent.$parent.getBadgeNumber();
        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);
        }
        this.getList(formData)
     },500),
    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);
      }
      waitList(Data?Data:formData,obj)
        .then(res => {
          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++;
          }
          // this.$parent.$parent.$parent.$parent.getBadgeNumber();
        })
     
    },
    touchstart(index, 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,
                  });
      })
      // if (this.showIndex != null) {
      //   this.showIndex = null;
      //   return;
      // }
      // if(item.appUrl){
      //   this.$router.push({
      //   name: item.appUrl,
      //   params: {
      //     id: item.pid,
      //     mainId:item.mainId,
      //     path:item.appUrl,
      //     nodeId:item.nodeId,
      //     theme:item.theme
      //     }
      // })
      // }
      // this.showIndex = index;
    },
    closeOverlay() {
      this.showIndex = null;
    },
    onClick(data) {
       this.showIndex = null;
       if (data.businessDes == "" || data.taskName == "") return;
      // // 进入对应的操作页面
      // switch (data.businessDes + "-" + data.taskName) {
      //   // 作业审批的情况
      //   case "动火作业-作业负责人确认": case "有限空间作业-作业负责人确认":
      //     this.$router.push({
      //       name: "approval-handle",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-项目负责人审批": case "有限空间作业-项目负责人审批":
      //     this.$router.push({
      //       name: "project-handle",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-会签部室会签": case "有限空间作业-会签部室会签":
      //     this.$router.push({
      //       name: "safe-dept-handle",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-安全管理部门审批": case "有限空间作业-安全管理部门审批":
      //     this.$router.push({
      //       name: "safe-officer-handle",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-主管领导审批": case "有限空间作业-主管领导审批":
      //     this.$router.push({
      //       name: "executive-director",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   // 现场作业的情况
      //   case "动火作业-重点人员签到":
      //     this.$router.push({
      //       name: "hot-work-confirm",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-现场人员签字":
      //     this.$router.push({
      //       name: "site-confirm",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-结束作业":
      //     this.$router.push({
      //       name: "work-patrol",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "动火作业-安全部门结束确认":
      //     this.$router.push({
      //       name: "check-accept",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "有限空间作业-重点人员签到":
      //     this.$router.push({
      //       name: "job-affirm",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "有限空间作业-交底上传":
      //     this.$router.push({
      //       name: "job-sign",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "有限空间作业-安全措施确认":
      //     this.$router.push({
      //       name: "safe-disclosure",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   case "有限空间作业-作业前确认":
      //     this.$router.push({
      //       name: "begin-work",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;

      //   case "有限空间作业-结束作业":
      //     this.$router.push({
      //       name: "end-work",
      //       params: {
      //         data: data
      //       }
      //     });
      //     break;
      //   default:
      //     break;
      // }
    },
     // 点击类别
    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 {
  min-height: 58vh;
  padding: 10px 10px 0;
  background-color: #f0f1f5;
  // 标签
  .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;
    }
  }
  // 搜索
  .search-wrap {
    margin: 0 0 10px 0;
    .van-search {
      border-radius: 5px;
      padding: 5px 12px;
    }
    .van-search__content {
      background-color: #ffffff;
    }
  }
  //  内容
  .con-list {
    .van-cell-group--inset {
      margin: 0;
      margin-bottom: 10px;
      font-size: 13px;
      padding: 10px;
      .message-content {
        margin-top: 10px;
      }
      .messgae-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .bot_but {
        display: flex;
        justify-content: right;
        align-items: center;
      }
    }
  }
}
</style>
