<template>
  <div class="app-container home">
    <el-card class="box-card home-top">
      <div class="count-box">
        <div class="count-item">
          <div class="count-left">
            <img src="@/assets/images/home-icon1.jpg" />
          </div>
          <div class="count-right">
            <div class="count-count">{{count.formTodayCount}}</div>
            <div class="count-tip">{{$t('page.business.home.jrxzbd')}}</div>
          </div>
        </div>
        <div class="count-item">
          <div class="count-left">
            <img src="@/assets/images/home-icon1.jpg" />
          </div>
          <div class="count-right">
            <div class="count-count">{{count.formYtjCount}}</div>
            <div class="count-tip">{{$t('page.business.home.ytjbd')}}</div>
          </div>
        </div>
        <div class="count-item">
          <div class="count-left">
            <img src="@/assets/images/home-icon1.jpg" />
          </div>
          <div class="count-right">
            <div class="count-count">{{count.formTbzCount}}</div>
            <div class="count-tip">{{$t('page.business.home.dtjbd')}}</div>
          </div>
        </div>
        <div class="count-item">
          <div class="count-left">
            <img src="@/assets/images/home-icon1.jpg" />
          </div>
          <div class="count-right">
            <div class="count-count">{{count.studyWeekCount}}</div>
            <div class="count-tip">{{$t('page.business.home.bzxzsy')}}</div>
          </div>
        </div>
        <div class="count-item">
          <div class="count-left">
            <img src="@/assets/images/home-icon1.jpg" />
          </div>
          <div class="count-right">
            <div class="count-count">{{count.studyFinishCount}}</div>
            <div class="count-tip">{{$t('page.business.home.ywcsy')}}</div>
          </div>
        </div>
      </div>
    </el-card>

    <el-card class="box-card home-content">
      <div slot="header" class="clearfix">
        <span>系统通知</span>
      </div>
      <div class="notice-list" v-loading="loading">
        <div class="notice-item" v-for="(item,index) in noticeList" :key="index" @click="gotoPage(item)">
          <div class="notice-title"><el-badge is-dot class="notice-dot" v-if="item.status===1"></el-badge>{{item.title}}</div>
          <div class="notice-date">{{item.createTime}}</div>
        </div>

        <pagination
          v-show="noticeTotal>0"
          :total="noticeTotal"
          :page.sync="searchForm.pageNum"
          :limit.sync="searchForm.pageSize"
          @pagination="getNoticeList"
        />
      </div>

    </el-card>
  </div>
</template>

<script>
import { home_count,home_noticeList,home_noticeRead} from "@/api/business/home/home";
export default {
  name: "Index",
  data() {
    return {
      count:{
        formTodayCount:'',
        formYtjCount:'',
        formTbzCount:'',
        studyFinishCount:'',
        studyWeekCount:'',
      },
      loading:false,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
      },
      noticeList:[],
      noticeTotal:0
    }
  },
  created(){
    this.getCount()
    this.getNoticeList()
  },
  methods: {
    getCount(){
      this.count = {
        formTodayCount:'',
        formYtjCount:'',
        formTbzCount:'',
        studyFinishCount:'',
        studyWeekCount:'',
      }
      this.$modal.loading()
      home_count().then(response => {
        this.count = response.data
      })
      .finally(() => {
        this.$modal.closeLoading()
      })
    },
    getNoticeList(){
      this.loading = true;
      home_noticeList(this.searchForm).then(response => {
        this.noticeList = response.rows;
        this.noticeTotal = response.total;
      }).finally(()=>{
        this.loading = false;
        this.refreshNoticeCount()
      })
    },
    gotoPage(item){
      this.read(item)

      let index  = _.findIndex(this.$store.state.tagsView.visitedViews,(tab)=>{
        return tab.path === item.url
      })
      if(index>-1){
        this.$tab.refreshPage(this.$store.state.tagsView.visitedViews[index]);
      }else{
        this.$tab.openPage(" ",item.url)
      }
    },
    refreshNoticeCount(){
      this.$store.dispatch('GetMenuCount').then(() => {
      }).catch(error => {
        console.log('GetMenuCount出错：' + (error.msg || ''))
      })
    },
    read(item){
      home_noticeRead({id:item.id}).then(response => {
      }).finally(()=>{
        this.refreshNoticeCount()
      })
    },
  }
}
</script>

<style scoped lang="scss">
.home{
  .home-top{
    margin-bottom: 20px;
    .count-box{
      display: flex;
      flex-direction: row;
      align-items: center;
      .count-item{
        flex-shrink: 0;
        width: 20%;
        display: flex;
        flex-direction: row;
        align-items: center;
        .count-left{
          flex-shrink: 0;
          img{
            width: 60px;
          }
        }
        .count-right{
          flex-grow: 1;
          margin-left: 20px;
          .count-count{
            font-size: 20px;
            font-weight: bold;
          }
          .count-tip{
            margin-top: 10px;
          }
        }
      }
    }
  }
  .home-content{
    min-height: 400px;
    .notice-list{
      .notice-item{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eee;
        margin-bottom: 10px;
        padding-bottom: 5px;
        font-size: 12px;
        cursor: pointer;
        .notice-title{
          flex-grow: 1;
          flex-wrap: wrap;
          .notice-dot{
            margin-right: 2px;
            margin-top: 5px;
          }
        }
        .notice-date{
          flex-shrink: 0;
        }
      }
    }
  }
}
</style>

