<template>
  <div class="insectset-box" :style="'height:' + fullHeight + 'px'">
    <!-- nav -->
    <div class="dbd">
      <!-- 返回 -->
      <el-page-header title @back="goBack" content="虫害统计模板"></el-page-header>

      <!-- 背景渐变 -->
      <div class="bgc"></div>
    </div>

    <div class="main-content" :style="'height:' + fullHeight + 'px'">
      <div class="main-contentA" :style="'height:' + fullHeight + 'px'">
        <mescroll-vue ref="mescroll" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
          <!-- 暂无数据 -->
          <div class="content-block" v-if="isShow">
            <img src="../../assets/imageOne/423.png" alt class="content-block-img" />
            <div class="content-block-text">暂无数据</div>
          </div>
          <!-- 暂无数据 -->

          <div
            class="content-blocks"
            @click="chakan($event, item.id)"
            v-else
            v-for="(item, index) in this.redata"
            :key="index"
          >
            <div class v-if="item.defa !== '0'">
              <img src="../../../static/moren.png" alt class="moren" />
            </div>
            <div class="yuan1"></div>
            <div class="yuan2"></div>
            <!-- 多选框和调查时间 -->
            <div class="content-blocks-div">
              <div class="content-blocks-div-diaocha">
                <span class="diaocha">调查时间：</span>
                <span class="time">{{item.upl_time}}</span>
              </div>

              <!-- 删除按钮 -->
              <div class="content-blocks-btn">
                <span class="content-blocks-btn-xiangqi">详情</span>
                <img
                  src="../../assets/imageOne/right.png"
                  alt
                  class="content-blocks-div-diaocha-img"
                />
              </div>
            </div>
            <div class="content-blocks-frame"></div>

            <!-- 采集地址 -->
            <div class="content-blocks-div2">
              <span class="content-blocks-div2-location">采集地区：</span>
              <span class="content-blocks-div2-site">{{ item.city | ellipsis }}</span>
            </div>

            <!-- 采集人 -->
            <div class="content-blocks-div2">
              <span class="content-blocks-div2-gatherer">采集人：</span>
              <span class="content-blocks-div2-name">{{item.user_name}}</span>
            </div>
          </div>
        </mescroll-vue>
      </div>
    </div>
  </div>
</template>

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";
export default {
  name: "insectset",
  //限时数据文本的字数限时
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 12) {
        return value.slice(0, 12) + "...";
      }
      return value;
    }
  },
  inject: ["reload"], //注入reload方法
  data() {
    return {
      // addShow: false, //在列表为空时，进行隐藏
      checked: [], //多选框的
      shanShow: false, //删除、导出界面显示或隐藏
      form: {
        newName: "" //添加中input的双向绑定
      },
      dialogTableVisible: false,
      dialogVisibleshan: false, //删除
      formLabelWidth: "120px",
      dialogTableVisible: false,
      inputText: "",
      current: "", //点击list的下标
      redata: {}, //后端数据
      conheight: {
        height: ""
      },
      fullHeight: document.documentElement.clientHeight, //
      loading: true, //loading
      isShow: false, //判断是否有数据
      reBlob: [], //导出数据
      ID: localStorage.getItem("insectsetID"),
      longClick: 0, // 长按标志
      timeOutEvent: 0, // 计时器
      istrue: localStorage.getItem("insectsetIndex"),
      // items: [{ name: "虫害" }, { name: "病害" }],
      //时间筛选框
      value1: "",
      //地区下拉框
      optionsA: [],
      props: {
        value: "label",
        label: "value"
      },
      selectedOptions: "",
      //月份下拉框
      options: [],
      propsA: {
        value: "label",
        label: "value"
      },
      selectedOptionsA: "",
      //上拉加载下拉刷新
      //测试
      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: "../../../static/gotop.png", //图片路径,默认null,支持网络图
          src:
            "http://pic.51yuansu.com/pic3/cover/00/65/23/5896fa4bc81fc_610.jpg", //图片路径,默认null,支持网络图
          offset: 800 //列表滚动1000px才显示回到顶部按钮
        },
        empty: {
          //列表第一页无任何数据时,显示的空提示布局; 需配置warpId才显示
          warpId: "", //父布局的id (1.3.5版本支持传入dom元素)
          icon: "./static/mescroll/mescroll-empty.png", //图标,默认null,支持网络图
          tip: "暂无相关数据~" //提示
        },
        onScroll: function(mescroll, y, isUp) {
          //监听滚动条
          // console.log(y);
        },
        lazyLoad: {
          use: true // 是否开启懒加载,默认false
        }
      },
      aggregate: "" //后端传来数据的总条数
    };
  },
  beforeRouteEnter(to, from, next) {
    // 如果没有配置顶部按钮或isBounce,则beforeRouteEnter不用写
    next(vm => {
      // 滚动到原来的列表位置,恢复顶部按钮和isBounce的配置
      vm.$refs.mescroll && vm.$refs.mescroll.beforeRouteEnter();
    });
  },
  beforeRouteLeave(to, from, next) {
    // 如果没有配置顶部按钮或isBounce,则beforeRouteLeave不用写
    // 记录列表滚动的位置,隐藏顶部按钮和isBounce的配置
    this.$refs.mescroll && this.$refs.mescroll.beforeRouteLeave();
    next();
  },
  components: {
    MescrollVue // 注册mescroll组件
  },
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function() {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },
    checked(val) {
      console.log(val);
    }
  },
  mounted() {
    let _this = this;
    this.get_boderHeight();
    //判断如果biaoshi为空的话就存否则不存
    if (localStorage.getItem("insectsetIndex") === null) {
      localStorage.setItem("insectsetIndex", "0");
    }
  },

  // 请求后端数据
  created() {
    let _this = this;
    window.addEventListener("resize", _this.getHeight);
    _this.getHeight();

    //请求筛选地址中的数据
    _this
      .$axios({
        method: "get",
        url: "bigservers/cate"
      })
      .then(res => {
        _this.optionsA = res.data.city; //地区筛选
        _this.options = res.data.month; //月份筛选
        // localStorage.setItem("insectsetRetDizhi", res.data[0].ret); //存地址ret数据
      })
      .catch(err => {
        console.log(err);
      });
  },
  methods: {
    mescrollInit(mescroll) {
      this.mescroll = mescroll; // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
    },

    // 上拉回调 page = {num:1, size:10}; num:当前页 ,默认从1开始; size:每页数据条数,默认10
    upCallback(page, mescroll) {
      let _this = this;
      if (localStorage.getItem("insectsetIndex") === "0") {
        _this
          .$axios({
            method: "get",
            url: "bigservers/seeuser",
            params: {
              page: this.mescrollUp.page.num,
              pest_sort: "2"
            }
          })
          .then(res => {
            //判断当前是否有设置为默认的数据
            var array = res.data.dat;
            for (var i = 0; i < array.length; i++) {
              if (array[i].defa == "1") {
                localStorage.setItem("defaultType", "2");
                localStorage.setItem("defaultID", array[i].id);
                localStorage.setItem("defaultHarmID", array[i].harm_id);
              }
            }
            //判断当前是否有数据
            if (res.data.dat.length === 0) {
              this.isShow = true;
              // this.addShow = false;
              mescroll.endErr();
            } else if (res.data.dat.length !== 0) {
              // this.addShow = true;
              //判断data中的变量是否为空 ，为空的话存后端发来的总条数
              if (this.aggregate === "") {
                //存变量
                this.aggregate = res.data.num;
                var b = 6;
                this.aggregate = this.aggregate / b;
                this.aggregate = Math.ceil(this.aggregate); //将小数点转换为整数
              }
              //删除了默认的每页条数后判断数是否大于页码
              if (this.mescrollUp.page.num <= this.aggregate) {
                if (this.aggregate <= "1") {
                  var hasNext = false;
                } else if (this.aggregate > "1") {
                  var hasNext = true;
                }
                this.$axios({
                  method: "get",
                  url: "bigservers/seeuser",
                  params: {
                    page: this.mescrollUp.page.num,
                    pest_sort: "2"
                  }
                })
                  .then(res => {
                    let arr = res.data.dat;
                    // 如果是第一页需手动置空列表
                    if (page.num === 1) this.redata = [];
                    // 把请求到的数据添加到列表
                    this.redata = this.redata.concat(arr);
                    for (var i = 0; i < this.redata.length; i++) {
                      if (this.redata[i].defa == "1") {
                        localStorage.setItem("defaultID", this.redata[i].id);
                      }
                    }
                    // 数据渲染成功后,隐藏下拉刷新的状态
                    this.$nextTick(() => {
                      mescroll.endSuccess(arr.length, hasNext);
                    });

                    if (arr.length === 0) {
                      this.isShow = true;
                      // this.addShow = false;
                    } else if (arr.length !== 0) {
                      this.isShow = false;
                      // this.addShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // // 数据渲染成功后,隐藏下拉刷新的状态
                // this.$nextTick(() => {
                //   mescroll.endSuccess(this.aggregate);
                // });
              }
            }

            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(this.aggregate, hasNext);
            });
          })
          .catch(error => {
            mescroll.endErr();
            console.log(error);
          });
      } else if (localStorage.getItem("insectsetIndex") === "1") {
        _this
          .$axios({
            method: "get",
            url: "bigservers/seeuser",
            params: {
              page: this.mescrollUp.page.num,
              pest_sort: "1"
            }
          })
          .then(res => {
            //判断当前是否有设置为默认的数据
            var array = res.data.dat;
            for (var i = 0; i < array.length; i++) {
              if (array[i].defa == "1") {
                localStorage.setItem("defaultType", "1");
                localStorage.setItem("defaultID", array[i].id);
                localStorage.setItem("defaultHarmID", array[i].harm_id);
              }
            }
            //判断当前是否有数据
            if (res.data.dat.length === 0) {
              this.isShow = true;
              // this.addShow = false;
              mescroll.endErr();
            } else if (res.data.dat.length !== 0) {
              // this.addShow = true;
              //判断data中的变量是否为空 ，为空的话存后端发来的总条数
              if (this.aggregate === "") {
                //存变量
                this.aggregate = res.data.num;
                var b = 6;
                this.aggregate = this.aggregate / b;
                this.aggregate = Math.ceil(this.aggregate); //将小数点转换为整数
              }
              //删除了默认的每页条数后判断数是否大于页码
              // console.log(this.mescrollUp.page.num)
              if (this.mescrollUp.page.num <= this.aggregate) {
                if (this.aggregate <= "1") {
                  var hasNext = false;
                } else if (this.aggregate > "1") {
                  var hasNext = true;
                }
                this.$axios({
                  method: "get",
                  url: "bigservers/seeuser",
                  params: {
                    page: this.mescrollUp.page.num,
                    pest_sort: "1"
                  }
                })
                  .then(res => {
                    let arr = res.data.dat;
                    // 如果是第一页需手动置空列表
                    if (page.num === 1) this.redata = [];
                    // 把请求到的数据添加到列表
                    this.redata = this.redata.concat(arr);
                    for (var i = 0; i < this.redata.length; i++) {
                      if (this.redata[i].defa == "1") {
                        localStorage.setItem("defaultID", this.redata[i].id);
                      }
                    }
                    // 数据渲染成功后,隐藏下拉刷新的状态
                    this.$nextTick(() => {
                      mescroll.endSuccess(arr.length, hasNext);
                    });
                    if (arr.length === 0) {
                      this.isShow = true;
                      // this.addShow = false;
                    } else if (arr.length !== 0) {
                      this.isShow = false;
                      // this.addShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(this.aggregate, hasNext);
                });
              }
            }
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(this.aggregate, hasNext);
            });
          })
          .catch(error => {
            mescroll.endErr();
            console.log(error);
          });
      }
    },

    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    getHeight() {
      this.conheight.height = window.innerHeight - 170 + "px";
    },

    goBack() {
      let _this = this;
      localStorage.removeItem("insectsetID");
      localStorage.removeItem("insectsetIndex");
      localStorage.removeItem("insectsetPage");
      localStorage.removeItem("insectsetRetDizhi");
      // _this.$router.push("/system");
      _this.$router.replace("/system");
    },

    //点击查看触发事件
    chakan(e, id) {
      let _this = this;
      _this.longClick = 0; // 初始化
      if (
        e.target.className === "content-blocks-div2" ||
        e.target.className === "content-blocks-btn" ||
        e.target.className === "content-blocks-btn-xiangqi" ||
        e.target.className === "content-blocks-div-diaocha-img"
      ) {
        let el = e.currentTarget;
        let dataID = id;
        localStorage.setItem("insectsetID", dataID);
        _this.$router.push("/plantinsectset");
      }
    }
  }
};
</script>

<style lang="scss">
@import "../../assets/style/bus.scss";
@import "../../assets/style/scss/insectset.scss";
</style>
