<template>
  <div id="index-container" class="container index-container">
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <h3>知识库列表</h3>
        <p class="mt10 fs14" style="line-height: 20px">
          易商数智知识库平台，旨在为用户提供更便捷、更灵敏、更全面的知识存储和操作平台，用户可以通过创建不同的知识库，完成对系统内知识的细分和储备。
        </p>
        <p class="mt10 fs14" style="line-height: 20px">同时也可以通过修改和删除操作，对系统内知识体系进行修正和优化。</p>
        <el-button type="text" disabled class="mt10">操作文档</el-button>
      </div>

      <div class="card-body">
        <el-row>
          <el-col :span="8">
            <el-input placeholder="输入知识库名称" clearable v-model="nameLike">
              <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <div class="ledge-list mt20">
          <el-card shadow="hover" class="item dashed">
            <div @click="createdKnowLedge()">
              新增知识库
            </div>
          </el-card>
          <el-card v-for="(item, index) in datasetList" class="item datasetList" shadow="hover" :key="index">
            <div class="dataset-header">
              <div class="folder-content">
                <svg-icon iconClass="folder" className="folder"></svg-icon>
              </div>
              <el-tag size="small" type="primary" effect="plain" class="dataset-type">
                {{ item.segmentedMode | filterSegmentedMode }}
              </el-tag>
            </div>
            <div class="dataset-body">
              <h4 class="dataset-title">{{ item.name }}</h4>
              <p class="dataset-desc">{{ item.description }}</p>
            </div>
            <div class="dataset-footer">
              <el-button type="text" @click="knowLedgeDetail(item)">查看详情</el-button>
              <el-button type="text" @click="createdKnowLedge(item)">修改</el-button>
              <el-button type="text" class="danger" @click="deleteKnowLedge(item)">删除</el-button>
            </div>
          </el-card>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import { docManageDataset, datasetDelete } from '@/api/generatedApi/index'
import knowledgeForm from '@/views/knowledge/detail/components/knowledgeForm.vue'

export default {
  name: 'index',
  data() {
    return {
      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">
.ledge-list {
  display: flex;
  flex-wrap: wrap;
  //justify-content: space-between;
  //margin-left: 2.5%;
  & .item {
    border-radius: 8px;
    width: calc(25% - 20px); // 每个项目占 25%，并预留间隙
    margin-bottom: 20px;
    margin-left: 0; // 移除左右内边距
    margin-right: 25px;
    /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;
        & .el-button {
          color: #8d8f8f;
          width: 33%;
          text-align: center;
          &:nth-child(2) {
            position: relative;
            &::before {
              position: absolute;
              content: '';
              left: 0;
              height: 100%;
              width: 1px;
              background: #eff0f1;
              top: 0;
            }
            &::after {
              position: absolute;
              content: '';
              right: 0;
              height: 100%;
              width: 1px;
              background: #eff0f1;
              top: 0;
            }
          }
        }
      }
    }
  }

  & .item:nth-child(4n) {
    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;
    }
  }
}

.datasetList {
  position: relative;
  transition: all 0.3s ease;
  overflow: hidden;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important;
  }

  /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: 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;

      .folder {
        width: 30px;
        height: 30px;
        color: #1890ff;
      }
    }

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

  .dataset-body {
    flex: 1;
    padding: 16px 20px;

    .dataset-title {
      margin: 0 0 12px;
      font-size: 16px;
      font-weight: 600;
      color: #2c3e50;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .dataset-desc {
      margin: 0;
      font-size: 13px;
      color: #606266;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      line-height: 1.6;
      height: 42px;
    }
  }

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

    .el-button {
      flex: 1;
      height: 42px;
      font-size: 14px;
      padding: 0;
      border-radius: 0;
      position: relative;
      color: #606266;
      transition: all 0.25s ease;
      margin-left: 0;

      &:hover {
        color: #409eff;
        background-color: rgba(64, 158, 255, 0.1);
      }

      &.danger:hover {
        color: #f56c6c;
        background-color: rgba(245, 108, 108, 0.1);
      }

      &:not(:last-child)::after {
        content: '';
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 1px;
        height: 20px;
        background-color: #e8e8e8;
      }
    }
  }
}
</style>
