<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-popup position="bottom" :style="{ height: '50%' }" v-model="isShowDatePicker">
          <van-datetime-picker :min-date="minDate" @confirm="onConfirm" @cancel="startDateCancel"
                               v-if="startDateShow"></van-datetime-picker>
          <van-datetime-picker :min-date="minDate" @confirm="onEndConfirm" @cancel="endDateCancel"
                               v-if="endDateShow"></van-datetime-picker>
        </van-popup>
        <van-dropdown-menu active-color="#1989fa" :closeOnClickOutside=false>
            <van-dropdown-item title="发布时间" ref="year">
              <div style="margin: 10px 10px 0 10px;">
<!--                <van-search v-model="startDateValue" placeholder="请输入开始日期" @focus="startDateSearch" left-icon="notes-o"></van-search>-->
<!--                <van-search v-model="endDateValue" placeholder="请输入结束日期" @focus="endDateSearch" left-icon="notes-o"></van-search>-->
<!--                <van-calendar v-model="startDateShow" @confirm="onConfirm" color="#1989fa" :min-date="minStartDate" :max-date="maxStartDate" />-->
<!--                <van-calendar v-model="endDateShow" @confirm="onEndConfirm" color="#1989fa" :min-date="minStartDate" :max-date="maxStartDate" />-->
                <van-cell title="选择开始日期" :value="startDateValue" @click="startDateSearch" />
                <van-cell title="选择结束日期" :value="endDateValue" @click="endDateSearch" />
              </div>
              <div style="padding: 10px 16px;margin-bottom: 45px;">
                <van-button class="br-6" style="width: 45%;float: left;"  type="default" @click="confirmDateCancel">
                  重置
                </van-button>
                <van-button class="br-6" style="width: 45%;float: right;" type="info" @click="confirmDateSure">
                  确认
                </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 manageNewsList" :key="index">
        <div style="padding-top:10px" class="fw-bold mg-lt-16">{{ item.title }}</div>
        <van-cell-group>
          <div style="font-size: 14px;margin: 20px;padding: 10px;background-color: #F7F8FB;">
            <div style="margin-bottom: 10px;"><van-icon color="#2372EB" name="link-o" style="margin-right: 5px;" /><span style="word-break: break-all"><a :href="item.url">{{ item.url }}</a></span></div>
            <div><span>{{ item.companyName }}</span><van-tag v-if="item.tags" class="mg-lt-5 pd-5 fs-13" color="#FDEAEB" text-color="#DB2C2C" size="large">{{ item.tags }}</van-tag></div>
          </div>
          <van-cell :title="item.source" :value="item.publishTime" />
        </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, DropdownMenu, DropdownItem, Tag, Area, Calendar, Search} from 'vant';
  
  // 3. 引入接口
  import {postManageNews} from "@/service/api/customerDashboard";

  
  export default {
    name: "PublicOpinionInfo",

    data() {
      return {
        pageLoading: true,
        manageNewsList: [],
        total: '',
        year: [{condition: '2021'},{condition: '2022'},{condition: '2023'},{condition: '2024'}],
        startDateValue: '',
        endDateValue: '',
        startTime: '',
        endTime: '',
        date: '',
        startDateShow: false,
        endDateShow: false,
        minStartDate: new Date(1996, 0, 1),
        maxStartDate: new Date(2025, 0, 31),
        minDate: new Date(2000, 0, 1),
        isShowDatePicker: false,
        currentPage: 1,
        loading: false,
        isEndLoading: false,
      }
    },
    components: {
      VanButton: Button,
      VanRow: Row,
      VanCol: Col,
      VanIcon: Icon,
      VanField: Field,
      VanTag: Tag,
      VanDropdownMenu: DropdownMenu,
      VanDropdownItem: DropdownItem,
      VanArea: Area,
      VanCalendar: Calendar,
      VanSearch: Search,
    },
    created() {
      window.scrollTo(0, 0);
    },
    mounted() {
      window.addEventListener('scroll', this.scrollBottom);
      this.postNews();
    },
    destroyed() {
      window.removeEventListener('scroll', this.scrollBottom)
    },
    methods: {
      // 获取舆情信息
      async postNews(){
        let params = {
          // companyId: '9401e794ddefc55bf3ca1f02384fdba6',
          companyId: sessionStorage.getItem('companyId'),
          startTime: this.startTime,
          endTime: this.endTime,
          pageNum: this.currentPage,
          pageSize: 10,
        }
        let result = await postManageNews(params);
        // this.manageNewsList = result.records;
        if (result) {
          this.pageLoading = true;
          this.total = result.total;
          if (this.loading) {
            this.manageNewsList = this.manageNewsList.concat(result.records);
          } else {
            this.manageNewsList = result.records;
          }
          if (this.manageNewsList.length < result.total) {
            this.isEndLoading = false;
          } else {
            this.isEndLoading = true;
          }
          this.loading = false;
        }
      },
      startDateSearch() {
        this.isShowDatePicker = true;
        this.startDateShow = true;
      },

      endDateSearch() {
        this.isShowDatePicker = true;
        this.endDateShow = true;
      },

      formatDate(date) {
        if (date.getMonth()+1 < 10) {
          return `${date.getYear()+1900}-0${date.getMonth()+1}-${date.getDate()}`;
        } else {
          return `${date.getYear()+1900}-${date.getMonth()+1}-${date.getDate()}`;
        }
      },
      formatUpdateDate(date) {
        if (date.getMonth()+1 < 10) {
          if (date.getDate() < 10) {
            return `${date.getYear()+1900}0${date.getMonth()+1}0${date.getDate()}`;
          } else {
            return `${date.getYear()+1900}0${date.getMonth()+1}${date.getDate()}`;
          }
        } else {
          if (date.getDate() < 10) {
            return `${date.getYear()+1900}${date.getMonth()+1}0${date.getDate()}`;
          } else {
            return `${date.getYear()+1900}${date.getMonth()+1}${date.getDate()}`;
          }
        }
      },
      onConfirm(date) {
        this.startDateShow = false;
        this.isShowDatePicker = false;
        this.startDateValue = this.formatDate(date)
        this.startTime = this.formatUpdateDate(date)
      },
      onEndConfirm(date) {
        this.isShowDatePicker = false;
        this.endDateShow = false;
        this.endDateValue = this.formatDate(date)
        this.endTime = this.formatUpdateDate(date)
      },

      startDateCancel() {
        this.startDateValue = '';
        this.startDateShow = false;
        this.endDateShow = false;
        this.isShowDatePicker = false;
      },
      endDateCancel() {
        this.endDateValue = '';
        this.startDateShow = false;
        this.endDateShow = false;
        this.isShowDatePicker = false;
      },

      confirmDateCancel() {
        this.endDateValue = '';
        this.startDateValue = '';
        this.endTime = '';
        this.startTime = '';
        this.currentPage = 1;
        this.pageLoading = false;
        this.postNews();
        this.$refs.year.toggle(false);
      },
      confirmDateSure() {
        console.log(this.endDateValue, this.startDateValue)
        this.currentPage = 1;
        this.pageLoading = false;
        this.postNews();
        this.$refs.year.toggle(false);
      },

      // 滚动到页面底部时，请求内容
      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.postNews();
        }
      },

      returnClick() {
        this.$router.go(-1)
      },

    },
  }
  </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 0px 10px;
  }
  /deep/ .van-nav-bar__text {
    color: gray !important;
    font-weight: bold;
  }
  .icon-mg {
    margin-left: 3px;
  }
  /deep/ .van-dropdown-menu__bar--opened {
    z-index: 10 !important;
  }
  ///deep/ .van-cell__value {
  //  bottom: 5px;
  //}
  </style>../../service/api/index