<template>
  <div class="library_box" :style="'height:' + fullHeight + 'px'">
    <!-- 页头 -->
    <!-- <div class="library_box_headr">
      <el-page-header title @back="goBack" content="知识库"></el-page-header>
      <img src="../../assets/imageOne/sou.png" @click="sousuo()" alt class="library_box_headr_sou" />
    </div> -->
     <headStyle>
      <img src="../../assets/imageOne/left.png" @click="goBack" class="leftImage" alt="">
       <div class="headerTitle">知识库</div>
      <img src="../../assets/imageOne/sou.png" @click="sousuo()" alt class="library_box_headr_sou" />
    </headStyle>

    <!-- 主内容 -->
    <div class="library_box_main">
      <!-- tab切换 -->
      <ul class="library_box_main_tab">
        <li
          class="main_tab_plant"
          v-for="(item,index) in items"
          :key="index"
          :class="{active:istrue==index}"
          @click="istrue=index, qingqiu(index)"
        >{{ item.name }}</li>
      </ul>

      <!-- 列表 -->
      <mescroll-vue ref="mescroll" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
        <ul class="library_box_main_ul" v-if="axiosShow">
          <li
            class="library_box_main_li"
            @click="listAdd($event, index, item.id, item.name)"
            v-for="(item, index) in this.redata"
            :key="index"
          >
            <div class="library_box_main_li_box">
              <!-- 图片 -->
               <img
                v-lazy="item.img_urls === null || item.img_urls === '' ? './static/picture.jpg' : item.img_urls"
                alt
                class="library_box_main_li_img"
              />

              <!-- 病虫害名称 -->
              <div class="library_box_main_li_name">
                <div class="library_box_main_li_name_text">{{ item.name | ellipsis }}</div>
                <div class="library_box_main_li_name_xiqng">{{ item.prevention | ellipsis }}</div>
              </div>

              <!-- 详情 -->
              <div class="library_box_main_li_btn">
                <span class="library_box_main_li_btn_text">详情</span>
                <img src="../../assets/imageOne/right.png" alt class="library_box_main_li_btn_del" />
              </div>
            </div>
          </li>
        </ul>

        <!-- 暂空判断 -->
        <div class="library_judge" v-else>
          <img src="../../assets/imageOne/wu.png" alt class="library_judge_img" />
          <div class="library_judge_text">抱歉，暂无此数据</div>
        </div>
      </mescroll-vue>

      <!-- 搜索弹框-->
      <el-dialog title="搜索" :visible.sync="dialogVisible" width="80%">
        <el-input
          @blur="animateWidthA()"
          @focus="animateWidth()"
          placeholder="请输入搜索内容"
          v-model="input"
          clearable
        ></el-input>
        <span slot="footer" class="dialog-footer">
          <div class="system-box_div">
            <!-- 测试 -->
            <el-button @click="dialogVisible = false" class="kunchongBtn">取 消</el-button>
          </div>

          <div class="system-box_div">
            <!-- 测试 -->
            <el-button @click="dialogVisible = false, search()" class="binghaiBtn">确 定</el-button>
          </div>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";
import headStyle from "../headStyle/headStyle"; //头部插件
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    MescrollVue, // 注册mescroll组件
    headStyle
  },
  //限时数据文本的字数限时
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 5) {
        return value.slice(0, 5) + "...";
      }
      return value;
    }
  },
  inject: ["reload"], //注入reload方法
  data() {
    return {
      msg: "hello world",
      fullHeight: document.documentElement.clientHeight,
      istrue: localStorage.getItem("quebie"),
      items: [{ name: "虫害" }, { name: "病害" }],
      redata: [], //数据
      dialogVisible: false, //搜索弹框
      input: "", //搜索值
      axiosShow: true, //暂空判断
      sousuoData: "", //搜索数据标识
      iptShow: true, //搜索框呈现效果
      //上拉加载下拉刷新
      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) {
    console.log(to, from)
    // 如果没有配置顶部按钮或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();
  },
  computed: {},
  //监控data中的数据变化
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function() {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },

    //监听搜索弹框的状态
    dialogVisible(val) {
      if (val === false) {
        this.input = "";
      }
    }
  },
  mounted() {
    let _this = this;
    _this.get_boderHeight();

    //判断如果biaoshi为空的话就存否则不存
    if (localStorage.getItem("quebie") === null) {
      localStorage.setItem("quebie", "0");
    }

    if (localStorage.getItem("quebie") === "0") {
      //判断是否是搜索数据
      if (localStorage.getItem("sousuoData") !== null) {
        let postData = _this.$qs.parse({
          name: localStorage.getItem("libraryIpt"),
          ret: "insect",
          page: localStorage.getItem("libraryPage")
        });
        _this
          .$axios({
            method: "get",
            url: "bigservers/insect_base?page=1"
          })
          .then(res => {
            if (res.data === 0) {
              _this.axiosShow = false;
              _this.FenShow = false;
            } else if (res.data !== 0) {
              _this.redata = res.data.dat;
              _this.FenShow = true;

              let a = res.data.page;
              let b = "5";
              let d = a / b;
              if (d % 1 === 0) {
                _this.page = d * 10;
              } else {
                let g = Math.ceil(d) * 10;
                _this.page = g;
              }
            }
          })
          .catch(error => {
            console.log(error);
          });
      } else if (localStorage.getItem("sousuoData") === null) {
        _this
          .$axios({
            method: "get",
            url: "bigservers/insect_base?page=1"
          })
          .then(res => {
            if (res.data === 0) {
              _this.axiosShow = false;
              _this.FenShow = false;
            } else if (res.data !== 0) {
              _this.redata = res.data.dat;
              _this.FenShow = true;

              let a = res.data.page;
              let b = "5";
              let d = a / b;
              if (d % 1 === 0) {
                _this.page = d * 10;
              } else {
                let g = Math.ceil(d) * 10;
                _this.page = g;
              }
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    } else if (localStorage.getItem("quebie") === "1") {
      _this
        .$axios({
          method: "get",
          url: "bigservers/plant_base?page=1"
        })
        .then(res => {
          if (res.data === 0) {
            _this.FenShow = false;
            _this.axiosShow = false;
          } else if (res.data !== 0) {
            _this.FenShow = true;

            _this.redata = res.data.dat;
            let a = res.data.page;
            let b = "5";
            let d = a / b;
            if (d % 1 === 0) {
              _this.page = d * 10;
            } else {
              let g = Math.ceil(d) * 10;
              _this.page = g;
            }
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  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("quebie") === "0") {
        //判断是否是搜索数据
        if (localStorage.getItem("sousuoData") !== null) {
          if (localStorage.getItem("quebie") === "0") {
            var aaa = "insect";
          } else if (localStorage.getItem("quebie") === "1") {
            var aaa = "plant";
          }
          localStorage.setItem("libraryPage", this.mescrollUp.page.num); // 存当前页数
          let postData = _this.$qs.parse({
            name: localStorage.getItem("libraryIpt"),
            ret: aaa,
            page: this.mescrollUp.page.num
          });
          _this
            .$axios({
              method: "post",
              url: "bigservers/search",
              data: postData,
              headers: {
                "Content-Type": "application/json"
              }
            })
            .then(res => {
              //判断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) {
                let postData = _this.$qs.parse({
                  name: localStorage.getItem("libraryIpt"),
                  ret: aaa,
                  page: this.mescrollUp.page.num
                });
                this.$axios({
                  method: "post",
                  url: "bigservers/search",
                  data: postData,
                  headers: {
                    "Content-Type": "application/json"
                  }
                })
                  .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.num === 0) {
                      _this.axiosShow = false;
                      _this.FenShow = false;
                    } else if (res.data.num !== 0) {
                      _this.axiosShow = true;
                      _this.FenShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(this.aggregate);
                });
              }

              if (this.aggregate === 0) {
                this.axiosShow = false;
                this.FenShow = false;
              }
              localStorage.setItem("sousuoData", res.data.dat[0].req); //将搜索数据标识存在data中
            })
            .catch(error => {
              mescroll.endErr();
            });
        } else if (localStorage.getItem("sousuoData") === null) {
          //虫害
          this.$axios({
            method: "get",
            url: "bigservers/insect_base",
            params: {
              page: this.mescrollUp.page.num
            }
          })
            .then(res => {
              //判断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) {
                this.$axios({
                  method: "get",
                  url: "bigservers/insect_base",
                  params: {
                    page: this.mescrollUp.page.num
                  }
                })
                  .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.num === 0) {
                      _this.axiosShow = false;
                      _this.FenShow = false;
                    } else if (res.data.num !== 0) {
                      _this.axiosShow = true;
                      _this.FenShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(this.aggregate);
                });
              }

              if (this.aggregate === 0) {
                this.axiosShow = false;
                this.FenShow = false;
              }

              // let arr = res.data.dat;
              // // console.log(arr);
              // // 如果是第一页需手动置空列表
              // if (page.num === 1) this.redata = [];
              // // 把请求到的数据添加到列表
              // this.redata = this.redata.concat(arr);
              // // 数据渲染成功后,隐藏下拉刷新的状态
              // this.$nextTick(() => {
              //   mescroll.endSuccess(arr.length);
              // });
            })
            .catch(error => {
              mescroll.endErr();
            });
        }
      } else if (localStorage.getItem("quebie") === "1") {
        //判断是否在搜索情况下
        if (localStorage.getItem("sousuoData") !== null) {
          if (localStorage.getItem("quebie") === "0") {
            var aaa = "insect";
          } else if (localStorage.getItem("quebie") === "1") {
            var aaa = "plant";
          }
          localStorage.setItem("libraryPage", this.mescrollUp.page.num); // 存当前页数
          let postData = _this.$qs.parse({
            name: localStorage.getItem("libraryIpt"),
            ret: aaa,
            page: this.mescrollUp.page.num
          });
          _this
            .$axios({
              method: "post",
              url: "bigservers/search",
              data: postData,
              headers: {
                "Content-Type": "application/json"
              }
            })
            .then(res => {
              //判断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) {
                let postData = _this.$qs.parse({
                  name: localStorage.getItem("libraryIpt"),
                  ret: aaa,
                  page: this.mescrollUp.page.num
                });
                this.$axios({
                  method: "post",
                  url: "bigservers/search",
                  data: postData,
                  headers: {
                    "Content-Type": "application/json"
                  }
                })
                  .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.num === 0) {
                      _this.axiosShow = false;
                      _this.FenShow = false;
                    } else if (res.data.num !== 0) {
                      _this.axiosShow = true;
                      _this.FenShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(this.aggregate);
                });
              }

              if (this.aggregate === 0) {
                this.axiosShow = false;
                this.FenShow = false;
              }

              // let arr = res.data.dat;
              // // 如果是第一页需手动置空列表
              // if (page.num === 1) this.redata = [];
              // // 把请求到的数据添加到列表
              // this.redata = this.redata.concat(arr);
              // // 数据渲染成功后,隐藏下拉刷新的状态
              // this.$nextTick(() => {
              //   mescroll.endSuccess(arr.length);
              // });
              localStorage.setItem("sousuoData", res.data.dat[0].req); //将搜索数据标识存在data中
            })
            .catch(error => {
              mescroll.endErr();
            });
        } else if (localStorage.getItem("sousuoData") === null) {
          //病害
          this.$axios({
            method: "get",
            url: "bigservers/plant_base",
            params: {
              page: this.mescrollUp.page.num
            }
          })
            .then(res => {
              //判断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) {
                this.$axios({
                  method: "get",
                  url: "bigservers/plant_base",
                  params: {
                    page: this.mescrollUp.page.num
                  }
                })
                  .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.num === 0) {
                      _this.axiosShow = false;
                      _this.FenShow = false;
                    } else if (res.data.num !== 0) {
                      _this.axiosShow = true;
                      _this.FenShow = true;
                    }
                  })
                  .catch(error => {
                    mescroll.endErr();
                  });
              } else if (this.mescrollUp.page.num > this.aggregate) {
                // 数据渲染成功后,隐藏下拉刷新的状态
                this.$nextTick(() => {
                  mescroll.endSuccess(this.aggregate);
                });
              }

              if (this.aggregate === 0) {
                this.axiosShow = false;
                this.FenShow = false;
              }

              // let arr = res.data.dat;
              // // 如果是第一页需手动置空列表
              // if (page.num === 1) this.redata = [];
              // // 把请求到的数据添加到列表
              // this.redata = this.redata.concat(arr);
              // // 数据渲染成功后,隐藏下拉刷新的状态
              // this.$nextTick(() => {
              //   mescroll.endSuccess(arr.length);
              // });
            })
            .catch(error => {
              mescroll.endErr();
            });
        }
      }
    },
    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    //点击返回系统设置
    goBack() {
      let _this = this;
      localStorage.removeItem("quebie");
      localStorage.removeItem("libraryID");
      localStorage.removeItem("libraryPage");
      localStorage.removeItem("sousuoData");
      localStorage.removeItem("libraryIpt");
      // _this.$router.push("/discern");
      _this.$router.replace("/discern");
    },

    //tab切换请求相应的数据
    qingqiu(index) {
      let _this = this;
      //判断当前点击的是虫害还是病害
      localStorage.setItem("quebie", index);
      // location.reload();
      // _this.reload();
      if (index === 0) {
        localStorage.removeItem("sousuoData");
        _this
          .$axios({
            method: "get",
            url: "bigservers/insect_base?page=1"
          })
          .then(res => {
            // this.reload();
            if (res.data.num === 0) {
              _this.axiosShow = false;
            } else if (res.data !== 0) {
              _this.axiosShow = true;
              _this.redata = res.data.dat;
              _this.FenShow = false;
              let a = res.data.page;
              let b = "5";
              let d = a / b;
              if (d % 1 === 0) {
              } else {
                let g = Math.ceil(d) * 10;
                // console.log(g);
                _this.page = g;
              }
              _this.FenShow = true;
            }
          })
          .catch(error => {
            console.log(error);
          });
        localStorage.removeItem("libraryPage"); //切换删除存在本地的当前页数
      } else if (index === 1) {
        localStorage.removeItem("sousuoData");
        _this
          .$axios({
            method: "get",
            url: "bigservers/plant_base?page=1"
          })
          .then(res => {
            if (res.data.num === 0) {
              _this.axiosShow = false;
              _this.FenShow = false;
            } else if (res.data.num !== 0) {
              _this.axiosShow = true;
              _this.redata = res.data.dat;
              let a = res.data.page;
              let b = "5";
              let d = a / b;
              if (d % 1 === 0) {
              } else {
                let g = Math.ceil(d) * 10;
                _this.page = g;
              }
              _this.FenShow = true;
            }
          })
          .catch(error => {
            console.log(error);
          });
        localStorage.removeItem("libraryPage"); //切换删除存在本地的当前页数
      }
    },

    //获取到当前li的id
    listAdd(e, index, id, name) {
      let _this = this;
      if (
        e.target.className === "library_box_main_li_btn_text" ||
        e.target.className === "library_box_main_li_btn_del" ||
        e.target.className === "library_box_main_li_btn" ||
        e.target.className === "library_box_main_li_img" ||
        e.target.className === "library_box_main_li_name" ||
        e.target.className === "library_box_main_li_name_text" ||
        e.target.className === "library_box_main_li_box"
      ) {
        _this.current = index;
        //获取点击对象
        let el = e.currentTarget;
        let listId = id;
        localStorage.setItem("libraryID", listId);
        _this.$router.push("/inventory");
      }
    },

    //点击图标弹出搜索框
    sousuo() {
      this.dialogVisible = true;
    },

    //搜索确定事件
    search() {
      let _this = this;
      //判断是虫害还是病害
      localStorage.setItem("libraryIpt", _this.input); //存下input框中的值
      if (localStorage.getItem("quebie") === "0") {
        var aaa = "insect";
      } else if (localStorage.getItem("quebie") === "1") {
        var aaa = "plant";
      }
      let postData = _this.$qs.parse({
        name: _this.input,
        ret: aaa,
        page: "1"
      });
      _this
        .$axios({
          method: "post",
          url: "bigservers/search",
          data: postData,
          headers: {
            "Content-Type": "application/json"
          }
        })
        .then(res => {
          if (res.data.dat.length === 0) {
            _this.axiosShow = false;
            _this.FenShow = false;
          } else if (res.data.dat.length !== 0) {
            _this.FenShow = true;
            _this.redata = res.data.dat;
            // let a = res.data.page;
            // let b = "5";
            // let d = a / b;
            // if (d % 1 === 0) {
            //   _this.page = d * 10;
            // } else {
            //   let g = Math.ceil(d) * 10;
            //   console.log(g);
            //   _this.page = g;
            // }
            localStorage.setItem("sousuoData", res.data.dat[0].req); //将搜索数据标识存在data中
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    //监听数字input-失去焦点
    animateWidthA() {
      // console.log("111");
    },

    //监听数字input-获取焦点
    animateWidth() {
      // console.log("222");
    }
  }
};
</script>

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