<template>
  <div :class="['chat-item', 'ai-message']">
    <!-- 头像 -->
    <img :src="aiIcon" class="user-icon" alt="" />
    <!-- 内容 -->
    <div class="message-content">
      <div class="message-time">{{ moment().format('YYYY/MM/DD HH:mm:ss') }}</div>
      <div class="message-text">
        <span>今天我能为您做些什么呢?</span>
        <p>作为你的智慧伙伴，我不仅可以写文案、出点子，还可以和你聊天、回答问题。您也可以点击</p>

        <div class="issue-list">
          <div class="issue-top">
            <div class="issue-title">下面的问题常用问题快速向我提问。</div>
            <div class="issue-btn" @click="handlerChange()">
              <n-icon size="14">
                <svg
                  version="1.1"
                  xmlns="http://www.w3.org/2000/svg"
                  xmlns:xlink="http://www.w3.org/1999/xlink"
                  x="0px"
                  y="0px"
                  viewBox="0 0 512 512"
                  enable-background="new 0 0 512 512"
                  xml:space="preserve"
                >
                  <path
                    d="M433,288.8c-7.7,0-14.3,5.9-14.9,13.6c-6.9,83.1-76.8,147.9-161.8,147.9c-89.5,0-162.4-72.4-162.4-161.4
	c0-87.6,70.6-159.2,158.2-161.4c2.3-0.1,4.1,1.7,4.1,4v50.3c0,12.6,13.9,20.2,24.6,13.5L377,128c10-6.3,10-20.8,0-27.1l-96.1-66.4
	c-10.7-6.7-24.6,0.9-24.6,13.5v45.7c0,2.2-1.7,4-3.9,4C148,99.8,64,184.6,64,288.9C64,394.5,150.1,480,256.3,480
	c100.8,0,183.4-76.7,191.6-175.1C448.7,296.2,441.7,288.8,433,288.8L433,288.8z"
                  ></path>
                </svg>
              </n-icon>
              <span class="refresh">换一批</span>
            </div>
          </div>
          <div class="issue-list">
            <div class="issue-item" @click="handlerQuerParmas(item.content)" v-for="(item, index) in list" :key="index">{{ item.content }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import moment from 'moment'
import aiIcon from '@/assets/images/ai/ai-icon.png'
import { ref, reactive, onMounted } from 'vue'
import { useMessage } from 'naive-ui'
import { getIssue } from '@/api/path'
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])

let list = ref([])
let queryParams = reactive({
  current: 1, size: 5
})
const message = useMessage()
let total = ref(0)

/**
 * 换一批
 */
 const handlerChange = ()=>{
  const totalPage = total.value / queryParams.size

  if(totalPage === queryParams.current){
    message.warning('没有更多了！')
    return
  }else{
    queryParams.current = getNonZeroRandom(totalPage);
    getList()
  }

 }

 const handlerQuerParmas = content => {
  emit('update:modelValue', content)
  emit('sendMessage')


}

 const getNonZeroRandom = (a)=> {
    return Math.floor(Math.random() * a) + 1;
}

const getList = async () => {
  try {
    const res = await getIssue(queryParams)
    if (res && res.data) {
      list.value = res.data.records
      total.value = res.data.total
    }
  } catch {
    message.warning('查询出错！请联系管理员')
  }
}

onMounted(()=>{
  getList()
})
</script>

<style scoped>
.message-text {
  span {
    font-weight: bold;
  }
  p {
    margin: 10px 0;
    font-size: 14px;
  }
  .issue-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .issue-title {
      font-size: 14px;
    }
    .issue-btn {
      display: flex;
      align-items: center;
      gap: 3px;
    }
    .refresh {
      cursor: pointer;
      font-size: 12px;
      font-weight: 400;
    }
  }
  .issue-list {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    .issue-item {
      font-size: 13px;
      cursor: pointer;
      box-sizing: border-box;
      padding: 5px 10px;
      background-color: #3d424d;
      border-radius: 4px;
    }
  }
}
</style>
