<template>
  <div class="chat">
    <div class="chatList">
      <div class="kuang">
        <div ref="allChat" :style="'transform: translateY(' + mT + 'px);'">
          <div :key="i.id" :id="'chat' + i.id" v-for="i in chatList">
            <div class="chat" v-text="i.content"></div>
          </div>
        </div>
      </div>
    </div>
    <van-search
      left-icon=""
      right-icon=""
      v-model="chat"
      show-action
      :clearable="false"
      placeholder="说点什么"
    >
      <template #action>
        <img
          class="saveBtn"
          @click="onSearch"
          :src="require('../../../assets/img/save.png')"
        />
      </template>
    </van-search>
  </div>
</template>
<script setup>
import { ref, nextTick } from "vue";
import { getChat, setChat } from "@/api/worldCup.js";
import { requestAnimationFrame } from "@/utils/tool.js";
import { Toast } from "vant";

// import { onMounted, reactive } from "vue";
// import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
/**
 * window.$originData.orginParames.title 页面标题
 * window.$originData.orginParames.parameters 固定参数值
 * window.$originData.urlParames url参数
 */
const chatList = ref([]);
const allChat = ref(null);
const chat = ref("");
const mT = ref(0);
let time = undefined;
let time1 = undefined;
let page = 1;
const AnimationFrame = requestAnimationFrame();
time = setTimeout(() => {
  clearTimeout(time);
  getChat({
    page,
    pagesize: 10,
  }).then(formmater);
  time = setInterval(() => {
    if (page == -1) return clearTimeout(time);
    getChat({
      page,
      pagesize: 10,
    }).then(formmater);
  }, 5000);
}, 200);

function formmater(li) {
  const list = li ? li.records || [] : [];
  chatList.value.push(...list);
  page++;
  if (chatList.value.length >= li.total) page = -1;
  nextTick(() => {
    AnimationFrame(scorllFun);
  });
}
function scorllFun() {
  if (time1) clearTimeout(time1);
  time1 = setTimeout(() => {
    clearTimeout(time1);
    AnimationFrame(scorllFun);
  }, 25);
  let T = mT.value;
  const chat1 = document.querySelector("#chat" + chatList.value[0].id);
  if (chat1.offsetHeight <= T * -1) {
    T = 0;
    let last = chatList.value.shift();
    chatList.value.push(last);
  } else T -= 1;
  mT.value = T;
}

function onSearch() {
  if (!chat.value) return;
  setChat({
    phone: "18439106376",
    content: chat.value,
  }).then(r => {
    console.log(r);
    chat.value = "";
    Toast("发送成功")
  });
}
</script>
<style lang="scss">
.chat {
  .van-search {
    background-color: transparent;
  }
  .chatList {
    background-image: url("../../../assets/img/dmbg.jpg");
    background-size: 100% 100%;
    background-position: 0% 0%;
    height: 250px;
    padding: 10px 0 10px 0.5em;
    width: 100%;
    .kuang {
      height: 100%;
      overflow: hidden;
      .chat {
        background: #fff;
        border-radius: 2em;
        max-width: 80%;
        font-size: 14px;
        padding: 0.5em;
        display: inline-block;
        min-width: 3em;
      }
    }
  }
  .van-search__action {
    position: relative;
    width: 4em;
    :active {
      background-color: transparent;
    }
    .saveBtn {
      width: 62px;
      height: 34px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
  .myswiper {
    height: 250px;
  }
}
</style>
