<template>
  <div class="PoliceAi">
    <HistoricalRecords ref="HistoricalRecordsRef" @getDetail="getDetail" @addNew="addNew" />
    <div class="PoliceAi-main">
      <div class="case-title">
        <div class="case-item">
          <span>案件名称</span>
          <span>|</span>
          <el-select v-model="caseId" :disabled="conversationId !==''" style="width: 200px;" clearable placeholder="请选择案件" @change="selectCase">
            <el-option v-for="item in caseOptions" :key="item.id" :label="item.caseName" :value="item.id" />
          </el-select>

        </div>
        <el-input v-model="caseActorName" class="case-input" readonly placeholder="行为人" />
      </div>
      <el-scrollbar ref="scrollbar" :style="{marginTop:firstEnter?'146px':''}" class="main-content">

        <div class="robot">
          <div class="robot-top">
            <img src="../../assets/police/robot.png" alt="">
            <span>数字民警</span>
          </div>
          <span class="robot-item">
            你好，本数字民警可为您提供案件分析结果查询服务，您还能通过输入 @来选取特定罪与非罪指标结果，以便精准获取您所需信息。
          </span>
        </div>
        <div v-if="navListFlag" class="nav-list">
          <div v-for="(item, index) in intentTypeList" :key="index" class="nav-item" @click="selectCaseType(item)">
            <div class="nav-item-top">
              <img v-if="item.type ==='1'" src="../../assets/police/analysis.png" alt="">
              <img v-if="item.type ==='2'" src="../../assets/police/case_info.png" alt="">
              <img v-if="item.type ==='3'" src="../../assets/police/evidence.png" alt="">
              <span>{{ item.name }}</span>
            </div>
            <span class="desc">{{ item.desc }}</span>
          </div>
        </div>
        <div
          v-for="(message, index) in conversationList"
          :id="'message' + index"
          :key="index"
          class="conversation-item"
        >
          <div v-if="message.role === 'user'" class="user-message">
            {{ message.text }}
          </div>
          <transition name="fade">
            <div v-if="message.role === 'robot'" class="robot">
              <div class="robot-top">
                <img src="../../assets/police/robot.png" alt="">
                <span>数字民警</span>
              </div>
              <div v-if="index !== conversationList.length -1 || !loading">
                <!-- 案件分析结果 -->
                <div v-if="message.intentType === '1'" class="robot-item">
                  <span>您好，【<span>{{ message.caseName }}</span> 】的综合得分为<span :class="['score', message.totalScore >= 70 ? 'success' : 'warning']">{{ `${message.commonScore}+${message.specificCrimeScore}=${message.totalScore}` }}</span>
                    分, 认定<span :class="['score', message.totalScore >= 70 ? 'success' : 'warning']">{{ message.scoreDesc }}</span>
                  </span>
                  <span class="robot-message-item">
                    <span class="point" />
                    <span>{{ `${message.commonIndexCount}个共性证据指标占${message.commonIndexHitCount}个` }}</span>
                  </span>
                  <span class="robot-message-item">
                    <span class="point" />
                    <span>{{ `${message.specificCrimeIndexCount}个${message.specificCrime}占${message.specificCrimeIndexHitCount}个` }}</span>
                  </span>
                </div>
                <!-- 案件概述 案件证据指引 -->
                <div v-if="message.intentType === '2' || message.intentType === '3'" class="robot-item">
                  <span class="robot-message-item">
                    {{ message.text }}
                  </span>
                </div>
                <!-- 指标回答 -->
                <div v-if="message.intentType === '0'">
                  <div class="robot-item index-content">
                    <span class="index-item">
                      <span class="label">指标类型：</span>
                      <span class="value">{{ getIndexType(message.indexType) }}</span>
                    </span>
                    <span class="index-item">
                      <span class="label">指标名称：</span>
                      <span class="value">{{ message.indexName }}</span>
                    </span>
                    <span class="index-item">
                      <span class="label">分析结果：</span>
                      <span class="value">{{ message.result }}</span>
                    </span>
                  </div>
                  <div v-if="message.qaSplitList.length > 0 || message.evidenceNames.length > 0" class="collapse" @click="changeShowFlag(message)">
                    <span>相关笔录及证据</span>
                    <i v-if="!message.showFlag" class="el-icon-arrow-down" />
                    <i v-if="message.showFlag" class="el-icon-arrow-up" />
                  </div>
                  <div v-if="message.showFlag" class="collapse-content">
                    <div v-if="message.qaSplitList.length > 0" class="title">涉及笔录：</div>
                    <div v-for="(item, index) in message.qaSplitList" :key="index" class="record-item">
                      <span class="file-name" @click="downloadRecord(item)">{{ item.noteName }}</span>
                      <span>{{ item.answer }}</span>
                      <span>{{ item.question }}</span>
                    </div>
                    <div v-if="message.evidenceNames.length > 0" class="title">证据：</div>
                    <div class="evidence-list">
                      <div v-for="(item, index) in message.evidenceNames" :key="index" class="evidence-item">
                        <span class="file-name" @click="downloadEvidence(item)">{{ item.evidenceName }}</span>
                      </div>
                    </div>

                  </div>
                </div>

                <!-- dify回答 -->
                <div v-if="message.type === '0'">
                  <div class="robot-item">
                    <span class="robot-message-item">
                      {{ message.answer }}
                    </span>
                  </div>
                  <div v-if="message.segmentVOList.length > 0" class="collapse">
                    <span>相关知识内容</span>
                    <i v-if="!message.showFlag" class="el-icon-arrow-down" />
                    <i v-if="message.showFlag" class="el-icon-arrow-up" />
                  </div>
                  <div v-if="message.segmentVOList.length > 0 && message.showFlag" class="collapse-content">
                    <div class="title">涉及文件：</div>
                    <div v-for="(item, index) in message.segmentVOList" :key="index" class="record-item">
                      <span class="file-name ">{{ item.name }}</span>
                      <span v-html="item.snippet" />
                    </div>
                  </div>
                </div>
              </div>
              <div v-else class="loading">
                <img src="../../assets/common/loading.gif" alt="">
                <span>模型正在生成结果....</span>
              </div>
              <!-- <div v-if="message.type === '3'">
                <div>您好，您是要查询【案件名称】的分析结果吗？</div>
                <div class="btns">
                  <div class="btn-item">是</div>
                  <div class="btn-item">否</div>
                </div>
              </div> -->

            </div>
          </transition>

        </div>
      </el-scrollbar>

      <div class="send-bottom">
        <el-input
          v-model="sendText"
          placeholder="可咨询案件内容、输入@可选择查询指标结果"
          @keyup.enter.native="handleSend"
        />
        <img v-if="!loading" src="@/assets/police/send.png" alt="" @click="handleSend">
        <img v-if="loading" src="@/assets/police/pause.png" alt="">
        <div v-if="indexFlag" class="select_index">
          <div class="top">
            <span v-for="(item, index) in indexTypeList" :key="index" :class="[item.type === activedIndex?'actived':'']" @click="changeType(item)">
              {{ item.name }}
            </span>

          </div>
          <el-input
            v-model="searchName"
            maxlength="50"
            class="top-input"
            placeholder="请输入内容"
          >
            <i slot="prefix" class="el-input__icon el-icon-search" />
          </el-input>
          <div class="index-list">
            <span v-for="(item,index) in showIndexList" :key="index" class="index-list-item" @click="selectIndex(item)">
              {{ item.name }}
            </span>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { queryCaseList, robotChat, conversationInfoList } from '@/api/caseManagement'
import { queryIndexData } from '@/api/indexRule'
import HistoricalRecords from './HistoricalRecords.vue'
import { baseURL } from '@/config'
import { downloadEvidence, commonDownloadFile } from '@/api/config/uploadApi'
export default {
  name: 'PoliceAi',
  components: {
    HistoricalRecords
  },
  data() {
    return {
      caseId: '',
      caseActorName: '',
      // 是否第一次进入
      firstEnter: true,
      navListFlag: true,
      sendText: '',
      indexFlag: false,
      loading: false,
      conversationId: '',
      conversationList: [],
      indexTypeList: [
        {
          name: '共性指标',
          type: '1'
        },
        {
          name: '入罪指标',
          type: '2'
        },
        {
          name: '出罪指标',
          type: '3'
        }
      ],
      activedIndex: '1',
      indexList: [],
      showIndexList: [], // 展示指标
      intentTypeList: [
        {
          name: '案件分析结果',
          type: '1',
          desc: '案件分析预测入罪与出罪，提示司法判定风险'
        },
        {
          name: '案件概况',
          type: '2',
          desc: '获取案件的基本情况概述'
        },
        {
          name: '案件证据指引',
          type: '3',
          desc: '为补充证据材料信息提供导向'
        }
      ],
      searchName: '',
      caseOptions: []
    }
  },
  watch: {
    sendText(val) {
      if (val === '@') {
        this.indexFlag = true
        this.activedIndex = '1'
        this.getIndexData('1')
      } else {
        this.indexFlag = false
      }
    },
    searchName(val) {
      if (val) {
        const list = this.filterByProperty(this.indexList, 'name', val)
        this.showIndexList = list
      } else {
        this.showIndexList = this.indexList
      }
    }
  },

  mounted() {
    this.fetchData()
  },
  methods: {
    // 获取详情
    async getDetail(id, caseId) {
      this.loading = false
      this.caseId = caseId
      this.selectCase(this.caseId)
      this.conversationList = []
      this.conversationId = id
      const res = await conversationInfoList({
        conversationId: this.conversationId,
        pageNum: 1,
        pageSize: 99999
      })
      if (res.code === 200) {
        for (const item of res.data.records) {
          this.conversationList.push({
            role: 'user',
            text: item.question
          })
          this.getAnswerContent(item)
          this.navListFlag = false
        }
      }
    },
    // 开启新对话
    addNew() {
      this.caseId = ''
      this.conversationId = ''
      this.caseActorName = ''
      this.conversationList = []
      this.navListFlag = true
    },
    // 请求接口数据
    fetchData() {
      queryCaseList({}, 1, 99999).then(res => {
        this.caseOptions = res.data.records
      })
    },
    // 查询指标
    getIndexData(val) {
      queryIndexData({
        indexType: val
      }, 1, 9999).then(res => {
        this.indexList = res.data.result
        this.showIndexList = res.data.result
      })
    },
    // 切换指标类型
    changeType(item) {
      this.activedIndex = item.type
      this.getIndexData(item.type)
      this.searchName = ''
    },
    selectCase(val) {
      const obj = this.caseOptions.find(item => item.id === val)
      this.caseActorName = obj.caseActorName
    },
    selectCaseType(item) {
      if (this.loading) return
      if (!this.caseId) {
        this.$baseMessage.error('请选择案件！')
        return
      }
      this.conversationList.push({
        role: 'user',
        text: item.name
      })
      this.navListFlag = false
      this.chat({
        caseId: this.caseId,
        conversationId: this.conversationId,
        query: item.name,
        type: '1',
        intentType: item.type
      })
    },
    // 过滤数组
    filterByProperty(arr, key, searchString) {
      return arr.filter(obj =>
        obj[key] && obj[key].toString().toLowerCase().includes(searchString.toLowerCase())
      )
    },
    downloadFile(url, fileName) {
      const link = document.createElement('a')
      link.href = url
      link.download = fileName
      link.click()
    },
    // 下载证据
    downloadEvidence(item) {
      this.downloadFile(`${baseURL}${downloadEvidence}${item.evidenceId}`, item.evidenceName)
    },
    // 下载笔录
    downloadRecord(item) {
      // const fileId = item.fileIds.split(',')[0]
      this.downloadFile(`${baseURL}${commonDownloadFile}${item.noteFileId}`, item.noteName)
    },
    // 选择指标
    selectIndex(item) {
      if (this.loading) return
      this.indexFlag = false
      this.searchName = ''
      this.conversationList.push({
        role: 'user',
        text: item.name
      })
      this.chat({
        caseId: this.caseId,
        query: item.name,
        conversationId: this.conversationId,
        type: '1',
        intentType: '0'
      })
    },
    chat(obj) {
      this.loading = true
      robotChat(obj).then(res => {
        if (res.code === 200) {
          if (this.conversationId !== res.data.conversationId) {
            this.$refs.HistoricalRecordsRef.getList()
          }
          this.conversationId = res.data.conversationId
          this.getAnswerContent(res.data)
        }
      })
    },
    // 识别回答内容
    getAnswerContent(data) {
      if (data.intentType === '1') {
        this.conversationList.push({
          ...data.answwerMap,
          role: 'robot',
          intentType: data.intentType
        })
      } else if (data.intentType === '2') {
        this.conversationList.push({
          role: 'robot',
          intentType: data.intentType,
          text: data.answwerMap.answerText

        })
      } else if (data.intentType === '3') {
        this.conversationList.push({
          role: 'robot',
          intentType: data.intentType,
          text: data.answwerMap.guideDesc

        })
        // 指标问答
      } else if (data.intentType === '0') {
        this.conversationList.push({
          role: 'robot',
          intentType: data.intentType,
          ...data.answwerMap
        })
      }
      // dify 问答
      if (data.type === '0') {
        this.conversationList.push({
          role: 'robot',
          answer: data.answer,
          type: data.type,
          segmentVOList: data.segmentVOList
        })
      }
      this.$nextTick(() => {
        setTimeout(() => {
          this.loading = false
        }, 3000)

        this.scrollToBottom()
      })
    },
    // 获取指标类型名称
    getIndexType(val) {
      for (const item of this.indexTypeList) {
        if (val === item.type) {
          return item.name
        }
      }
    },
    // 展开收缩具体内容
    changeShowFlag(item) {
      if (item.showFlag === true) {
        this.$set(item, 'showFlag', false)
      } else {
        this.$set(item, 'showFlag', true)
      }
    },
    scrollToBottom() {
      const scrollbar = this.$refs.scrollbar
      if (scrollbar) {
        scrollbar.wrap.scrollTo(0, scrollbar.wrap.scrollHeight)
      }
    },
    handleSend() {
      if (this.loading) return
      if (!this.caseId) {
        this.$baseMessage.error('请选择案件！')
        return
      }
      if (!this.sendText) {
        this.$baseMessage.error('请输入内容！')
        return
      }

      this.conversationList.push({
        role: 'user',
        text: this.sendText
      })
      this.scrollToBottom()
      this.chat({
        caseId: this.caseId,
        query: this.sendText,
        conversationId: this.conversationId,
        type: '0',
        intentType: ''
      })
      this.sendText = ''
    }
  }
}
</script>
<style lang="scss" scoped>
.PoliceAi {
    background: #FFFFFF;
    height: 100%;
    position: relative;
    padding-top: 24px;
    display: flex;
    .PoliceAi-main {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;
      .case-title {
        width: 632px;
        height: 98px;
        background: rgba(55,99,255,0.15);
        border-radius: 21px 21px 21px 21px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        z-index: 9;
        top: 24px;
        .case-item {
            display: flex;background: #FFFFFF;
            border-radius: 6px 6px 6px 6px;
            border: 1px solid #D9D9D9;
            padding: 8px 16px;
            align-items: center;
            justify-content: center;
            span {
                font-size: 16px;
                color: #333333;
                margin-right: 16px;
            }
            ::v-deep {
                .el-input__inner {
                    border: none !important;
                }
            }

        }
        .case-input {
                width: 202px;
                height: 50px;
                background: #F4F8FF;
                border-radius: 6px 6px 6px 6px;
                margin-left: 16px;
                ::v-deep {
                .el-input__inner {
                    height: 50px !important;
                }
            }
            }
    }
    .main-content {
        flex: 1;
        overflow-y: auto;
        width: 1154px;
        position: relative;
        // padding-right: 16px;
        .robot {

            .robot-top {
                display: flex;
                align-items: center;
                margin-bottom: 10px;
                img {
                    width: 45px;
                    height: 45px;
                    margin-right: 10px;
                }
                span {
                    font-size: 20px;
                    color: #3763FF;
                }
            }
            .loading {
              display: flex;
              background: #F5F7FF;
              border-radius: 16px 0px 16px 16px;
              line-height: 28px;font-size: 16px;
              color: #333333;
              padding: 16px;
              margin-bottom: 16px;
              align-items: center;
              img {
                width: 38px;
                height: 38px;
                margin-right: 8px;
              }
            }
            .robot-item {

                background: #F5F7FF;
                border-radius: 16px 0px 16px 16px;
                line-height: 28px;font-size: 16px;
                color: #333333;
                padding: 16px;
                width: 90%;
                margin-bottom: 16px;
                display: flex;
                flex-direction: column;
                .success {
                  color: #00975E;
                  font-weight: bold;
                }
                .warning {
                  color: rgba(255, 147, 17, 1);
                  font-weight: bold;
                }
                .robot-message-item {
                  display: flex;
                  align-items: center;
                  .point {
                    width: 5px;
                    height: 5px;
                    background-color: black;
                    border-radius: 50%;
                    margin: 0 10px;
                  }
                }
            }
            .index-content {
              display: flex;
              flex-direction: column;
              .index-item {
                display: flex;
                margin-bottom: 16px;
                .label {
                  font-weight: bold;
                  font-size: 16px;
                  color: #333333;
                  margin-right: 10px;
                }
                .value {
                  font-size: 16px;
                  color: #333333;
                }
              }
            }
            .collapse {
              width: 198px;
              padding: 10px 16px;
              background: #F6F7F9;
              border-radius: 6px 6px 6px 6px;
              font-size: 16px;
              display: flex;
              color: #666666;
              justify-content: space-between;
              margin-bottom: 8px;
              cursor: pointer;
            }
            .collapse-content {
              padding: 16px;
              background: #F6F7F9;
              border-radius: 6px 6px 6px 6px;
              .title {
                font-size: 16px;
                color: #333333;
                margin-bottom: 8px;
              }
              .record-item {
                margin-bottom: 16px;
                display: flex;
                flex-direction: column;font-size: 16px;
                color: #333333;
                line-height: 28px;
                .file-name {
                  font-size: 16px;
                  color: #3763FF;
                  cursor: pointer;
                  margin-bottom: 8px;
                }
              }
              .evidence-list {
                display: flex;
                .evidence-item {
                  font-size: 16px;
                  color: #3763FF;
                  cursor: pointer;
                  margin-bottom: 8px;
                  margin-right: 8px;
                }
              }
            }
        }
        .user-message {
          margin: 24px 0;
          text-align: right;font-size: 16px;
          color: #333333;
          padding-right: 16px;
        }
        .nav-list {
            display: flex;
            margin-top: 16px;
            margin-bottom: 16px;
            .nav-item {
                background: #FFFFFF;
                border-radius: 8px 8px 8px 8px;
                border: 1px solid #D9D9D9;
                padding: 24px;
                margin-right: 24px;
                cursor: pointer;
                .desc {
                    font-size: 14px;
                    color: #999999;
                }
                .nav-item-top {
                    display: flex;
                    margin-bottom: 8px;
                    align-items: center;
                    img {
                        width: 30px;
                        height: 30px;
                        margin-right: 10px;
                    }
                }
            }
        }

    }
    .main-content::-webkit-scrollbar {
          // display: none; /* 对于 Chrome, Safari 和 Opera 隐藏滚动条 */
        }
    .send-bottom {
        width: 1154px;
        height: 75px;
        position: relative;
        background: #FFFFFF;
        border-radius: 16px 16px 16px 16px;
        border: 2px solid rgba(55, 99, 255, 1);
        // border-image: linear-gradient(90deg, rgba(55, 99, 255, 1), rgba(25, 166, 254, 1)) 2 2;
        margin-bottom: 56px;
        display: flex;
        align-items: center;
        position: relative;
        ::v-deep {
            .el-input__inner {
                border: none !important;
                font-size: 16px;
            }
        }
        img {
            width: 60px;
            height: 60px;
            position: absolute;
            right: 12px;
            top: 12px;
            cursor: pointer;
        }
        .select_index {
            position: absolute;
            bottom: 77px;
            left: 12px;
            width: 1132px;
            height: 424px;
            background: #FFFFFF;
            box-shadow: 0px 0px 8px 0px rgba(0,0,0,0.15);
            border-radius: 6px 6px 6px 6px;
            .top {
              height: 62px;
              display: flex;
              border-bottom: 1px solid #EAEAEA;
              align-items: center;
              padding-left: 24px;
              span {
                font-size: 16px;
                height: 62px;
                line-height: 62px;
                color: #999999;
                margin-right: 40px;
                cursor: pointer;
              }
              .actived {
                color: #3763FF;
                border-bottom: 3px solid #3763FF;
              }
            }
            .top-input {
                width: 1000px;
                border-radius: 6px 6px 6px 6px;
                border: 1.4px solid #3763FF;
                margin-top: 24px;
                margin: 24px;
              }
              .index-list {
                display: flex;
                flex-direction: column;
                overflow: auto;
                padding-left: 24px;
                height: 260px;
                .index-list-item {
                  font-weight: 400;
                  font-size: 16px;
                  color: #333333;
                  cursor: pointer;
                  margin-bottom: 24px;
                }
              }
          }
    }
    }

}
</style>
<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
