<template>
  <div class="segment-split-view">
    <div class="segment-list">
      <!--      <el-checkbox-group v-model="selectedSegments">-->
      <div
        class="segment-list-item"
        v-for="(segment, index) in descriptions.data"
        :key="index"
        :class="{ active: activeSegment === index }"
        @click="handleSegmentClick(index)"
      >
        <div class="flex align-items-c justify-content-b" style="gap: 8px">
          <div class="flex align-items-c " style="gap: 8px">
            <!--          <el-checkbox class="mr pt15" :label="index">-->
            <span class="el-icon-s-unfold"></span>
            <span class="segment-number"
              >分段 - {{ (index + 1).toString().padStart(2, '0') }}</span
            >
            <span v-if="segment.word_count > 0">
              · {{ segment.word_count }}个字符 ·</span
            >
            <span> {{ segment.hit_count }} 次召回次数</span>
          </div>
          <div
            class="actions ml10 flex align-items-c mr10"
            style="gap: 10px"
            v-if="!noEdit"
          >
            <!--            删除 编辑-->
            <el-icon
              class="el-icon-edit mr10"
              @click.native.stop="handleSegmentClick(index)"
            ></el-icon>
            <el-icon
              class="el-icon-delete"
              @click.native.stop="deleteSegment(segment, index)"
            ></el-icon>
            <!--            <el-switch-->
            <!--              size="mini"-->
            <!--              @click.native.stop=""-->
            <!--              v-model="segment.enabled"-->
            <!--            ></el-switch>-->
          </div>
        </div>

        <!--          </el-checkbox>-->
        <div>
          <div>
            <p class="context">{{ segment.content }}</p>
          </div>

          <div
            class="segment-keywords flex"
            v-if="segment.keywords && segment.keywords.length"
          >
            <p
              v-for="(item, index) in segment.keywords"
              :key="index"
              class="mr10"
            >
              #{{ item }}
            </p>
          </div>
        </div>
      </div>
      <!--      </el-checkbox-group>-->
    </div>

    <!-- 弹窗 -->
    <el-drawer
      title="分段详情"
      :wrapperClosable="false"
      :visible.sync="dialogVisible"
      :modal="false"
      append-to-body
      :before-close="handleClose"
    >
      <div
        style="height:calc(100% - 32px);overflow-x: hidden;overflow-y: auto"
        v-if="
          activeSegment !== null &&
            descriptions.data &&
            descriptions.data.length > 0
        "
      >
        <div class="segment-content">
          <p contenteditable class="resetHtml fs13" ref="content">
            {{ descriptions.data[activeSegment].content }}
          </p>
          <div
            class="flex align-items-c mt20"
            v-if="
              descriptions.data[activeSegment].keywords &&
                descriptions.data[activeSegment].keywords.length
            "
            style="flex-wrap: wrap"
          >
            <span>关键词 ：</span>
            <el-tag
              v-for="(item, index) in descriptions.data[activeSegment].keywords"
              :key="item"
              class="mr10 ellipsis"
              size="medium"
              :closable="
                descriptions.data[activeSegment].keywords.length > 1 && !noEdit
              "
              type="info"
              @close="tagClose(item)"
            >
              {{ item }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="createdTag"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
            />
            <el-button
              v-if="!noEdit && !createdTag"
              size="medium"
              @click="showInput"
              class="fs12"
              style="padding: 7px;border-radius: 4px;font-size: 12px"
              >添加标签</el-button
            >
          </div>
        </div>
      </div>
      <div class="text-right">
        <el-button @click="dialogVisible = false" size="medium">关闭</el-button>
        <el-button @click="saveUS" size="medium" type="primary" v-if="!noEdit"
          >保存</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { segmentDelete, segmentUpdate } from '@/api/generatedApi'

export default {
  name: 'TextModel',
  props: {
    noEdit: {
      type: Boolean,
      default: false
    },
    visible: Boolean,
    descriptions: {
      type: Object,
      default: () => ({ data: [] })
    },
    parentForm: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      createdTag: false,
      inputValue: '',
      activeSegment: null,
      dialogVisible: false,
      selectedSegments: []
    }
  },
  watch: {},
  methods: {
    deleteSegment(segment, index) {
      this.$messageBox(
        () => {
          segmentDelete({
            documentId: this.parentForm.id,
            segmentId: segment.id
          }).then(res => {
            if (res) {
              this.$message.success('删除成功')

              this.descriptions.data.splice(index, 1)
            }
          })
        },
        '是否删除当前分段,删除后不可恢复',
        'warning',
        '提示'
      )
    },

    saveUS() {
      let params = {
        keywords: this.descriptions.data[this.activeSegment].keywords,
        content: this.$refs.content.innerHTML,
        answer: null
      }
      this.saveSegment(params, () => {
        this.$message.success('保存成功')
      })
    },

    showInput() {
      this.createdTag = true
    },
    tagClose() {
      this.descriptions.data[this.activeSegment].keywords.splice(index, 1)
      let params = {
        keywords: this.descriptions.data[this.activeSegment].keywords,
        content: this.$refs.content.innerHTML,
        answer: null
      }
      this.saveSegment(params)
    },

    handleInputConfirm() {
      let params = {
        keywords: [
          ...this.descriptions.data[this.activeSegment].keywords,
          this.inputValue
        ],
        content: this.$refs.content.innerHTML,
        answer: null
      }
      this.saveSegment(params)
    },

    saveSegment(params, fun) {
      params.documentId = this.parentForm.id
      params.segmentId = this.descriptions.data[this.activeSegment].id
      segmentUpdate(params).then(res => {
        if (res) {
          this.descriptions.data[this.activeSegment] = JSON.parse(
            JSON.stringify(res.content.content)
          )
          // res.content.content.keywords
          this.descriptions.data[this.activeSegment].keywords = JSON.parse(
            JSON.stringify(res.content.content.keywords)
          )
          // this.descriptions.data[this.activeSegment].answer =
          //   res.content.content.answer
          this.createdTag = false
          this.inputValue = ''
          if (fun) {
            fun()
          }
        }
      })
    },

    handleSegmentClick(index) {
      this.activeSegment = index
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped lang="scss">
.segment-split-view {
  display: block;
}
.context {
  width: auto;
  overflow: hidden;
  //省略号
  text-overflow: ellipsis;
  white-space: nowrap;

  color: #3a3f4f;
  font-size: 14px;
}
.segment-list {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.segment-list-item {
  color: #70778d;
  cursor: pointer;
  padding: 15px 0 20px 15px;
  border-radius: 2px;
  transition: background-color 0.3s;
  font-size: 14px;
  border-bottom: 1px solid #f3f5f7;
  position: relative;
  //margin-bottom: 10px;

  &.active {
    background: #f3f5f7;
  }

  .actions {
    position: absolute;
    display: none; /* 默认隐藏 */
    background: #fff;
    padding: 3px 5px;
    border-radius: 5px;
    right: 10px;
    top: 15px;
  }

  &:hover .actions {
    display: flex; /* 悬停或激活时显示操作按钮 */
    gap: 10px;
  }
  &:hover {
    background: #f3f5f7;
  }

  &.active {
    background: #f3f5f7;
  }

  p {
    margin: 15px 0;
  }

  .segment-number {
    //color: #0a84ff;
  }

  .segment-keywords {
    font-weight: 400;
    font-size: 12px;
    color: #70778d;
    flex-wrap: wrap;

    p {
      margin: 0 10px 0 0;
    }
  }
}

.segment-content {
  padding: 20px;
  background: #f9f9f9;
  border-radius: 15px;
  //white-space: pre-wrap;
  line-height: 35px;
  color: #666;
}
.input-new-tag {
  width: 100px;
}
</style>
