<template>
<div class="wrap">
  <div class="navBar">
    <van-nav-bar
        left-arrow
        left-arrow-color="#FFFFFF"
        :border="false"
        :fixed="true"
        :safe-area-inset-top="true"
        @click-left="goBack"
    >
      <template slot="title">
         <div v-show="!show">培训文档</div>
         <van-field v-model="keyword" placeholder="请输入关键词" v-show="show"   @input="getTrainingList"/>
      </template>
      <template slot="right">
        <div class="rightWrap" @click="show = !show">
          <img src="@/assets/serach.png" class="img2" v-show="!show"/>
          <img src="@/assets/delKey.png" class="img2" v-show="show" @click="initShow"/>
        </div>
      </template>
    </van-nav-bar>
  </div>
  <div class="tab_wrap">
    <div v-for="(item, index) in tabArr" :key="index" :class="{'active' : activeIndex == index,'active1':activeIndex==0}"
         @click="changeTab(index)">
      {{ item.name }}<span v-if="item.count" :class="{'commonNum':true,'notFinish':index==2}">{{item.count}}</span>
    </div>
  </div>
  <div class="contentWrap" v-show="!showEmpty">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh"  style="min-height:85vh">
      <div class="itemWrap" v-for="(item,index) in  pageList" :key="index" @click="goH5Detail(item,'培训文档')">
        <div class="info flexBetween common">
          <div class="title">{{ item.name }}<span v-if="item.mustRead===1" class="read">必读</span></div>
<!--          <div class="time">{{ item.pushTime		 }}</div>-->
        </div>
        <div class="imgWrap">
          <img :src="item.themePicture">
        </div>
        <div class="num common">{{ item.synopsis }}</div>
        <div  class="info flexBetween common" >
          <div class="time">{{ item.pushTime		 }}</div>
          <span style="color: red;font-size: 14px;margin-right: 8px" :class="item.alreadyRead===1 ? 'alRead' : ''">{{ item.alreadyRead===1 ? '已处理' :  '未处理' }}</span>
        </div>
      </div>
    </van-pull-refresh>
</div>
  <div class="empty" v-show="showEmpty">
    <img src="@/assets/empty.png" />
  </div>
</div>
</template>

<script>
import {myMixins} from "@/utils/myMixins"
import {getTrainingList} from  "@/api/mine"
export default {
  name: "trainDocment",
  computed: {
  },
  mixins:[myMixins],
  data(){
    return{
      tabArr: [{name: '全部文档', status: 1,count:0}, {name: '已培训', status:2,count:0},{name: '未培训', status: 3,count:0}],
      activeIndex: 0,
      pageNum:1,
      pageSize:10,
      pageList:[],
      totalList:[],
      keyword:'',
      serviceTypeName:'',
      show:false,
      showEmpty:false,
      isLoading:false,
      supplierId:'',
      numInfo:'',
    }
  },
  activated() {
    console.log('activated')
  },
  mounted() {
    console.log('mounted')
    const urlParams = new URLSearchParams(window.location.search);
    this.supplierId = urlParams.get('supplierId');
    this.keyword=urlParams.get('keyword') || ''
    this.serviceTypeName=urlParams.get('serviceTypeName') || ''
    this.getTrainingList();
    document.addEventListener('visibilitychange', async (  ) => {
      let state = document.visibilityState
      if (state == 'hidden') {  // 用户离开了
      }
      if (state == 'visible') {
        this.getTrainingList();
      }
    });
  },
  methods:{
    onRefresh() {
      this.getTrainingList()
      setTimeout(() => {
        this.$toast('刷新成功');
        this.isLoading = false;
      }, 1000);
    },
    async changeTab(index) {
      this.activeIndex = index
      await this.getTrainingList()
    },
    async getTrainingList(){
      let res= await getTrainingList({
        type:2,
        id: this.supplierId,
        text:this.keyword || '',
        serviceTypeName:this.serviceTypeName || ''
      })
      this.totalList=[]
      this.pageList=[]
      this.tabArr[0].count=0
      this.tabArr[1].count=0
      this.tabArr[2].count=0
      this.totalList=res?.data?.list ? res?.data?.list[0]?.materials : []
      this.numInfo=res.data
      if(res.data.totalNum){
        this.tabArr[0].count=res.data.totalNum
      }
      if(res.data.readNum){
        this.tabArr[1].count=res.data.readNum
      }
      if(res.data.notReadNum){
        this.tabArr[2].count=res.data.notReadNum
      }
      // console.log(' this.totalList',this.totalList)
      console.log(' this.tabArr',this.tabArr)
      let result=[]
      if(this.activeIndex === 0){
        result=this.totalList || []
        // result=this.totalList?.filter(q => q.title === '车型技术参数');
      }else if(this.activeIndex === 1){
        // result=this.totalList?.filter(q => q.title === '中道服务规范');
        result=this.totalList?.filter(item => item.alreadyRead==1) || []
      }else if(this.activeIndex === 2){
        result=this.totalList?.filter(item => item.alreadyRead!=1) || []
        // result=this.totalList?.filter(q => q.title === '中道小课堂');
      }
      if(result){
        this.pageList=result
      }
      if(this.pageList?.length === 0){
        this.showEmpty  = true
      }else {
        this.showEmpty = false
      }
    },
    initShow(){
      this.keyword= ''
      this.getTrainingList()
    }
  }
}
</script>
<style scoped lang="scss">
@import "@/styles/common.scss";
@import "@/styles/mixin.scss";
@import "@/styles/docment.scss";
.tab_wrap {
  .active:after {
    width: 37px;
    left: 0%;
    transform: translateX(0%);
  }
  .active1:after {
    width:48px !important;
  }
}
.read{
  padding: 2px 8px;
  border-radius: 5px;
  border: 1px solid red;
  color: red;
  font-size: 8px;
}
.alRead{
  color: #cccccc !important;
}
</style>