<!--  -->
<template>
  <div class="details_box" :style="'height:' + fullHeight + 'px'">
    <!-- 页头 -->
    <div class="details_box_headr">
      <el-page-header title @back="goBack" content="识别记录详情"></el-page-header>
    </div>

    <!-- 主内容 -->
    <div class="details_box_main">
      <!-- 图片 -->
      <div class="details_box_main_tupian">
        <!-- <img
          preview="1"
          v-lazy="this.redata.img_urls === null || this.redata.img_urls === '' ? 'bigservers/' + this.redata.img :  this.redata.img_urls"
          alt
          class="details_box_main_tupian_img"
        /> -->
         <img
          preview="1"
          v-lazy="this.redata.img_urls"
          alt
          class="details_box_main_tupian_img"
        />

        <!-- 病虫害名字 -->
        <div class="details_box_main_tupian_name" v-if="dataShow">
          <div class="details_box_main_tupian_nameA">{{ this.redata.name }}</div>
        </div>
      </div>

      <!-- 详情 -->
      <div class="details_box_main_particulars" v-if="dataShow">
        <!-- 地址和拍摄时间 -->
        <div class="locationTime_box">
          <div
            class="locationTime_location"
            v-if="localShow"
          >地址: {{ this.redata.addr == 'undefined' || this.redata.addr == 'null' ? '定位失败' : this.redata.addr }}</div>
          <div class="locationTime_tiem">时间: {{ this.redata.time }}</div>
        </div>

        <!-- 小百科 -->
        <div class="details_box_main_cyclopedia">
          <img src="../../assets/imageOne/38.png" alt class="details_box_main_cyclopedia_img" />
          <span class="details_box_main_cyclopedia_title">小百科</span>
        </div>

        <!-- 详情text -->
        <div class="details_box_main_text" v-for="item in this.msg" :key="item">{{ item }}</div>
      </div>

      <!-- 暂空判断 -->
      <div v-else class="details_box_main_text_box">
        <img src="../../assets/imageOne/cry.png" alt class="cry_img" />
        <div class="cry_text">暂无此类病虫害的资料;努力更新中...</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      fullHeight: document.documentElement.clientHeight,
      redata: [], //后端传来的数据
      msg: "",
      dataShow: true, //判断是否没有数据
      localShow: true //判断当前是否定位成功
    };
  },
  //监听属性 类似于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: {
    //点击返回系统设置
    goBack() {
      let _this = this;
      localStorage.removeItem("recordID");
      // _this.$router.push("/record");
      _this.$router.replace("/record");
    },

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

    //切割详情字符串数据并渲染
    apply() {
      let _this = this;
      let Adata = _this.redata.prevention;
      var a = Adata.replace(/]/g, "：");
      let Bdata = new Array();
      Bdata = a.split("[");
      _this.msg = Bdata;
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.get_boderHeight();
    let postData = this.$qs.parse({
      id: localStorage.getItem("recordID")
    });
    if (localStorage.getItem("biaoshi") === "0") {
      this.$axios({
        method: "post",
        url: "bigservers/insect_details",
        data: postData,
        headers: {
          "Content-Type": "application/json"
        }
      })
        .then(res => {
          this.redata = res.data[0];
          if (res.data[0].prevention !== "[防治方法] null") {
            this.apply();
            this.dataShow = true;
          } else {
            this.dataShow = false;
          }

          //判断后端传来的数据中是否有定位
          if (res.data[0].addr !== "null") {
            this.localShow = true;
          } else {
            this.localShow = false;
          }
        })
        .catch(error => {
          console.log(error);
        });
    } else if (localStorage.getItem("biaoshi") === "1") {
      this.$axios({
        method: "post",
        url: "bigservers/plant_details",
        data: postData,
        headers: {
          "Content-Type": "application/json"
        }
      })
        .then(res => {
          this.redata = res.data[0];
          this.apply();
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>
<style lang='scss'>
@import "../../assets/style/bus.scss";
@import "../../assets/style/scss/details.scss";
</style>