<template>
  <a-modal title="添加知识库文档" :visible="createAddUploadVisible" @cancel="handleClose" @ok="handleSave"
    :confirmLoading="confirmLoading" width="1000px">
    <div class="upload">
      <!-- <div class="head" @click="handleClose">
        <a-icon type="arrow-left"/>
        <span class="head-title">添加知识库文档</span>
      </div> -->
      <div class="content">
        <div class="content-box">
          <!-- <p class="title">上传文档</p> -->
          <a-upload-dragger name="file" :file-list="fileList" ref="file" :accept="accept" :multiple="true" :action="uploadUrl"
            @change="handleChange">
            <p class="ant-upload-drag-icon">
              <a-icon type="inbox" />
            </p>
            <p class="ant-upload-text">
              将文件拖拽至此区域或 <span style="color: #4e7cff;">选择文件上传</span>
            </p>
            <p class="ant-upload-hint">
              支持格式: DOCX、DOC、XLS、XLSX、PDF、PPT、PPTX
            </p>
          </a-upload-dragger>
        </div>
        <div class="right">
          <div class="file-head">
            <img src="../../assets/system.png" class="icon-file" />
            <p class="file-title">文档内容</p>
          </div>
          <div class="file-box">
            <template v-if="fileList.length">
              <div class="file-item" v-for="(item, index) in fileList"
                v-if="item.status == 'done' && item.response.code == 200" :key="index">
                <span class="name">{{ item.response.data[0].name }}</span>
                <div class="file-content" v-for="(item1, index1) in item.response.data[0].content" :key="index1">
                  <span class="title">{{ item1.title }}</span>
                  <span class="content">{{ item1.content }}</span>
                </div>
              </div>
            </template>
            <div class="default" v-else>
              <div class="default-box">
                <img src="../../assets/default.png" />
                <p class="default-title">暂无内容</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="footer">
        <a-button class="mr20" @click="handleClose">取消</a-button>
        <a-button type="primary" @click="handleSave">确定</a-button>
      </div> -->
    </div>
  </a-modal>
</template>

<script>
import { addDocument } from "../../api";

export default {
  data() {
    return {
      uploadUrl: `${process.env.API}/api/dataset/document/split`,
      fileList: [],
      accept: '.docx,.doc,.pdf,.ppt,.pptx,xls,.xlsx',
      confirmLoading: false,
      createAddUploadVisible: false,
    }
  },
  methods: {
    add(id) {
      this.createAddUploadVisible = true;
      this.id = id
      this.fileList = []
    },
    handleChange(info) {
      const status = info.file.status;
      this.fileList = info.fileList;
      if (status !== 'uploading') {
      }
      if (status === 'done') {
      } else if (status === 'error') {
        this.$message.error(`${info.file.name} 上传失败.`);
      }
    },
    handleSave() {
      if(!this.fileList.length) return this.$message.error('请上传知识库文档！');
      let id = this.id
      if (!id) {
        this.$message.error('该知识不存在');
        return
      }
      let totalNum = this.fileList.length;
      let num = 0
      this.fileList.forEach(async item => {
        if (item.status == 'done' && item.response.code == 200) {
          try {
            let params = {
              id,
              name: item.response.data[0].name,
              paragraphs: item.response.data[0].content
            }
            const result = await addDocument(params)
            if (result.code == 200) {
              this.$message.success(`${item.response.data[0].name} 添加成功`);
              this.$emit('uploadSave')
              this.handleClose()
            } else {
              this.$message.error(`${item.response.data[0].name} 添加失败`);
            }
          } catch (e) {
            throw e
          } finally {
            num++
            if (num == totalNum) {
              setTimeout(() => {
                // this.$router.go(-1)
                this.createAddUploadVisible = false
              })
            }
          }
        }
      })
    },
    handleClose() {
      this.createAddUploadVisible = false
      this.fileList = []

    }
  }

}
</script>

<style lang="less" scoped>

/deep/ .ant-modal {
  .ant-modal-content {
    background-color: #18181b !important;
  }
  
  .ant-modal-header {
    background-color: #18181b !important;
    border-bottom: 1px solid #27272a !important;
    .ant-modal-title {
      color: #ffffff !important;
    }
  }

  .ant-modal-footer {
    background-color: #27272a !important;
    border-top: 1px solid #27272a !important;
  }
}

/deep/ .ant-upload.ant-upload-drag {
  background-color: #27272a !important;
  border-color: #3f3f46 !important;
  
  &:hover {
    border-color: #ef4444 !important;
  }
  
  .ant-upload-text {
    color: #ffffff !important;
  }
  
  .ant-upload-hint {
    color: #a1a1aa !important;
  }
  
  .ant-upload-drag-icon {
    color: #a1a1aa !important;
  }
}

.upload {
  display: flex;
  flex-direction: column;
  height: 360px;
  overflow-y: auto;

  .head {
    background: #fafafa;
    text-align: left;
    padding: 10px 20px;
    cursor: pointer;
    margin: 0;

    .head-title {
      font-size: 16px;
      font-weight: 600;
      color: #000;
      margin-left: 10px;
    }
  }

  &>.content {
    flex: 1;
    width: 100%;
    height: 0;
    margin: 0 auto;
    background: #18181b;
    display: flex;
    align-items: flex-start;

    .right {
      width: 400px;
      margin-left: 20px;
      display: flex;
      flex-direction: column;
      height: 100%;

      .file-head {
        border-bottom: 1px solid #27272a;
        display: flex;

        .icon-file {
          width: 23px;
          height: 23px;
          margin-right: 6px;
        }

        .file-title {
          font-family: PingFang SC, PingFang SC;
          font-weight: 500;
          font-size: 20px;
          color: #ffffff;
          margin-bottom: 12px;
          line-height: 23px;
          text-align: left;
          font-style: normal;
        }
      }

      // & > .title {
      //   text-align: left;
      //   font-size: 16px;
      //   color: #000;
      //   margin-bottom: 16px;
      //   position: relative;
      //   padding-left: 14px;

      //   &::before {
      //     content: "";
      //     position: absolute;
      //     top: 2px;
      //     height: 20px;
      //     left: 0;
      //     bottom: 0;
      //     border-right: 3px solid #1890ff;
      //     transform: scaleY(1);
      //     opacity: 1;
      //     transition: transform .15s cubic-bezier(.645, .045, .355, 1), opacity .15s cubic-bezier(.645, .045, .355, 1);
      //   }
      // }

      .file-box {
        flex: 1;
        overflow-y: auto;
        padding-right: 10px;
        position: relative;
        margin-top: 16px;
        border: 1px solid #27272a;

        &::-webkit-scrollbar {
          width: 8px;
        }

        &::-webkit-scrollbar-thumb {
          background: #3f3f46; // 滑块颜色
          border-radius: 5px; // 滑块圆角
        }

        .file-item {
          padding: 10px;
          margin-bottom: 15px;
          border-radius: 5px;

          .name {
            color: #ffffff;
            font-weight: bold;
            font-size: 18px;
          }

          .file-content {
            color: #a1a1aa;
            padding: 10px;
            display: flex;
            flex-direction: column;
            text-indent: 2em;

            &+.file-content {
              border-top: 1px dashed #27272a;
            }
            .content {
              font-size: 14px;
            }
          }
        }
      }
    }
    .content-box {
      flex: 1;
    }
  }

  .footer {
    padding: 10px 20px;
    text-align: right;
    background: #fafafa;

    .mr20 {
      margin-right: 20px;

    }
  }
}
.default {
  position: relative;
  width: 100%;
  height: 100%;
  flex: 1;

  .default-box {
    position: absolute;
    top: 50%;
    /* 使图片顶部在容器中垂直居中 */
    left: 50%;
    /* 使图片左侧在容器中水平居中 */
    transform: translate(-50%, -50%);
    text-align: center;
    img{
      width: 120px;
      height: 65px;
      margin-bottom: 2px;
    }
    .default-title{
      font-family: PingFang HK, PingFang HK;
      font-weight: 400;
      font-size: 12px;
      color: #a1a1aa;
      line-height: 20px;
      text-align: left;
      font-style: normal;
      text-align: center;
      margin: 0;
    }
  }
}


//删除pop-up样式
:deep(.ant-popover) {  
  .ant-popover-content {
    .ant-popover-inner {
      background-color: #18181b !important;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
      
      .ant-popover-inner-content {
        color: #ffffff !important;
      }

      .ant-popover-message {
        color: #ffffff !important;
        
        .anticon {
          color: #ef4444 !important;
        }
      }

      .ant-popover-buttons {
        .ant-btn {
          &.ant-btn-default {
            background: #27272a !important;
            border-color: #3f3f46 !important;
            color: #ffffff !important;
            
            &:hover {
              background: #3f3f46 !important;
            }
          }
          
          &.ant-btn-primary {
            background: #ef4444 !important;
            border-color: #ef4444 !important;
            
            &:hover {
              background: #dc2626 !important;
            }
          }
        }
      }
    }

    .ant-popover-arrow {
      &::after {
        background-color: #18181b !important;
        box-shadow: none !important;
      }
    }
  }
}


</style>



