<template>
  <div id="category">
    <!--头部-->
    <!-- <Header/> -->
    <div class="header-title">
      <span class="header-title-return-back" @click="returnClick"><van-icon name="arrow-left" /></span>
      <span class="header-title-name-bold">法院公告</span>
    </div>
    <div v-if="pageLoading == true">
      <div>
        <van-dropdown-menu active-color="#1989fa">
          <van-dropdown-item title="公告类型" ref="noticeType">
            <div style="margin: 10px 10px 0 10px;">
              <van-tag type="primary" v-for="(item, index) in noticeType" :key="index" @click="clickLabels(item, index)"
                :color="noticeTypeName == item.condition ? '#E9F1FD' : '#F2F3F5'"
                :text-color="noticeTypeName == item.condition ? '#2372EB' : '#6D6F6D'"
                class="mg-lt-5 pd-5 pd-bt-10 mg-10 fs-13" size="large">{{ item.condition }}</van-tag>
            </div>
            <div style="padding: 10px 16px;margin-bottom: 45px;">
              <van-button class="br-6" style="width: 45%;float: left;" type="default" @click="typeConfirmCancel">
                重置
              </van-button>
              <van-button class="br-6" style="width: 45%;float: right;" type="info" @click="typeConfirmSure">
                确认
              </van-button>
            </div>
          </van-dropdown-item>
          <van-dropdown-item title="年份" ref="year">
            <div style="margin: 10px 10px 0 10px;">
              <!-- <van-tag type="primary" v-for="(item, index) in year" :key="index" @click="clickYearLabels(item, index)" :color="yearName == item.condition?'#E9F1FD':'#F2F3F5'" :text-color="yearName == item.condition?'#2372EB':'#6D6F6D'" class="mg-lt-5 pd-5 pd-bt-10 mg-10 fs-13" size="large">{{ item.condition }}</van-tag> -->
              <van-field v-model="selectYear" is-link readonly placeholder="请选择年份" @click="showYearPicker = true" />
              <van-popup v-model:show="showYearPicker" round position="bottom">
                <van-picker show-toolbar :columns="yearList" cancel-button-text="取消" @cancel="onCloseYearPicker"
                  @confirm="onYearPicked" />
              </van-popup>
            </div>
            <div style="padding: 10px 16px;margin-bottom: 45px;">
              <van-button class="br-6" style="width: 45%;float: left;" type="default" @click="yearConfirmCancel">
                重置
              </van-button>
              <van-button class="br-6" style="width: 45%;float: right;" type="info" @click="yearConfirmSure">
                确认
              </van-button>
            </div>
          </van-dropdown-item>
        </van-dropdown-menu>
      </div>
      <div style="font-size: 13px;margin-top: 10px;margin-left: 5px;">共<span style="color: #DB2C2C;">{{ total
      }}</span>条法院公告</div>

      <div class="manage-body">
        <div v-for="(item, index) in illegalCourtList" :key="index">
          <div @click="jumpDetail(item)" class="fw-bold mg-lt-16 mg-bt-10 mg-tp-10">{{ item.companyName }}</div>
          <van-cell-group @click="jumpDetail(item)">
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">案由：</div>
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">上诉人：{{ item.prosecutorList }}</div>
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">被上诉人：{{ item.defendantList }}</div>
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">公告类型：{{ item.category }}</div>
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">当事人：{{ item.party }}</div>
            <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">刊登日期：{{ item.publishDate }}</div>
          </van-cell-group>
        </div>
      </div>
    </div>
    <van-loading style="text-align: center;margin-top: 50%" v-if="pageLoading == false" />
  </div>
</template>
  
<script>
import { Button, Col, Row, Icon, Field, Tag, DropdownMenu, DropdownItem,Picker } from 'vant';

// 3. 引入接口
import { postQueryIllegalCourt, postQueryCateGory, postQueryYear } from "@/service/api/customerDashboard";


export default {
  name: "CourtAnnouncements",

  data() {
    return {
      selectYear: '',
      showYearPicker: false,
      yearList: [],
      pageLoading: true,
      noticeType: [],
      noticeTypeName: '',
      noticeTypeValue: '',
      noticeLabels: [],
      year: [],
      yearName: '',
      yearValue: '',
      illegalCourtList: [],
      total: '',
      currentPage: 1,
      loading: false,
      isEndLoading: false,
    }
  },
  components: {
    VanButton: Button,
    VanRow: Row,
    VanCol: Col,
    VanIcon: Icon,
    VanField: Field,
    VanTag: Tag,
    VanDropdownMenu: DropdownMenu,
    VanDropdownItem: DropdownItem,
    VanPicker: Picker,
  },
  created() {
    this.postIllegalCourt();
    this.postCateGory();
    // this.postYear();
    this.getYearList()
  },
  mounted() {
    window.addEventListener('scroll', this.scrollBottom);
  },
  destroyed() {
    window.removeEventListener('scroll', this.scrollBottom)
  },
  methods: {
    // 获取法院公告
    async postIllegalCourt() {
      let params = {
        // companyId: '00334179afa2e99e4d2932c4ae1f738c',
        companyId: sessionStorage.getItem('companyId'),
        category: this.noticeTypeValue,
        courtYear: this.selectYear,
        pageNum: this.currentPage,
        pageSize: 10,
      }
      let result = await postQueryIllegalCourt(params);
      // this.illegalCourtList = result.records;
      if (result) {
        this.pageLoading = true;
        this.total = result.total;
        if (this.loading) {
          this.illegalCourtList = this.illegalCourtList.concat(result.records);
        } else {
          this.illegalCourtList = result.records;
        }
        if (this.illegalCourtList.length < result.total) {
          this.isEndLoading = false;
        } else {
          this.isEndLoading = true;
        }
        this.loading = false;
      }
    },
    // 获取公告类型
    async postCateGory() {
      let result = await postQueryCateGory({ companyId: sessionStorage.getItem('companyId') });
      this.noticeType = result;
    },
    // 获取公告年份
    // async postYear() {
    //   let result = await postQueryYear({ companyId: sessionStorage.getItem('companyId') });
    //   this.year = result;
    // },
    getYearList(){
      let currentYear = new Date().getFullYear()
      for(let i=currentYear;i>=2000;i--){
        this.yearList.push(i)
      }
    },
    onCloseYearPicker() { 
      this.showYearPicker = false
      this.selectYear = ''
    },
    onYearPicked(val) {
      this.selectYear = val 
      this.showYearPicker = false
     },
    returnClick() {
      this.$router.go(-1)
    },
    jumpDetail(data) {
      sessionStorage.setItem('courtAnnouncementsData', JSON.stringify(data))
      this.$router.push({ path: 'CourtAnnouncementsDetail' })
    },
    // 根据公告类型查询
    clickLabels(tag, i) {
      this.noticeTypeName = tag.condition;
    },
    // 公告类型点击重置
    typeConfirmCancel() {
      this.noticeTypeName = '';
      this.noticeTypeValue = '';
      this.currentPage = 1;
      this.pageLoading = false;
      this.postIllegalCourt();
    },
    // 公告类型点击确定
    typeConfirmSure() {
      this.$refs.noticeType.toggle(false);
      this.noticeTypeValue = this.noticeTypeName;
      this.currentPage = 1;
      this.pageLoading = false;
      this.postIllegalCourt();
    },
    // 根据年份查询
    clickYearLabels(tag, i) {
      this.yearName = tag.condition;
    },
    // 年份查询点击重置
    yearConfirmCancel() {
      this.selectYear = ''
      this.yearName = '';
      this.yearValue = '';
      this.currentPage = 1;
      this.pageLoading = false;
      this.postIllegalCourt();
    },
    // 年份查询点击确定
    yearConfirmSure() {
      this.$refs.year.toggle(false);
      this.yearValue = this.yearName;
      this.currentPage = 1;
      this.pageLoading = false;
      this.postIllegalCourt();
    },

    scrollBottom() {
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop  //页面滚动的高度
      let clientHeight = document.documentElement.clientHeight  //可视区域高度
      let scrollHeight = document.documentElement.scrollHeight  //可滚动内容的高度
      let bottomOfWindow = scrollTop + clientHeight + 1000 >= scrollHeight    //是否滚动到底部
      //loading 和 this.tableData.length和this.total是自己的判断条件
      if (scrollTop !== 0 && bottomOfWindow && !this.loading && this.total > 10) {
        this.loading = true;
        this.currentPage += 1;
        this.postIllegalCourt();
      }
    },

  },
}
</script>
  
<style lang="less" scoped>
.header-title {
  height: 46px;
  width: 100%;
  background-color: #fff;
  text-align: center;
}

.header-title-name {
  font-weight: bold;
  line-height: 46px;
  align-items: center;
  justify-content: center;
  display: inline-flex;
}

.header-title-return {
  position: absolute;
  top: 13px;
  left: 20px;
  font-size: 15px;
}

.manage-body {
  margin-top: 10px;
  background-color: #fff;
  padding: 10px 10px 10px 10px;
}

/deep/ .van-nav-bar__text {
  color: gray !important;
  font-weight: bold;
}

.icon-mg {
  margin-left: 3px;
}

/deep/ .van-cell__value {
  bottom: 5px;
}
/deep/ .van-dropdown-menu__bar--opened{
  z-index: 0;
}
</style>../../service/api/index