<!--  -->
<template>
  <div
    class="conversionList_box"
    :style="'height:' + fullHeight + 'px'"
    v-loading="loading"
    element-loading-text="拼命加载中"
  >
    <!-- 头部 -->
    <div class="conversionList_box-head">
      <div class="conversionList_divBox">
        <img
          src="../../assets/image/new/30.png"
          alt
          class="conversionList_headrBox-img"
          @click="goBack()"
        />
        <div class="conversionList_headrBox-tlt">兑换列表</div>
      </div>
    </div>

    <!-- 主内容 -->
    <mescroll-vue
      ref="mescroll"
      class="home_box_mescroll"
      :down="mescrollDown"
      :up="mescrollUp"
      @init="mescrollInit"
    >
      <div class="conversionList_box-main">
        <!-- 未处理或已处理切换 -->
        <div class="conversionList_box-mainB">
          <!-- 标题 -->
          <div class="navlist">
            <ul>
              <li
                class="navli"
                v-for="(item, index) in navList"
                :class="{ activeT: nowIndex === index }"
                @click="tabClick(index)"
                :key="index"
              >
                <div class="navliVal">{{ item.name }}</div>
              </li>
            </ul>
          </div>

          <!-- 相应内容 -->
          <div class="swiper-container swiper_con" style="background: #f7f8fc">
            <div class="swiper-wrapper">
              <!-- 未处理 -->
              <div class="swiper-slide">
                <ul
                  class="conversionList_-main-authenticateUlA"
                  v-if="insectShow"
                >
                  <li
                    class="conversionList_-main-authenticateListA"
                    v-for="(item, index) in insectPlantDat"
                    :key="index"
                    @click="plantDetails(item)"
                  >
                    <!-- <li class="conversionList_-main-authenticateListA"> -->
                    <div class="conversionList_-main-authenticateListA-box1">
                      <div
                        class="conversionList_-main-authenticateListA-box1-txt"
                      >
                        用户{{ item.username }}
                      </div>
                      <div
                        class="conversionList_-main-authenticateListA-box1-time"
                      >
                        {{ item.time }}
                      </div>
                    </div>

                    <div class="conversionList_-main-authenticateListA-box2">
                      <div
                        class="conversionList_-main-authenticateListA-box2-txt"
                      >
                        {{ item.prizeid }}
                      </div>
                      <img
                        src="../../assets/image/new/43.png"
                        alt=""
                        class="conversionList_headrBox-img"
                      />
                    </div>
                  </li>
                </ul>

                <!-- 暂空判断 -->
                <div class="seek_data_judge" v-else>
                  <img
                    src="../../assets/image/wu.png"
                    alt
                    class="seek_data_judge_img"
                  />
                  <div class="seek_data_judge_text">抱歉，暂无此数据</div>
                </div>
              </div>

              <!-- 已处理 -->
              <div class="swiper-slide">
                <ul
                  class="conversionList_-main-authenticateUlA"
                  v-if="insectShow"
                >
                  <li
                    class="conversionList_-main-authenticateListA"
                    v-for="(itemA, index) in insectPlantDat"
                    :key="index"
                    @click="plantDetails(itemA)"
                  >
                    <!-- <li class="conversionList_-main-authenticateListA"> -->
                    <div class="conversionList_-main-authenticateListA-box1">
                      <div
                        class="conversionList_-main-authenticateListA-box1-txt"
                      >
                        用户{{ itemA.username }}
                      </div>
                      <div
                        class="conversionList_-main-authenticateListA-box1-time"
                      >
                        {{ itemA.time }}
                      </div>
                    </div>

                    <div class="conversionList_-main-authenticateListA-box2">
                      <div
                        class="conversionList_-main-authenticateListA-box2-txt"
                      >
                        {{ itemA.prizeid }}
                      </div>
                      <img
                        src="../../assets/image/new/43.png"
                        alt=""
                        class="conversionList_headrBox-img"
                      />
                    </div>
                  </li>
                </ul>

                <!-- 暂空判断 -->
                <div class="seek_data_judge" v-else>
                  <img
                    src="../../assets/image/wu.png"
                    alt
                    class="seek_data_judge_img"
                  />
                  <div class="seek_data_judge_text">抱歉，暂无此数据</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </mescroll-vue>
  </div>
</template>                          

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";
import Swiper from "swiper";
import "../../../swiper/css/swiper.min.css";
export default {
  name: "conversionList",
  //import引入的组件需要注入到对象中才能使用
  components: {
    MescrollVue,
    Swiper,
  },
  data() {
    return {
      fullHeight: document.documentElement.clientHeight, //自定义高度
      botNav: true, //底部导航
      navList: [{ name: "未处理" }, { name: "已处理" }],
      nowIndex: 0,
      mySwiper: "",
      insectPlantDat: [], //病虫害数据列表
      insectShow: true, //病虫害暂无判断
      integralVal: "",
      userName: "",
      ret: "", //判断是从哪个页面进入
      // 上拉加载下拉刷新
      mescroll: null, // mescroll实例对象
      mescrollDown: {}, //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了)
      mescrollUp: {
        // 上拉加载的配置.
        callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
        //以下是一些常用的配置,当然不写也可以的.
        page: {
          num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
          size: 6, //每页数据条数,默认10
        },
        isBounce: true, //根据记录的滚动条高度回弹
        htmlNodata: '<p class="upwarp-nodata">-- 已经到底了! --</p>',
        noMoreSize: 6, //如果列表已无数据,可设置列表的总数量要大于5才显示无更多数据;
        // 避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
        // 这就是为什么无更多数据有时候不显示的原因
        toTop: {
          //回到顶部按钮
          src:
            "http://pic.51yuansu.com/pic3/cover/00/65/23/5896fa4bc81fc_610.jpg", //图片路径,默认null,支持网络图
          offset: 500, //列表滚动1000px才显示回到顶部按钮
        },
        onScroll: function (mescroll, y, isUp) {
          //吸顶
          // if (y > 500) {
          //   self.navBarFixed = true;
          // } else {
          //   self.navBarFixed = false;
          // }
        },
        lazyLoad: {
          use: true, // 是否开启懒加载,默认false
        },
      },
      aggregate: "", //后端传来数据的总条数
      loading: true,
      // 上拉加载下拉刷新
    };
  },
  beforeRouteEnter(to, from, next) {
    if (from.name === "conversionListDetails") {
      to.meta.isBack = true;
    }

    // 如果没有配置顶部按钮或isBounce,则beforeRouteEnter不用写
    next((vm) => {
      if (from.name === "conversionListDetails") {
        // 滚动到原来的列表位置,恢复顶部按钮和isBounce的配置
        vm.$refs.mescroll && vm.$refs.mescroll.beforeRouteEnter();
      }
    });
  },
  beforeRouteLeave(to, from, next) {
    if (to.name !== "conversionListDetails") {
      to.meta.isBack = false;
    } else {
      to.meta.isBack = true;
      // 如果没有配置顶部按钮或isBounce,则beforeRouteLeave不用写
      // 记录列表滚动的位置,隐藏顶部按钮和isBounce的配置
      this.$refs.mescroll && this.$refs.mescroll.beforeRouteLeave();
    }
    next();
  },
  activated() {
    if (!this.$route.meta.isBack) {
      // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
      this.mescroll.resetUpScroll(); // 刷新列表数据
    }
    // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
    this.$route.meta.isBack = false;
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function () {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },

    // tab切换
    nowIndex(val) {
      this.listData();
    },
  },
  //方法集合
  methods: {
    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    // mescroll组件初始化的回调,可获取到mescroll对象
    mescrollInit(mescroll) {
      this.mescroll = mescroll; // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
    },

    upCallback(page, mescroll) {
      let that = this;
      //   0 未处理  1处理
      var prize = that.nowIndex;
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/user_prize_list",
          params: {
            page: that.mescrollUp.page.num,
            prize: prize,
          },
          headers: {
            "Content-Type": "multipart/form-data",
          },
        })
        .then((res) => {
          var nums = res.data.nums;
          if (this.aggregate === "") {
            //存变量
            this.aggregate = nums;
            var b = "10";
            this.aggregate = this.aggregate / b;
            this.aggregate = Math.ceil(this.aggregate); //将小数点转换为整数
            console.log(this.aggregate);
          }
          var data = res.data.data;
          if (res.data.nums !== 0) {
            // 如果是第一页需手动置空列表
            if (page.num === 1) this.insectPlantDat = [];
            // 把请求到的数据添加到列表
            this.insectPlantDat = this.insectPlantDat.concat(data);
            that.insectShow = true;
          } else {
            console.log("暂无");
            that.insectShow = false;
          }
          that.loading = false;
          // 数据渲染成功后,隐藏下拉刷新的状态
          this.$nextTick(() => {
            mescroll.endSuccess(data.length);
          });
        })
        .catch((err) => {
          mescroll.endErr();
          console.log(err);
        });
    },

    // 返回
    goBack() {
      var that = this;
      that.$router.replace({
        name: "share",
        query: {
          name: that.userName,
          num: that.integralVal,
          ret: that.ret,
        },
      });
    },

    // tab切换交流、鉴定、方案
    initSwiper() {
      var that = this;
      that.mySwiper = new Swiper(".swiper-container", {
        initialSlide: 0,
        autoplay: false,
        keyboardControl: true,
        autoHeight: true,
        observer: true, //修改swiper自己或子元素时，自动初始化swiper
        observeParents: true,
        autoplayDisableOnInteraction: false, //解决拖动之后不能自动轮播
        preventLinksPropagation: false, // 阻止点击事件冒泡
        onSlideChangeStart: function (swiper) {
          //这个是当swiper被改变是的回调函数，可以拿到当前索引
          console.log(swiper.activeIndex);
          that.nowIndex = swiper.activeIndex;
        },
        onSlideChangeTransitionStart: function (swiper, event) {},
      });
    },
    tabClick(index) {
      this.nowIndex = index;
      if (!this.mySwiper.slideTo) {
        this.initSwiper();
      }
      this.mySwiper.slideTo(index, 300, false);
    },

    //列表数据接口
    listData() {
      var that = this;
      //   0 未处理  1处理
      var prize = that.nowIndex;
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/user_prize_list",
          params: {
            page: 1,
            prize: prize,
          },
          headers: {
            "Content-Type": "multipart/form-data",
          },
        })
        .then((res) => {
          if (res.data.nums !== 0) {
            console.log(res.data);
            var data = res.data.data;
            that.insectPlantDat = data;
          } else {
            console.log("暂无");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 点击进入详情
    plantDetails(data) {
      var that = this;
      console.log(data);
      // console.log(that.nowIndex)
      that.$router.push({
        name: "conversionListDetails",
        query: {
          data: JSON.stringify(data),
          nowIndex: that.nowIndex,
          name: that.userName,
          num: that.integralVal,
          ret: that.ret,
        },
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.integralVal = this.$route.query.num; //个人积分
    this.userName = this.$route.query.name; //当前账号名称
    this.ret = this.$route.query.ret; //判断是从哪个界面进入
    this.initSwiper();
    this.get_boderHeight();
    // this.listData(); //兑奖订单列表数据请求
  },
};
</script>
<style lang='scss'>
.conversionList_box {
  width: 100%;
  background: #f7f8fc;
  // background: #f5f5f5;
}

// 头部
.conversionList_box-head {
  background: -webkit-linear-gradient(right, #0dc6b6, #0cd3aa, #1dd69c);
  position: fixed;
  width: 100%;
  z-index: 100;
}

.conversionList_box-mainB .navlist {
  padding: 1.1rem 0 0 0;
}

.conversionList_divBox {
  display: flex;
  justify-content: space-between;
  width: 55%;
  margin: 0 0 0 0.5rem;
}

.conversionList_headrBox-tlt {
  text-align: center;
  font-size: 18px;
  color: #fff;
  padding: 8px 0 8px 0;
}

.conversionList_headrBox-img {
  width: 0.5rem;
  height: 0.5rem;
  vertical-align: middle;
  margin-top: 0.3rem;
}

// 主内容
.conversionList_box-main {
  background: #f7f8fc;
  // background: #f5f5f5;
}

.conversionList_box-mainB,
.swiper-wrapper {
  height: 100% !important;
}

// 病虫害切换
.conversionList_box-main .navlist ul {
  border-bottom: 1px solid #e9e9e9;
  display: flex;
  justify-content: space-between;
  background: #fff;
}

.conversionList_box-main .navlist ul li {
  margin: 0 0.5rem 0 0.5rem;
}

.conversionList_box-main .navlist .activeT {
  border-bottom: 1px solid #1ec8a1;
  color: #1ec8a1;
}

.navli .navliVal {
  padding: 10px 35px 10px 35px;
  font-size: 15px;
}

// 交流页面
.conversionList_box-main-ulA {
  // border: 1px solid #000;
}

.conversionList_box-main-listA {
  background: #fff;
  padding-top: 0.25rem;
  display: flex;
  justify-content: space-between;
}

.conversionList_-main-authenticateUlA {
  width: 100%;
  margin: 0.25rem auto;
}

.conversionList_-main-authenticateListA {
  border: 1px solid #fff;
  background: #fff;
  width: 100%;
  display: flex;
  margin: 0.2rem 0 0 0;
  padding: 0.15rem 0 0.15rem 0;
}

.conversionList_-main-authenticateListA-box1 {
  width: 100%;
  // border-radius: 5px;
  // border: 1px solid #000;
}

.conversionList_-main-authenticateListA-box1-txt {
  font-size: 12px;
  color: #686565;
  text-align: left;
  padding: 0.05rem 0.15rem 0.05rem 0.15rem;
}
.conversionList_-main-authenticateListA-box1-time {
  // border: 1px solid #000;
  text-align: left;
  padding: 0.05rem 0.15rem 0.05rem 0.15rem;
  width: 40%;
}

.conversionList_-main-authenticateListA-box2 {
  // border: 1px solid #000;
  display: flex;
  width: 30%;
}
.conversionList_-main-authenticateListA-box2-txt {
  font-size: 12px;
  color: #686565;
  text-align: left;
  padding: 0.05rem 0.15rem 0.05rem 0.15rem;
  line-height: 1.1rem;
}

.conversionList_-main-authenticateListA-box1-num {
  text-align: right;
  color: #acacac;
}

// 暂无处理
.seek_data_judge {
  margin: 45% 0 0 0;
}

.seek_data_judge_img {
  margin: 0 auto;
}

.seek_data_judge_text {
  font-size: 25px;
  color: #bfbfbf;
  margin: 0.5rem 0 0 0;
  text-align: center;
}
</style>