<!--  -->
<template>
  <div
    class="help_box"
    :style="'height:' + fullHeight + 'px'"
    v-loading="loading"
    element-loading-text="拼命加载中"
  >
    <!-- 头部信息 -->
    <div class="help_box-head">
      <img
        src="../../assets/image/new/30.png"
        @click="goBack()"
        alt=""
        class="help_box-head-img"
      />
      <div class="help_box-headBox">
        <img
          preview="1"
          v-lazy="'http://192.168.1.8:8002/' + img"
          alt=""
          class="help_box-headImg"
        />
      </div>
    </div>

    <!-- 主内容 -->
    <mescroll-vue
      ref="mescroll"
      class="home_box_mescroll"
      :down="mescrollDown"
      :up="mescrollUp"
      @init="mescrollInit"
    >
      <div class="help_box-main">
        <!-- 标题 -->
        <div class="navlist">
          <ul>
            <li
              class="navli"
              v-for="(item, index) in navList"
              :class="{ activeT: nowIndex === index }"
              @click="tabClick(index)"
              :key="index"
            >
              <div class="navliVal">{{ item.name }}</div>
            </li>
          </ul>
        </div>

        <!-- 相应内容 -->
        <div class="swiper-container swiper_con" style="background: #f7f8fc">
          <div class="swiper-wrapper">
            <!-- 交流 -->
            <div class="swiper-slide" ref="viewBox">
              <ul class="help_box-main-ulA" v-if="interflowShow">
                <li
                  class="help_box-main-listA"
                  v-for="(item, index) in this.interflowDat"
                  :key="index"
                  @click="
                    interflowDetails(
                      $event,
                      item.id,
                      item.visit_num,
                      item.replys,
                      item.like,
                      item.like_num
                    )
                  "
                >
                  <!-- 用户信息 -->
                  <div class="help_box-main-listA-userBox">
                    <img
                      preview="1"
                      v-lazy="'http://192.168.1.8:8002/' + item.photo"
                      alt
                      class="help_box-main-listA-userBox-img"
                    />

                    <div class="help_box-main-listA-userBox-txt">
                      <div class="help_box-main-listA-userBox-name">
                        {{
                          item.user_remark == null
                            ? item.username
                            : item.user_remark
                        }}
                        <!-- <div class="help_box-main-listA-userBox-name-del">删除</div> -->
                        <img
                          src="../../assets/image/new/32.png"
                          @click="delData(item.id)"
                          alt=""
                          class="help_box-main-listA-userBox-name-del"
                        />
                      </div>
                      <div class="help_box-main-listA-userBox-time">
                        {{ item.time }}
                      </div>
                    </div>
                  </div>

                  <!-- 交流内容-文字 -->
                  <div class="help_box-main-listA-writing">
                    {{ item.problem }}
                  </div>

                  <!-- 交流图片 -->
                  <div class="help_box-main-listA-image">
                    <ul class="help_box-main-listA-image-ul">
                      <li
                        class="help_box-main-listA-image-list"
                        v-for="itemA in item.img"
                        :key="itemA"
                        @click="imgShow(itemA)"
                        preview="1"
                      >
                        <img
                          v-lazy="'http://192.168.1.8:8002/' + itemA"
                          alt
                          class="help_box-main-listA-imageList"
                        />
                      </li>
                    </ul>
                  </div>

                  <!-- 交流数据的查看评论点赞 -->
                  <div class="help_box-main-listA-operation">
                    <ul class="help_box-main-listA-operation-ul">
                      <li class="help_box-main-listA-operation-list">
                        <img
                          src="../../assets/image/new/8.png"
                          alt
                          class="help_box-main-listA-operation-listImg"
                        />
                        <div class="help_box-main-listA-operation-listNum">
                          {{ item.visit_num }}
                        </div>
                      </li>
                      <li class="help_box-main-listA-operation-list">
                        <img
                          src="../../assets/image/new/10.png"
                          alt
                          class="help_box-main-listA-operation-listImg"
                        />
                        <div class="help_box-main-listA-operation-listNum">
                          {{ item.reply_num }}
                        </div>
                      </li>
                      <li class="help_box-main-listA-operation-list">
                        <!-- 未点赞 -->
                        <img
                          @click.stop="like(item.id, index)"
                          v-if="item.exist === '0'"
                          src="../../assets/image/new/3.png"
                          alt
                          class="help_box-main-listA-operation-listImg"
                        />
                        <!-- 点赞了 -->
                        <img
                          v-else
                          @click.stop="noLike(item.id, index)"
                          src="../../assets/image/new/23.png"
                          alt
                          class="help_box-main-listA-operation-listImg"
                        />
                        <div class="help_box-main-listA-operation-listNum">
                          {{ item.like_num }}
                        </div>
                      </li>
                    </ul>
                  </div>
                </li>
              </ul>

              <!-- 暂空判断 -->
              <div class="collect_judge" id="empty" v-else>
                <img
                  src="../../assets/image/wu.png"
                  alt
                  class="collect_judge_img"
                />
                <div class="collect_judge_text">抱歉，暂无鉴定</div>
              </div>
            </div>

            <!-- 鉴定 -->
            <div class="swiper-slide">
              <ul class="help_box-main-authenticateUl" v-if="interflowShow">
                <li
                  class="help_box-main-authenticateList"
                  v-for="(itemA, index) in interflowDat"
                  :key="index"
                  @click="authenticateDetails($event, itemA.id)"
                >
                  <!-- 删除 -->
                  <img
                    src="../../assets/image/new/33.png"
                    @click="delData(itemA.id)"
                    alt=""
                    class="help_box-main-authenticateList-imgDel"
                  />

                  <div class="help_box-main-authenticateList-box1">
                    <img
                      preview="1"
                      v-lazy="'http://192.168.1.8:8002/' + itemA.img[0]"
                      alt
                      class="help_box-main-authenticateList-box1-img"
                    />
                    <div class="help_box-main-authenticateList-box1-txt">
                      {{ itemA.problem }}
                    </div>
                  </div>

                  <div class="help_box-main-authenticateList-box2">
                    <div class="help_box-main-authenticateList-box3">
                      <img
                        preview="1"
                        v-lazy="'http://192.168.1.8:8002/' + itemA.photo"
                        alt
                        class="help_box-main-authenticateList-box3-img"
                      />
                      <div class="help_box-main-authenticateList-box3-name">
                        {{
                          itemA.user_remark == "" || itemA.user_remark == null
                            ? itemA.username
                            : itemA.user_remark
                        }}
                      </div>
                    </div>
                    <img
                      src="../../assets/image/new/10.png"
                      alt
                      class="help_box-main-authenticateList-box4-img"
                    />
                  </div>
                </li>
              </ul>

              <!-- 暂空判断 -->
              <div class="collect_judge" id="empty" v-else>
                <img
                  src="../../assets/image/wu.png"
                  alt
                  class="collect_judge_img"
                />
                <div class="collect_judge_text">抱歉，暂无鉴定</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </mescroll-vue>
  </div>
</template>

<script>
// 引入mescroll的vue组件
import MescrollVue from "mescroll.js/mescroll.vue";
import Swiper from "swiper";
import "../../../swiper/css/swiper.min.css";

export default {
  name: "help",
  //import引入的组件需要注入到对象中才能使用
  components: {
    MescrollVue,
    Swiper,
  },
  data() {
    //这里存放数据
    return {
      fullHeight: document.documentElement.clientHeight,
      navList: [{ name: "交流" }, { name: "鉴定" }],
      nowIndex: 0,
      mySwiper: "",
      // 交流
      interflowDat: [], //交流列表数据
      isClick: true, //点赞开关
      amplificationShow: false, //查看大图
      imgList: "",
      img: "", //用户头像
      interflowShow: 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:
            "http://pic.51yuansu.com/pic3/cover/00/65/23/5896fa4bc81fc_610.jpg", //图片路径,默认null,支持网络图
          offset: 500, //列表滚动1000px才显示回到顶部按钮
        },
        onScroll: function (mescroll, y, isUp) {
          //吸顶
          // if (y > 500) {
          //   self.navBarFixed = true;
          // } else {
          //   self.navBarFixed = false;
          // }
        },
        lazyLoad: {
          use: true, // 是否开启懒加载,默认false
        },
      },
      aggregate: "", //后端传来数据的总条数
      loading: true,
      // 上拉加载下拉刷新
    };
  },
  beforeRouteEnter(to, from, next) {
    if (from.name === "field_details") {
      to.meta.isBack = true;
    }
    // 如果没有配置顶部按钮或isBounce,则beforeRouteEnter不用写
    next((vm) => {
      if (from.name === "field_details") {
        // 滚动到原来的列表位置,恢复顶部按钮和isBounce的配置
        vm.$refs.mescroll && vm.$refs.mescroll.beforeRouteEnter();
      }
    });
  },
  beforeRouteLeave(to, from, next) {
    if (to.name !== "field_details") {
      to.meta.isBack = false;
    } else {
      to.meta.isBack = true;
      // 如果没有配置顶部按钮或isBounce,则beforeRouteLeave不用写
      // 记录列表滚动的位置,隐藏顶部按钮和isBounce的配置
      this.$refs.mescroll && this.$refs.mescroll.beforeRouteLeave();
    }

    next();
  },
  activated() {
    if (!this.$route.meta.isBack) {
      // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
      this.mescroll.resetUpScroll(); // 刷新列表数据
    }
    // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
    this.$route.meta.isBack = false;
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    fullHeight(val) {
      //监控浏览器高度变化
      if (!this.timer) {
        this.fullHeight = val;
        this.timer = true;
        let that = this;
        setTimeout(function () {
          //防止过度调用监测事件，导致卡顿
          that.timer = false;
        }, 400);
      }
    },

    //交流鉴定切换
    nowIndex(val) {
      if (val == 0) {
        this.interflowData();
      } else if (val == 1) {
        this.interflowData();
      }
    },
  },
  //方法集合
  methods: {
    //动态获取浏览器高度
    get_boderHeight() {
      const that = this;
      window.onresize = () => {
        return (() => {
          window.fullHeight = document.documentElement.clientHeight;
          that.fullHeight = window.fullHeight;
        })();
      };
    },

    // mescroll组件初始化的回调,可获取到mescroll对象
    mescrollInit(mescroll) {
      this.mescroll = mescroll; // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
    },

    upCallback(page, mescroll) {
      var that = this;
      // diverse 0 鉴定 1、交流
      if (that.nowIndex == 0) {
        var diverse = "1";
      } else if (that.nowIndex == 1) {
        var diverse = "0";
      }
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/user_answer",
          params: {
            page: that.mescrollUp.page.num,
            ret: "user",
            diverse: diverse,
          },
        })
        .then((res) => {
          var nums = res.data.num;
          if (this.aggregate === "") {
            //存变量
            this.aggregate = nums;
            var b = "6";
            this.aggregate = this.aggregate / b;
            this.aggregate = Math.ceil(this.aggregate); //将小数点转换为整数
            console.log(this.aggregate);
          }
          if (res.data.num !== 0) {
            var data = res.data.dat;
            for (var i = 0; i < data.length; i++) {
              if (data[i].username !== "admin") {
                // 1字符串转化成数组
                let phoneArr = [...data[i].username];
                var n = "";
                // 2.将数组中的4-7位变成*
                phoneArr.map((res, index) => {
                  if (index > 2 && index < 7) {
                    n += "*";
                    // return '*';
                  } else {
                    n += res;
                  }
                });
                data[i]["username"] = n;
              } else {
                data[i]["username"] = data[i].username;
              }
            }

            // 如果是第一页需手动置空列表
            if (page.num === 1) this.interflowDat = [];
            // 把请求到的数据添加到列表
            this.interflowDat = this.interflowDat.concat(data);

            // // 数据渲染成功后,隐藏下拉刷新的状态
            // this.$nextTick(() => {
            //   mescroll.endSuccess(data.length);
            // });
            that.interflowShow = true;
          } else if (res.data.num == 0) {
            this.gerenShow = false;
            var data = res.data.dat;
            that.interflowDat = data;
            that.interflowShow = false;
          }
          // 数据渲染成功后,隐藏下拉刷新的状态
          this.$nextTick(() => {
            mescroll.endSuccess(res.data.dat.length);
          });
          that.loading = false;
        })
        .catch((err) => {
          mescroll.endErr();
          console.log(err);
        });
    },

    // 返回
    goBack() {
      var that = this;
      that.$router.replace("/person");
    },

    // tab切换交流、鉴定
    initSwiper() {
      var that = this;
      that.mySwiper = new Swiper(".swiper-container", {
        initialSlide: 0,
        autoplay: false,
        keyboardControl: true,
        autoHeight: true,
        observer: true, //修改swiper自己或子元素时，自动初始化swiper
        observeParents: true,
        autoplayDisableOnInteraction: false, //解决拖动之后不能自动轮播
        preventLinksPropagation: false, // 阻止点击事件冒泡
        onSlideChangeStart: function (swiper) {
          //这个是当swiper被改变是的回调函数，可以拿到当前索引
          console.log(swiper.activeIndex);
          // alert(swiper.activeIndex);
          that.nowIndex = swiper.activeIndex;
        },
        onSlideChangeTransitionStart: function (swiper, event) {},
      });
    },
    tabClick(index) {
      this.nowIndex = index;
      if (!this.mySwiper.slideTo) {
        this.initSwiper();
      }
      this.mySwiper.slideTo(index, 300, false);
    },

    // 交流、鉴定列表数据接口
    interflowData() {
      var that = this;
      // diverse 0 鉴定 1、交流
      if (that.nowIndex == 0) {
        var diverse = "1";
      } else if (that.nowIndex == 1) {
        var diverse = "0";
      }
      that
        .$axios({
          method: "get",
          url: "http://192.168.1.8:8002/user_answer",
          params: {
            page: 1,
            ret: "user",
            diverse: diverse,
          },
        })
        .then((res) => {
          if (res.data.num !== 0) {
            var data = res.data.dat;
            that.interflowDat = [];
            for (var i = 0; i < data.length; i++) {
              if (data[i].username !== "admin") {
                // 1字符串转化成数组
                let phoneArr = [...data[i].username];
                var n = "";
                // 2.将数组中的4-7位变成*
                phoneArr.map((res, index) => {
                  if (index > 2 && index < 7) {
                    n += "*";
                    // return '*';
                  } else {
                    n += res;
                  }
                });
                data[i]["username"] = n;
              } else {
                data[i]["username"] = data[i].username;
              }
            }

            that.interflowDat = data;
            that.interflowShow = true;
          } else if (res.data.num == 0) {
            this.gerenShow = false;
            var data = res.data.dat;
            that.interflowDat = data;
            that.interflowShow = false;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 点击进入交流详情
    interflowDetails(e, id, visit_num, replys, like, like_num) {
      var that = this;
      if (e.target.className !== "help_box-main-listA-userBox-name-del") {
        this.$router.push({
          name: "field_details",
          query: {
            id: id, //id
            visit_num: visit_num, //查看
            replys: replys, //评论
            like: like, //点赞状态
            like_num: like_num, //点赞次数
            ret: "help", //判断是从我的主页进入还是在交流进入
          },
        });
      }
    },
    //  点击进入鉴定详情
    authenticateDetails(e, id) {
      var that = this;
      if (e.target.className !== "help_box-main-authenticateList-imgDel") {
        that.$router.push({
          name: "addData_details",
          query: {
            id: id, //id
            ret: "help",
            // visit_num: visit_num, //查看
            // replys: replys, //评论
            // like: like, //点赞状态
            // like_num: like_num, //点赞次数
          },
        });
      }
    },
    // 交流点赞
    like(id, index) {
      var that = this;
      var isClick = that.isClick;
      var postData = that.$qs.parse({
        id: id, //id
        ret: "add", //点赞
        req: "title", //主问题
      });
      if (isClick === true) {
        that.isClick = false;
        that
          .$axios({
            method: "post",
            url: "http://192.168.1.8:8002/farm_likes",
            data: postData,
          })
          .then((res) => {
            if (res.data === 0) {
              this.$message({
                type: "success",
                message: "点赞成功!",
                duration: 1500,
              });
              that.interflowDat[index].like = "1"; //改变点赞状态
              that.interflowDat[index].like_num =
                that.interflowDat[index].like_num + 1; //改变点赞数据
            } else if (res.data === 1) {
              this.$notify.error({
                title: "错误",
                message: "点赞出错了！！",
                duration: 1500,
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
        setTimeout(function () {
          that.isClick = true;
        }, 1500);
      }
    },
    // 交流取消点赞
    noLike(id, index) {
      var that = this;
      var isClick = that.isClick;
      var postData = that.$qs.parse({
        id: id, //id
        ret: "off", //点赞
        req: "title", //主问题
      });
      if (isClick === true) {
        that.isClick = false;
        that
          .$axios({
            method: "post",
            url: "http://192.168.1.8:8002/farm_likes",
            data: postData,
          })
          .then((res) => {
            if (res.data === 0) {
              that.$message({
                type: "success",
                message: "取消点赞成功！",
                duration: 1500,
              });
              that.interflowDat[index].like = "0"; //改变点赞状态
              that.interflowDat[index].like_num =
                that.interflowDat[index].like_num - 1; //改变点赞数量
            } else if (res.data === 1) {
              that.$notify.error({
                title: "错误",
                message: "取消点赞出错了！！",
                duration: 1500,
              });
            }
          })
          .catch((err) => {
            console.log(err);
          });
        setTimeout(function () {
          that.isClick = true;
        }, 1500);
      }
    },
    //点击轮播图片进行放大
    imgShow(img) {
      this.amplificationShow = true;
      this.imgList = img;
      console.log(img);
      console.log(111);
      console.log(this.imgList);
    },

    // 点击删除数据
    delData(id) {
      var that = this;
      this.$confirm("确定删除当前数据？")
        .then((_) => {
          that
            .$axios({
              method: "get",
              url: "http://192.168.1.8:8002/user_reply",
              params: {
                id: id,
                ret: "anwers",
              },
            })
            .then((res) => {
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                  duration: 1500,
                });
                that.interflowData();
              } else {
                this.$notify.error({
                  title: "错误",
                  message: "删除出错了！！",
                  duration: 1500,
                });
              }
            })
            .catch((err) => {
              consoel.log(err);
            });
        })
        .catch((_) => {});
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.img = this.$route.query.img;
    this.get_boderHeight();
    // this.initSwiper(); //tab切换交流、鉴定、方案
    // this.interflowData(); //交流、鉴定列表接口
  },
};
</script>
<style lang='scss'>
@import "../../assets/scss/help.scss";
//@import url(); 引入公共css类
.el-message-box {
  width: 340px;
}
</style>