import {onBeforeUnmount, onMounted, ref} from "vue";
// import {editLoginState, updateLoginLasttime} from "@/service/login";
import {editLoginState} from "@/service/show-platform";
import {updateLoginLasttime} from "@/service/interfaceKoolkit";
import router from '@/router/index'
import {showMessage} from "@/unit/element-ui/tip";

import {useRootStore} from "@/stores/useStore";


export function UseChangeOperationTime(){
 let time = 900
 const store = useRootStore()
 const timer = ref(null)
 const timeout = ref(null)
 const outTimer = ref(null)
 const myTimer = ref( time )
 function fn () {
  clearInterval(timer.value)
  timer.value = setTimeout(() => {
   updateLoginLasttime().then(res => {

   })
   fn();
  }, 3000);
 }
 fn();
 function exitFun (){
  alert('超时退出了')
  showMessage({
   tpe:'warning',
   message:'超时未操作,已退出'
  })

  router.push('/login')
  editLoginState(store.userInfo.ptjk_usercode,store.userInfo.ptjk_userkeycode,'0').then(res => {


   store.userInfo = {}
   localStorage.removeItem('dataWatchLoginUserInfo')
  })

 }
 function moveEvent(){

  clearTimeout(timeout.value)

  timeout.value = setTimeout(exitFun,time * 1000)
  clearInterval(outTimer.value)
  myTimer.value =  time


  outTimer.value = setInterval(() => {

   myTimer.value = myTimer.value - 1
  },1000)
 }
 clearTimeout(timeout.value)
 timeout.value = setTimeout(exitFun,time * 1000)
 clearInterval(outTimer.value)

 outTimer.value = setInterval(() => {
  myTimer.value = myTimer.value - 1
 },1000)
 onMounted(() => {
  document.body.removeEventListener('mousemove',moveEvent)
  document.body.addEventListener('mousemove',moveEvent)
 })

 onBeforeUnmount(() => {

  clearInterval(timer.value)
  clearTimeout(timeout.value)
  clearInterval(outTimer.value)
  document.body.removeEventListener('mousemove',moveEvent)
 })
 return {
  myTimer
 }
}
