

<!--  -->
<template>
  <div
    class="memory_box"
    :style="'height:' + fullHeight + 'px'"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <!-- 头部 -->
    <div class="memory_head">
      <div class="memory_head_returnA" @click="FanHui()">
        <img src="../../assets/image/left.png" alt class="memory_head_returnA_img" />
      </div>

      <!-- 标题 -->
      <div class="head_title">
        <span class="head_title_text">识别记录</span>
      </div>
    </div>

    <!-- 列表 -->
    <mescroll-vue ref="mescroll" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
      <div class="memory_list">
        <ul class="memory_ul" v-if="axiosShow">
          <li
            class="memory_li"
            @click="xiangqi($event, index, item.name, item.id)"
            v-for="(item, index) in this.reData"
            :key="index"
          >
            <!-- 图片 -->
            <div class="memory_li_div">
              <img
                v-lazy="item.img_url !== '' ? item.img_url : 'bigservers/' + item.img"
                alt
                class="memory_li_img"
              />
            </div>
            <!-- 昆虫或病害名称 -->
            <div class="memory_li_name">{{ item.name !== null ? item.name : '识别失败!' | ellipsis}}</div>

            <!-- 详情按钮 -->
            <div class="memory_li_btn_div">
              <span class="memory_li_btn_text">{{item.name === null ? '编辑' : '详情'}}</span>
              <img src="../../assets/image/right.png" alt class="memory_li_btn_img" />
            </div>

            <!-- 科目 -->
            <div class="memory_li_subject">
              <span class="memory_li_subject_text">{{ item.course | ellipsis }}</span>
            </div>

            <!-- 线 -->
            <div class="memory_li_line"></div>

            <!-- 时间和删除 -->
            <div class="memory_li_group">
              <!-- 时间 -->
              <div class="memory_li_group_time">
                <span class="group_time_date">{{ item.time }}</span>
              </div>
              <!-- 删除 -->
              <div class="memory_li_group_del" @click="del($event, index)">
                <img src="../../assets/image/del2.png" alt class="memory_li_group_del_img" />
              </div>
            </div>
          </li>
        </ul>

        <!-- 暂空判断 -->
        <div class="memory_judge" v-else>
          <img src="../../assets/image/wu.png" alt class="memory_judge_img" />
          <div class="memory_judge_text">抱歉，暂无识别记录</div>
        </div>
      </div>
    </mescroll-vue>

    <!-- 病虫害筛选按钮 -->
    <div class="memory_screen">
      <el-button class="memory_screen_btn" @click="show2 = !show2">
        <img src="../../assets/image/jia.png" alt class="memory_screen_btn_img" />
      </el-button>

      <transition name="el-zoom-in-bottom">
        <ul v-show="show2" class="transition-box">
          <!-- 全部 -->
          <li class="memory_all" :class="{activeC:shows == 1}" @click="all(),allA()">
            <div class="memory_all_text_box">
              <span class="memory_all_text">全部病虫害</span>
            </div>

            <div class="memory_all_img_box">
              <img src="../../assets/image/all.png" alt class="memory_all_img" />
            </div>
          </li>

          <!-- 病害 -->
          <li class="memory_plant" :class="{activeC:shows == 2}" @click="plant(),plantA()">
            <div class="memory_plant_text_box">
              <span class="memory_plant_text">病害</span>
            </div>

            <div class="memory_plant_img_box">
              <img src="../../assets/image/leaf.png" alt class="memory_plant_img" />
            </div>
          </li>

          <!-- 虫害 -->
          <li class="memory_insect" :class="{activeC:shows == 3}" @click="insect(),insectA()">
            <div class="memory_insect_text_box">
              <span class="memory_insect_text">虫害</span>
            </div>

            <div class="memory_insect_img_box">
              <img src="../../assets/image/insect.png" alt class="memory_insect_img" />
            </div>
          </li>
        </ul>
      </transition>
    </div>
  </div>
</template>

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";
export default {
  name: "memory",
  //限时数据文本的字数限时
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 5) {
        return value.slice(0, 5) + "...";
      }
      return value;
    }
  },
  components: {
    MescrollVue // 注册mescroll组件
  },
  data() {
    return {
      loading: true, //加载
      fullHeight: document.documentElement.clientHeight,
      memoryA: localStorage.getItem("seek_name"),
      show2: false, //筛选按钮
      reData: [], //后端传来的列表数据
      image: "", //后端传来的图片
      shibai: "识别失败！",
      axiosShow: true, //暂空判断
      shows: localStorage.getItem("shows"), //active
      //测试
      mescroll: null, // mescroll实例对象
      mescrollDown: {}, //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了)
      mescrollUp: {
        // 上拉加载的配置.
        callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
        //以下是一些常用的配置,当然不写也可以的.
        page: {
          num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
          size: 7 //每页数据条数,默认10
        },
        isBounce: true, //根据记录的滚动条高度回弹
        htmlNodata: '<p class="upwarp-nodata">-- 已经到底了! --</p>',
        noMoreSize: 7, //如果列表已无数据,可设置列表的总数量要大于5才显示无更多数据;
        // 避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
        // 这就是为什么无更多数据有时候不显示的原因
        toTop: {
          //回到顶部按钮
          // src: "../../../static/gotop.png", //图片路径,默认null,支持网络图
          src:
            "http://pic.51yuansu.com/pic3/cover/00/65/23/5896fa4bc81fc_610.jpg", //图片路径,默认null,支持网络图
          offset: 500 //列表滚动1000px才显示回到顶部按钮
        },
        empty: {
          //列表第一页无任何数据时,显示的空提示布局; 需配置warpId才显示
          warpId: "xxid", //父布局的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();
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function() {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    }
  },
  //方法集合
  methods: {
    mescrollInit(mescroll) {
      this.mescroll = mescroll; // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
    },
    // 上拉回调 page = {num:1, size:10}; num:当前页 ,默认从1开始; size:每页数据条数,默认10
    upCallback(page, mescroll) {
      let _this = this;
      let postData = this.$qs.parse({
        page: this.mescrollUp.page.num,
        ret: localStorage.getItem("memoryBiaoshi"),
        imei: localStorage.getItem("uuID")
      });
      this.$axios({
        method: "post",
        url: "bigservers/check_record",
        data: postData
      })
        .then(res => {
          //判断data中的变量是否为空，为空的话存后端发来的总条数
          if (_this.aggregate === "") {
            //存变量
            _this.aggregate = res.data.num;
            var b = "7";
            _this.aggregate = _this.aggregate / b;
            _this.aggregate = Math.ceil(_this.aggregate); //将小数转换为整数
          }

          //删除了默认的每页条数后判断数是否大于页码
          if (this.mescrollUp.page.num <= _this.aggregate) {
            let postData = this.$qs.parse({
              page: this.mescrollUp.page.num,
              ret: localStorage.getItem("memoryBiaoshi"),
              imei: localStorage.getItem("uuID")
            });
            this.$axios({
              method: "post",
              url: "bigservers/check_record",
              data: postData
            })
              .then(res => {
                let arr = res.data.dat;
                // 如果是第一页需手动置空列表
                if (page.num === 1) this.reData = [];
                // 把请求到的数据添加到列表
                this.reData = this.reData.concat(arr);
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(arr.length);
                });
                if (res.data.dat.length === 0) {
                  _this.axiosShow = false;
                  _this.FenShow = false;
                } else if (res.data.dat.length !== 0) {
                  _this.axiosShow = true;
                  _this.FenShow = true;
                }
                this.loading = false
              })
              .catch(error => {
                mescroll.endErr();
                this.loading = false
              });
          } else if (this.mescrollUp.page.num > _this.aggregate) {
                this.loading = false
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(_this.aggregate);
            });

            if (_this.aggregate === 0) {
              _this.axiosShow = false;
              _this.FenShow = false;
            }
          }
        })
        .catch(error => {
          mescroll.endErr();
        });
    },

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

    // 返回home页面
    FanHui() {
      let _this = this;
      localStorage.removeItem("memoryID");
      localStorage.removeItem("memoryPage");
      localStorage.removeItem("memoryDistinction");
      localStorage.removeItem("memoryBiaoshi");
      localStorage.removeItem("shows");
      localStorage.removeItem("biaoshi");
      _this.$router.push("/home");
    },

    //点击进入详情页面,并获取到当前列表的id
    xiangqi(e, index, name, id) {
      let _this = this;
      if (
        e.target.className === "memory_li_btn_text" ||
        e.target.className === "memory_li_div" ||
        e.target.className === "memory_li" ||
        e.target.className === "memory_li_name" ||
        e.target.className === "memory_li_img" ||
        e.target.className === "memory_li_group_time" ||
        e.target.className === "memory_li_subject"
      ) {
        _this.current = index;
        //获取点击对象
        let el = e.currentTarget;
        let listId = _this.reData[index].id;
        let listDistinction = _this.reData[index].ret;
        // 判断当前点击的数据是否识别失败的
        localStorage.setItem("memoryID", listId); //保存获取的ID
        localStorage.setItem("memoryDistinction", listDistinction); // 保存获取的区别

        // 判断是识别失败还是成功
        if (name == null) {
          localStorage.setItem("memoryCompileID", listId);
          // _this.$router.replace("/addData");
          _this.$router.push("/addData");
        } else if (name !== null || name !== "") {
          _this.$router.push("/record");
          // _this.$router.replace("/record");
        }
      }
    },

    // 点击全部筛选
    all() {
      location.reload();
      let _this = this;
      localStorage.removeItem("memoryPage");
      localStorage.setItem("memoryBiaoshi", "edit"); //添加一个标识，好判断当前要请求什么数据
      let postData = _this.$qs.parse({
        ret: "edit",
        page: this.mescrollUp.page.num,
        imei: localStorage.getItem("uuID")
      });
      _this
        .$axios({
          method: "post",
          url: "bigservers/check_record",
          data: postData,
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(res => {
          if (res.data.dat.length === 0) {
            _this.axiosShow = false;
            _this.FenShow = false;
          } else if (res.data.dat.length !== 0) {
            this.currentPage = localStorage.getItem("collectPage");
            _this.reData = res.data.dat; //全部数据
            _this.axiosShow = true;
            _this.FenShow = true;
          }
          _this.location = false
        })
        .catch(error => {
          alert("失败");
        });
    },

    // 点击病害筛选
    plant() {
      location.reload();
      localStorage.removeItem("memoryPage");
      let _this = this;
      localStorage.setItem("memoryBiaoshi", "plant"); //添加一个标识，好判断当前要请求什么数据
      let postData = _this.$qs.parse({
        ret: localStorage.getItem("memoryBiaoshi"),
        imei: localStorage.getItem("uuID"),
        page: this.mescrollUp.page.num
      });
      _this
        .$axios({
          method: "post",
          url: "bigservers/check_record",
          data: postData,
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(res => {
          if (res.data.dat.length === 0) {
            _this.axiosShow = false;
            _this.FenShow = false;
          } else if (res.data.dat.length !== 0) {
            _this.reData = res.data.dat; //全部数据
            _this.axiosShow = true;
            _this.FenShow = true;
          }
          _this.location = false
        })
        .catch(error => {
          alert("失败");
        });
    },

    // 点击虫害筛选
    insect() {
      location.reload();

      localStorage.removeItem("memoryPage");
      let _this = this;
      localStorage.setItem("memoryBiaoshi", "insect"); //添加一个标识，好判断当前要请求什么数据
      let postData = _this.$qs.parse({
        ret: localStorage.getItem("memoryBiaoshi"),
        imei: localStorage.getItem("uuID"),
        page: this.mescrollUp.page.num
      });
      _this
        .$axios({
          method: "post",
          url: "bigservers/check_record",
          data: postData,
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(res => {
          if (res.data.dat.length === 0) {
            _this.axiosShow = false;
            _this.FenShow = false;
          } else if (res.data.dat.length !== 0) {
            _this.reData = res.data.dat; //全部数据
            _this.axiosShow = true;
            _this.FenShow = true;
          }
          _this.loading = false
        })
        .catch(error => {
          alert("失败");
        });
    },

    //点击进行删除
    del(e, index) {
      let _this = this;
      if (e.target.className === "memory_li_group_del_img") {
        _this.current = index;
        //获取点击对象
        let el = e.currentTarget;
        let listId = _this.reData[index].id;
        let listDistinction = _this.reData[index].ret;
        localStorage.setItem("memoryID", listId); //保存获取的ID
        localStorage.setItem("memoryDistinction", listDistinction); // 保存获取的区别
        // _this.$router.push('/record');
      }
      let postData = _this.$qs.parse({
        id: localStorage.getItem("memoryID"),
        ret: localStorage.getItem("memoryDistinction")
      });
      console.log(localStorage.getItem("memoryID"))
      _this
        .$axios({
          method: "post",
          url: "bigservers/del_record",
          data: postData,
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(res => {
          if (res.data === 0) {
            this.$message({
              message: "删除成功",
              type: "success",
              duration: 1000
            });
            const timer = setInterval(() => {
              location.reload();
            }, 1000);
          } else if (res.data === 1) {
            this.$message({
              message: "删除失败",
              type: "error",
              duration: 1000
            });
          }
        })
        .catch(error => {
          this.$message({
            message: "删除失败",
            type: "error",
            duration: 1000
          });
        });
    },

    //测试
    allA() {
      localStorage.setItem("shows", 1);
    },

    plantA() {
      localStorage.setItem("shows", 2);
    },

    insectA() {
      localStorage.setItem("shows", 3);
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.get_boderHeight();
    if (localStorage.getItem("memoryBiaoshi") === null) {
      localStorage.setItem("memoryBiaoshi", "edit"); //添加一个标识，好判断当前要请求什么数据
    }
  },
};
</script>
<style lang='scss'>
@import "../../assets/scss/bus.scss";
@import "../../assets/scss/memory.scss";
.memory_box .mescroll {
  position: fixed;
  top: 44px;
  bottom: 0;
  height: auto;
}
</style>