<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="changeType">
            <div style="margin: 10px 10px 0 10px;">
              <van-tag type="primary" v-for="(item, index) in changeType" :key="index" @click="clickLabels(item, index)"
                :color="changeTypeName == item.condition ? '#E9F1FD' : '#F2F3F5'"
                :text-color="changeTypeName == 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-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" v-for="(item, index) in changeList" :key="index">
        <div style="font-size: 13px;margin: 10px;border-bottom: 1px solid #F2F3F4;padding-bottom: 10px;">{{
          item.changeDate }}</div>
        <div class="">
          <div class="wt-95-per hg-14-rem mg-bt-30"><span
              style="font-size: 13px;padding: 4px;border-radius: 5px;color: #8C8C8C;background-color: #DADCE0;font-weight: bold;margin-right: 5px;">0{{
                index + 1 }}</span><span class="fs-16 fw-bold">{{ item.projectName }}</span></div>
        </div>
        <div class="firm-info">
          <van-grid :column-num="2">
            <van-grid-item style="width: 50%;flex-basis: 50% !important;word-break: break-all;">
              <div class="van-item-style">变更前:</div>
              <div v-html="item.beforestr" class="fs-14 cl-595959 fw-bold" :class="[item.isShowAll ? '' : 'slText']">
              </div>
            </van-grid-item>
            <van-grid-item style="width: 50%;flex-basis: 50% !important;word-break: break-all;">
              <div class="van-item-style">变更后:</div>
              <div v-html="item.afterstr" class="fs-14 cl-595959 fw-bold" :class="[item.isShowAll ? '' : 'slText']"></div>
            </van-grid-item>
            <div class="djzk_box" v-if="item.beforeContent.length > 18 || item.afterContent.length > 18">
              <div class="djzk" @click="handleZk(item)" v-if="!item.isShowAll">...展开对比</div>
              <div class="djzk" @click="handleSq(item)" v-if="item.isShowAll">...收起对比</div>
            </div>

          </van-grid>
        </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, Picker } from 'vant';

// 3. 引入接口
import {
  postQueryChangeInfo,
  postQueryChangeType,
  queryTypeItem,
  postQueryChangeYear,
} from "@/service/api/customerDashboard";


export default {
  name: "HistoryChange",
  data() {
    return {
      selectYear: '',
      showYearPicker: false,
      yearList: [],
      pageLoading: true,
      changeList: [],
      text: '',
      changeType: [],
      changeTypeName: '',
      changeTypeValue: '',
      changeYear: [],
      changeYearName: '',
      changeYearValue: '',
      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() {
    window.scrollTo(0, 0)
    this.postChangeType();
    // this.postChangeYear();
  },
  mounted() {
    window.addEventListener('scroll', this.scrollBottom);
    this.postChangeInfo();
    this.getYearList()
  },
  destroyed() {
    window.removeEventListener('scroll', this.scrollBottom)
  },

  methods: {
    // 获取股东信息
    async postChangeInfo() {
      let params = {
        // companyId: '36bf85a19783477881cd5c4a9df3d2cb',
        companyId: sessionStorage.getItem('companyId'),
        projectName: this.changeTypeValue,
        changeDate: this.selectYear,
        pageNum: this.currentPage,
        pageSize: 10
      }
      let result = await postQueryChangeInfo(params);
      // this.changeList = result.records;
      if (result) {
        this.pageLoading = true;
        this.total = result.total;
        if (this.loading) {
          let arr = []
          arr = this.changeList.concat(result.records);
          arr.forEach(item => {
            item.afterstr = []
            item.beforestr = []
            for (let i = 0; i < item.beforeContent.length; i++) {
              for (let j = 0; j < item.afterContent.length; j++) {
                if (i == j) {
                  if (item.beforeContent[i] !== item.afterContent[j]) {
                    item.afterstr.push(`<span style="color:red">${item.afterContent[j]}</span>`)
                    item.beforestr.push(`<span style="color:rgb(22,140,237)">${item.beforeContent[i]}</span>`)
                  } else {
                    item.afterstr.push(item.afterContent[j])
                    item.beforestr.push(item.beforeContent[i])
                  }
                }

              }
            }
          })
          this.changeList = arr.map(item => {
            return {
              afterContent: item.afterContent,
              beforeContent: item.beforeContent,
              changeDate: item.changeDate,
              projectName: item.projectName,
              afterstr: item.afterstr.join(''),
              beforestr: item.beforestr.join(''),
              isShowAll: false
            }

          })
        } else {
          let arr = []
          arr = result.records;
          arr.forEach(item => {
            item.afterstr = []
            item.beforestr = []
            for (let i = 0; i < item.beforeContent.length; i++) {
              for (let j = 0; j < item.afterContent.length; j++) {
                if (i == j) {
                  if (item.beforeContent[i] !== item.afterContent[j]) {
                    item.afterstr.push(`<span style="color:red">${item.afterContent[j]}</span>`)
                    item.beforestr.push(`<span style="color:rgb(22,140,237)">${item.beforeContent[i]}</span>`)
                  } else {
                    item.afterstr.push(item.afterContent[j])
                    item.beforestr.push(item.beforeContent[i])
                  }
                }

              }
            }
          })
          this.changeList = arr.map(item => {
            return {
              afterContent: item.afterContent,
              beforeContent: item.beforeContent,
              changeDate: item.changeDate,
              projectName: item.projectName,
              afterstr: item.afterstr.join(''),
              beforestr: item.beforestr.join(''),
              isShowAll: false
            }

          })
          console.log(this.changeList, 'this.changeList')
        }
        if (this.changeList.length < result.total) {
          this.isEndLoading = false;
        } else {
          this.isEndLoading = true;
        }
        this.loading = false;
      }
    },
    // 获取变更类型
    async postChangeType() {
      let result = await queryTypeItem({ keyword: '历史变更类型' });
      this.changeType = result;
    },
    // 获取变更年份
    // async postChangeYear() {
    //   let result = await postQueryChangeYear({ companyId: sessionStorage.getItem('companyId') });
    //   this.changeYear = 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
    },
    // 根据变更类型查询
    clickLabels(tag, i) {
      this.changeTypeName = tag.condition;
    },
    // 变更类型点击重置
    typeConfirmCancel() {
      this.changeTypeName = '';
      this.changeTypeValue = '';
      this.currentPage = 1;
      this.pageLoading = false;
      this.postChangeInfo();
    },
    handleZk(item) {
      item.isShowAll = true
    },
    handleSq(item) {
      item.isShowAll = false
    },
    // 变更类型点击确定
    typeConfirmSure() {
      this.$refs.changeType.toggle(false);
      this.changeTypeValue = this.changeTypeName;
      this.currentPage = 1;
      this.pageLoading = false;
      this.postChangeInfo();
    },
    // 根据年份查询
    clickYearLabels(tag, i) {
      this.changeYearName = tag.condition;
    },
    // 年份查询点击重置
    yearConfirmCancel() {
      this.selectYear = ''
      this.changeYearName = '';
      this.changeYearValue = '';
      this.currentPage = 1;
      this.pageLoading = false;
      this.postChangeInfo();
    },
    // 年份查询点击确定
    yearConfirmSure() {
      this.$refs.year.toggle(false);
      this.changeYearValue = this.changeYearName;
      this.currentPage = 1;
      this.pageLoading = false;
      this.postChangeInfo();
    },
    returnClick() {
      this.$router.go(-1)
    },

    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.postChangeInfo();
      }
    },

  },
}
</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: 5px 10px 0px 10px;
}

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

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

/deep/ .van-card__thumb {
  width: 60px;
}

/deep/ .van-cell__value {
  bottom: 5px;
}

.firm-info {
  width: 92%;
  margin-left: 4%;
  padding-bottom: 20px;
}

.firm-info /deep/ .van-grid-item__content--center {
  background-color: #F9F9FA;
}

.firm-info /deep/ .van-grid-item__content {
  padding: 16px 16px;
}

/deep/ .van-dropdown-menu__bar--opened {
  z-index: 0;
}

.van-item-style {
  font-size: 14px;
  font-weight: bold;
  color: #595959;
  margin-bottom: 5px;
}

.slText {
  height: 38px;
  overflow: hidden;
}

.zkText {
  height: 80px;
}

.djzk_box {
  width: 100%;
}

.djzk {
  color: rgb(22, 140, 237);
  width: 100%;
  text-align: center;
}</style>../../service/api/index