<!--
 * @Author: 高瑞廷 2419056691@qq.com
 * @Date: 2025-09-28 14:31:53
 * @LastEditors: 高瑞廷 2419056691@qq.com
 * @LastEditTime: 2026-01-27 14:36:37
 * @FilePath: \zjkj-nodejs-npm_customer-client\src\lib\layout\main.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!--
 * @Author: 高瑞廷 2419056691@qq.com
 * @Date: 2025-09-28 14:31:53
 * @LastEditors: 高瑞廷 2419056691@qq.com
 * @LastEditTime: 2025-10-10 15:22:24
 * @FilePath: \zjkj-nodejs-npm_customer-client\src\lib\layout\main.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!--
 * @Desc: 新版窗口
 * @Auth: Hrc
 * @Date: 2021-06-03 15:26:57
-->
<template>
  <div class="zjtodo__root  zjtodo-scrollbar zjtodo-common-e-ui">
    <div
      class="zjtodo__wrapper"
      :style="{ 'z-index': zIndex }" 
    >
      <transition name="zjtodo-slide-in">
        <chat-main  :title="title" @handle="toolbarClick" @UnreadFun="UnreadFun"></chat-main>
      </transition>
    </div>
  </div>
</template>
<script>
import ChatMain from '@lib/components/chatMain/index.vue'
// 初始化连接
// import { openIM, closeIM } from '@lib/socket/socket.js'
// 工具
// import { getOperationSys, getBrowser } from '@lib/utils/index.js'
// import { insertAmap } from '@lib/utils/amap.js'
import { loadSpider } from '@lib/utils/spiderHack.js'
import { getToken } from '@lib/utils/cookie.js'
import pkg from '../../../package.json'
import * as kfStore from '@lib/store/utils.js'
import * as AiStore from '@lib/store/utils'
export default {
  name: 'ZJKJQgbnpmTodo',
  components: {
    ChatMain,
  },
  provide() {
    return {
      superCtrl: this.superControl,
    }
  },
  props: {
    imAccount: {
      type: [String, Object],
      default:"202007091611352" //wkk
    },
    isShow: {
      default: false,
      type: Boolean
    },
    chatImToken: {
      type: [String, Function]
    },
    platformType:{
      type:String,
    },
    selectActive:{
      type:String
    }
  },
  data() {
    return {
      imUser: {}, // 传入的用户参数
      env: '', // 传入的当前环境
      title: 'Ai助理', //传入默认标题
      zIndex: 2000,
      userInfo: {
        launchPage: '',
        terminalSource: '',
        ipAddress: ''
      },
      visiable: true, // 显示隐藏
      showChat: false, // 显示聊天窗口
      addClass: false, // 添加class
      isFullScreen: false, // 是否全屏
      onClose: null, // 关闭回调
      errorType: 0, // 错误参数
      superControl: {
        priviewUrl: '', // 预览图片url
        privieShow: false, // 预览组件显示异常
        showEmoji: false // 显示表情版面
      },
      version: pkg.version
    }
  },
  computed: {
    valideParams() {
      return { env: this.env, im: this.imUser }
    }
  },
  watch: {
    // imAccount: {
    //   handler: function (val) {
    //     debugger
    //     if (val) {
    //         AiStore.commit('SET_ACCOUNT', val);
    //     }
    //   },
    //   immediate: true
    // },
    visiable(newVal) {
      if (newVal && !this.showChat) {
        this.setParams()
      }
    },
    showChat(newVal) {
      if (newVal) {
        setTimeout(() => {
          this.addClass = true
        }, 100)
      } else {
        this.addClass = false
      }
    }
  },
  methods: {
    UnreadFun(val){
      this.$emit('syncUnreadFun',val)
    },
    // 验证参数
    validate() {
      if (!this.imUser.accountId) {
        this.errorType = 1
        return false
      } else if (!this.env) {
        this.errorType = 3
        return false
      }
      return true
    },
    loadScript() {
      // insertAmap()  // 加载amap
      loadSpider()  // 加载spider
    },
    // 设置用户参数
    setUserParams() {
      // this.userInfo.launchPage = window.location.href
      // this.userInfo.terminalSource = getOperationSys() + '-' + getBrowser()
      this.userInfo.token = getToken()
      kfStore.commit('SET_CURR_ENV', this.env)  // 存储传入环境变量
      this.loadScript()
      // openIM(this.userInfo) // 建立会话
    },
    registComp() {
      this.$nextTick(() => {
      })
    },
    setParams() {
      if (!this.validate()) return
      this.showChat = true
      this.setUserParams()
      window.addEventListener('beforeunload', e => {
        this.close()
      })
    },
    // 点击工具栏按钮
    toolbarClick(data) {
      switch (data.type) {
        case 'mini':
          this.close(true)
          break
        case 'zoom':
          this.isFullScreen = data.isFull
          break
        case 'close':
          this.close(false)
          break
      }
    },
    // 关闭窗口
    close(isMinimized) {
      if (isMinimized) {
        this.onClose(isMinimized)
      } else {
        this.showChat = false
        // closeIM(true)
        setTimeout(() => {
          this.doDestroy()
        }, 200)
      }
    },
    // 销毁
    doDestroy() {
      if (typeof this.onClose === 'function') {
        this.onClose()
      }
      this.$destroy(true)
      this.$el.parentNode.removeChild(this.$el)
    }
  }
}
</script>
<style lang="scss" scoped>
.zjtodo__root {
  height: 100%;
  overflow: hidden;
  background: transparent;
  z-index: 2001;
}
.zjtodo-slide-in-enter-active {
  transition: all 0.3s ease;
}
.zjtodo-slide-in-leave-active {
  transition: all 0.3s ease-out;
}
.zjtodo-slide-in-enter,
.zjtodo-slide-in-leave-to {
  transform: translateY(10%);
  opacity: 0;
}
</style>
