<template>
  <div id="index-container" class="container index-container">
    <el-row>
      <el-col :span="8">
        <el-input placeholder="输入知识库名称" clearable v-model="nameLike" size="medium" class="underBorder" @keydown.enter.native="getList">
          <template slot="prepend">
            <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
          </template>
        </el-input>
      </el-col>
      <el-col :span="16" class="text-right">
        <el-button class="render-button line-button pv8 ph10" icon="el-icon-plus" @click="createdKnowLedge()">创建知识库</el-button>
      </el-col>
    </el-row>

    <div slot="header" class="clearfix mt30 flex">
      <div>
        <h3>知识库列表</h3>
        <p class="mt20 fs14 theme-primary-desc-text" style="line-height: 10px">
          易商数智知识库平台，旨在为用户提供更便捷、更灵敏、更全面的知识存储和操作平台，用户可以通过创建不同的知识库，完成对系统内知识的细分和储备。
        </p>
        <p class="mt10 fs14 theme-primary-desc-text" style="line-height: 15px">同时也可以通过修改和删除操作，对系统内知识体系进行修正和优化。</p>
        <el-button type="primary" class="mt20 render-button pv8 ph10" plain icon="el-icon-document-remove">操作文档</el-button>
      </div>
    </div>

    <div class="card-body">
      <div class="ledge-list mt20">
        <el-card v-for="(item, index) in datasetList" class="item datasetList" shadow="hover" :key="index" @click.native.stop="knowLedgeDetail(item)">
          <div class="dataset-header">
            <div class="folder-content">
              <img :src="konwledgePng_1" alt="" class="folder" />
              <!--              <svg-icon iconClass="folder" className="folder"></svg-icon>-->
            </div>
            <div class="dataset-body" style="flex:1">
              <h4 class="dataset-title">{{ item.name }}</h4>
              <p class="dataset-desc">{{ item.description }}</p>
            </div>
          </div>

          <div class="flex align-items-c justify-content-b mh20 mv10">
            <div class="theme-primary-desc-text-drank fs12">
              {{ item.segmentedMode | filterSegmentedMode }}
            </div>
            <div class="opacity-button">
              <el-button class="default render-button" icon="el-icon-delete" @click.stop="deleteKnowLedge(item)">删除</el-button>
              <el-button class="render-button line-button" icon="el-icon-edit" @click.stop="createdKnowLedge(item)" type="primary">修改</el-button>
            </div>
          </div>

          <!--            <el-button type="text" @click="knowLedgeDetail(item)">查看详情</el-button>-->
        </el-card>
      </div>
    </div>
  </div>
</template>
<script>
import { docManageDataset, datasetDelete } from '@/api/generatedApi/index'
import knowledgeForm from '@/views/knowledge/detail/components/knowledgeForm.vue'
import konwledgePng_1 from '@/assets/images/konwledge/konwledge-1.png'

export default {
  name: 'index',
  data() {
    return {
      konwledgePng_1,
      datasetList: [],
      nameLike: '',
      drawer: false,
      direction: 'ltr',
      size: '50%',
      withHeader: false,
      drawerTitle: '创建知识库',
      knowledgeId: '',
      knowledgeForm: {
        name: 'knowledgeForm',
        component: knowledgeForm
      }
    }
  },
  props: {},
  watch: {
    nameLike(newVal) {
      if (!newVal) {
        this.getList()
      }
    }
  },
  components: {
    knowledgeForm
  },
  methods: {
    getList() {
      docManageDataset({ nameLike: this.nameLike }).then(res => {
        this.datasetList = res.content.content
      })
    },
    createdKnowLedge(item) {
      this.$router.push({
        path: '/knowledge/knowledge-create',
        query: {
          datasetId: item ? item.id : null
        }
      })
    },
    knowLedgeDetail(item) {
      this.$router.push({
        path: '/knowledge/detail',
        query: {
          datasetId: item.id
        }
      })
    },
    deleteKnowLedge(item) {
      this.$confirm('此操作将永久删除该知识库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        datasetDelete({ id: item.id }).then(res => {
          if (res.content.result === '0') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          }
        })
      })
    }
  },
  filters: {
    filterSegmentedMode(val) {
      switch (val) {
        case 0:
          return '传统分段模式'
        case 1:
          return ' Q&A分段模式'
        default:
          return '未知模式'
      }
    }
  },
  created() {},
  mounted() {
    this.getList()
  },
  computed: {}
}
</script>
<style scoped lang="scss">
#index-container {
  background-image: url('../../assets/images/backimage.png');
  background-size: 600px 300px;
  background-position-x: right;
  background-position-y: top;
  background-repeat: no-repeat;
}

.ledge-list {
  display: flex;
  flex-wrap: wrap;
  //justify-content: space-between;
  //margin-left: 2.5%;
  & .item {
    border-radius: 8px;
    width: calc(33.33% - 10px); // 每个项目占 33.33%，并预留间隙
    margin-bottom: 10px;
    margin-left: 0; // 移除左右内边距
    margin-right: 15px;
    /deep/ .el-card__body {
      padding: 0;
      & .item-body {
        padding: 0 20px;
        display: flex;
        height: 119px;
        margin-top: 20px;
      }
      & .item-bottom {
        //padding:0 20px;
        display: flex;
        justify-content: space-between;
        background: #f7f9fa;
        border-top: 1px solid #eff0f1;
      }
    }
  }

  & .item:nth-child(3n) {
    margin-right: 0;
  }

  & .dashed {
    border: 1px dashed #dedede;
    cursor: pointer;
    text-align: center;
    line-height: 200px;
    color: #6666;
    &:hover {
      color: #409eff;
      border: 1px dashed #409eff;
    }
  }

  & .folder-content {
    //background-color: rgba(245, 248, 255, 1);
    text-align: center;
    border-radius: 8px;
    //padding: 10px;
    & .folder {
      width: 40px;
      height: 40px;
    }
  }
}
.opacity-button {
  opacity: 0;
}
.datasetList {
  position: relative;
  transition: all 0.3s ease;
  overflow: hidden;
  cursor: pointer;

  &:hover {
    //transform: translateY(-3px);
    background: linear-gradient(211deg, #eef4fd 0%, #ffffff 100%);
    box-shadow: 0px 4px 7px 2px #dbe3f2;
    border-radius: 7px;
    border: 1px solid #635cff;
    .opacity-button {
      transition: all 0.5s ease;
      opacity: 1;
    }
  }

  /deep/ .el-card__body {
    padding: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .dataset-header {
    padding: 20px 20px 0;
    display: flex;
    justify-content: space-between;
    align-items: start;

    //align-items: center;

    .folder-content {
      //width: 50px;
      //height: 50px;
      //background: linear-gradient(135deg, #e6f7ff 0%, #bae7ff 100%);
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .dataset-type {
      font-size: 12px;
      border-radius: 4px;
    }
  }

  .dataset-body {
    flex: 1;
    max-width: 90%;
    padding: 0 10px;

    .dataset-title {
      font-family: PingFangSC, PingFang SC;
      font-weight: 600;
      font-size: 16px;
      color: #000000;
      line-height: 22px;
      text-align: left;
      font-style: normal;

      margin: 0 0 5px;
      white-space: nowrap; // 确保单行显示
      overflow: hidden; // 隐藏超出部分
      text-overflow: ellipsis; // 显示省略号
      max-width: 100%; // 限制宽度
    }

    .dataset-desc {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 13px;
      color: #666666;
      line-height: 18px;
      text-align: left;
      font-style: normal;

      height: 35px;
      margin: 0;
      display: -webkit-box; // 支持多行省略
      -webkit-line-clamp: 2; // 限制为两行
      -webkit-box-orient: vertical; // 垂直方向排列
      overflow: hidden; // 隐藏超出部分
      text-overflow: ellipsis; // 显示省略号
      max-width: 100%; // 限制宽度
    }
  }

  .dataset-footer {
    margin-top: auto;
    display: flex;
    border-top: 1px solid #f0f2f5;
    background: linear-gradient(to bottom, #fafbfc, #f5f7fa);
  }
}
</style>
