<template>
  <div class="cNumber-box" :style="'height:' + fullHeight + 'px'">
    <el-header>
      <el-page-header title @back="goBack" content="作物数量"></el-page-header>
      <img src="../../assets/imageOne/24.png" alt class="cNumber-img" @click="save()" />
    </el-header>

    <!-- 主内容 -->
    <el-main :style="'height:' + fullHeight + 'px'">
      <el-input
        type="number"
        :rows="10"
        placeholder="只能输入数字"
        v-model.trim="text"
        v-enter-number
      ></el-input>
    </el-main>
  </div>
</template>

<script>
export default {
  name: "cNumber",
  data() {
    return {
      text: "",
      fullHeight: document.documentElement.clientHeight
    };
  },
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function() {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },
  },
  mounted() {
    this.get_boderHeight();
  },

  created() {},
  computed: {
    numB() {
      return this.$store.state.zuowuNumber;
    }
  },
  methods: {
    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    goBack() {
      let _this = this;
      if (localStorage.getItem("danxuan") !== "植物病害") {
        _this.$router.push("/insect");
      } else {
        _this.$router.push("/secondstep");
      }
      const timer = setInterval(() => {
        location.reload();
      }, 1000);
    },

    //点击图标保存到本地
    save() {
      let _this = this;
      if (_this.text !== "") {
        localStorage.setItem("textNumber", _this.text);
        this.$message({
          type: "success",
          message: "保存成功!"
        });
        _this.goBack()
      } else {
        this.$message({
          type: "warning",
          message: "作物数量不能为空！"
        });
      }
      if (_this.text !== "" && localStorage.getItem("area") === "面积大小") {
        localStorage.setItem("textNumber", _this.text); //存面积的数值
        localStorage.removeItem("textBNumber");
      } else {
        localStorage.setItem("textBNumber", _this.text); //存作物数量的数值
        localStorage.removeItem("textNumber");
      }
    },
  }
};
</script>

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