<template>
  <div class="g_p_10 g_position_rela">
    <div class="g_bg_f g_p_10 g_radius_8">
      <u-line-progress class="g_position_abso" v-if="ailoading" style="position: fixed; width: 100%; top: -13px; left: 0px; z-index: 99999" active-color="#3578f6" :percent="defaultPercent" :round="false" height="8" :show-percent="false" inactiveColor="#f5f5f5"></u-line-progress>
      <gao-ChatSSEClient ref="sseTest" @onOpen="openCore" @onError="errorCore" @onMessage="messageCore" @onFinish="finishCore" />
      <textarea class data-type="desp" v-model="aitext" placeholder="粘贴老乡信息, 用AI识别 " placeholder-style="color:#999" :maxlength="-1" :style="{ width: '100%', height: true ? '104px' : '104px' }"></textarea>
      <div class>
        <view class="g_flex_row_end flex_center">
          <view class="iconfont icon-jia1 g_c_9 g_mr_8 g_fs_20"></view>
          <view class="g_mr_8 g_w_72 g_fw_600">
            <g-button size="small_auto" type="none" height="24" :fontSize="12" @clickBtn="getClipboard" :btnText="(aitext ? '清空' : '粘贴') + '内容'"></g-button>
          </view>
          <view @click.stop="getCoze" class="g_w_72 g_fw_600">
            <g-button size="small_auto" gradual="linear-gradient(-130deg, #5c6cf7, #9963f9)" icon="icon-aitubiao" type="gradual" height="24" :fontSize="12" btnText="AI识别" @clickBtn="getCoze"></g-button>
          </view>
        </view>
      </div>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      aitext: '',
      timer: null,
      timer1: null,
      defaultPercent: 0,
      ailoading: false,
      aiConfig: {
        token: '',
        id: '',
      },
    }
  },
  created() {
    this.getAiToken()
  },

  methods: {
    openCore(respons) {
      console.log('open sse：', respons)
    },
    errorCore(err) {
      console.log('error sse：', err)
    },
    messageCore(msg) {
      console.log('message sse：', msg)
      // if (msg.event == 'Done') {
      //   if (!that.formInfo.jobName) {
      //     clearInterval(that.timer)
      //     that.ailoading = false
      //   }
      // } else {
      //   let job = JSON.parse(msg.data)
      //   checkDataInfo(job)
      // }
    },
    getAiToken() {
      let that = this
      that.G.Get('/yishoudan/common/structure/getConfig', {}, (res) => {
        console.log('res', res)
        that.aiConfig = {
          token: res.token,
          id: res.workflowId,
        }
      })
    },
    getClipboard() {
      let that = this
      if (this.aitext) {
        this.aitext = ''
      } else {
        uni.getClipboardData({
          success(res) {
            console.log(res)
            that.aitext = res.data
          },
          fail() {
            uni.showToast({
              title: '读取剪切板失败',
              icon: 'none',
            })
          },
        })
      }
    },
    getCoze() {
      let that = this
      if (!that.aitext) {
        uni.showToast({
          title: '请粘贴内容',
          icon: 'none',
        })
        return false
      }
      // 重置内容
      that.ailoading = true
      that.defaultPercent = 3

      that.timer = setInterval(() => {
        that.defaultPercent = that.defaultPercent < 15 ? (that.defaultPercent = 15) : that.defaultPercent + 1
        // console.log('12313123123123123')
        if (that.defaultPercent >= 99) {
          clearInterval(that.timer)
        }
      }, 500)
      that.timer1 = setTimeout(() => {
        console.log('5687678678678678678')
        that.ailoading = false
        clearInterval(that.timer)
        // setLoading()
      }, 30000)
      this.appCoze()
    },
    appCoze() {
      let that = this
      this.$refs.sseTest.startChat({
        /**
         * 将它换成你的地址
         * 注意：
         * 如果使用 sse-server.js 要在手机端使用的话，请确保你的手机和电脑处在一个局域网下并且是正常的ip地址
         */
        url: 'https://api.coze.cn/v1/workflow/stream_run',
        // 请求头
        headers: {
          // that.aiConfig.token
          Authorization: 'Bearer ' + 'pat_cHNGkdbe3VRsmpMUXCNbZVt1ltDVyCI8k2bt9soIm2te76KXmtIfrOMKJ7xdwtYc',
          'Content-Type': 'application/json',
        },
        // 默认为 post
        method: 'post',
        body: {
          parameters: {
            txt: that.aitext,
          },
          // workflow_id: that.aiConfig.id,
          workflow_id: '7520535773797318690',
          // workflow_id: '7492238568816656410',
        },
      })
    },
  },
}
</script>

<style>
</style>