<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>
        <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>
      </div>
      <div v-if="pageLoading == true">
      <div>
        <van-dropdown-menu active-color="#1989fa" :closeOnClickOutside=false>
          <van-dropdown-item title="融资轮次" ref="round">
            <div style="margin: 10px 10px 0 10px;">
              <van-tag type="primary" v-for="(item, index) in round" :key="index" @click="clickLabels(item, index)" :color="roundName == item.condition?'#E9F1FD':'#F2F3F5'" :text-color="roundName == 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="confirmCancel">
                重置
              </van-button>
              <van-button class="br-6" style="width: 45%;float: right;" type="info" @click="confirmSure">
                确认
              </van-button>
            </div>
          </van-dropdown-item>
          <van-dropdown-item title="融资日期" ref="date">
            <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 manageFinancingList" :key="index">
        <div class="fw-bold mg-lt-16">{{ item.round }}</div>
        <van-cell-group>
          <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">融资日期：{{ item.date }}</div>
          <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">融资金额：{{ item.amount }}</div>
          <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">投资方：{{ item.investment }}</div>
          <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">产品名称：{{ item.productName }}</div>
          <div class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">新闻来源：{{ item.newsTitle }}</div>
          <div style="padding-bottom: 10px" class="fs-14 mg-lt-16 mg-bt-5 mg-tp-8">估值：{{ item.valuation }}</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, DropdownMenu, DropdownItem, Tag, Area, Calendar, Search} from 'vant';
  import { areaList } from '@vant/area-data';
  
  // 3. 引入接口
  import {postManageFinancing, postFinancingRound} from "@/service/api/customerDashboard";

  
  export default {
    name: "FinancingInfo",

    data() {
      return {
        pageLoading: true,
        areaList,
        manageFinancingList: [],
        total: '',
        round: [],
        roundName: '',
        roundValue: '',
        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);
      this.postGory();
    },
    mounted() {
      window.addEventListener('scroll', this.scrollBottom);
      this.postFinancing();
    },
    destroyed() {
      window.removeEventListener('scroll', this.scrollBottom)
    },
    methods: {
      // 获取融资信息
      async postFinancing(){
        let params = {
          // companyId: '9401e794ddefc55bf3ca1f02384fdba6',
          companyId: sessionStorage.getItem('companyId'),
          round: this.roundValue,
          startTime: this.startTime,
          endTime: this.endTime,
          pageNum: this.currentPage,
          pageSize: 10,
        }
        let result = await postManageFinancing(params);
        // this.manageFinancingList = result.records;
        if (result) {
          this.pageLoading = true;
          this.total = result.total;
          if (this.loading) {
            this.manageFinancingList = this.manageFinancingList.concat(result.records);
          } else {
            this.manageFinancingList = result.records;
          }
          if (this.manageFinancingList.length < result.total) {
            this.isEndLoading = false;
          } else {
            this.isEndLoading = true;
          }
          this.loading = false;
        }
      },
      // 获取融资轮次
      async postGory(){
        let result = await postFinancingRound({companyId: sessionStorage.getItem('companyId')});
        this.round = result;
      },
      // 根据融资轮次查询
      clickLabels(tag, i) {
        this.roundName = tag.condition;
      },
      // 融资轮次点击重置
      confirmCancel() {
        this.roundName = '';
        this.roundValue = '';
        this.currentPage = 1;
        this.pageLoading = false;
        this.postFinancing();
      },
      // 融资轮次点击确定
      confirmSure() {
        this.$refs.round.toggle(false);
        this.roundValue = this.roundName;
        this.currentPage = 1;
        this.pageLoading = false;
        this.postFinancing();
      },

      startDateSearch() {
        this.isShowDatePicker = true;
        this.startDateShow = true;
      },

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

      returnClick() {
        this.$router.go(-1)
      },
      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.endDateShow = false;
        this.isShowDatePicker = 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.postFinancing();
        this.$refs.date.toggle(false);
      },
      confirmDateSure() {
        console.log(this.endDateValue, this.startDateValue)
        this.currentPage = 1;
        this.pageLoading = false;
        this.postFinancing();
        this.$refs.date.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.postFinancing();
        }
      },
    },
  }
  </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-cell__value {
    bottom: 5px;
  }
  /deep/ .van-field__control[type=search] {
    padding-top: 5px;
  }
  /deep/ .van-dropdown-menu__bar--opened {
    z-index: 10 !important;
  }
  </style>../../service/api/index