<template>
  <div class="search-table-box aidex-table" v-loading="loading" element-loading-text="数据处理中" style="height: calc(100vh - 105px);">
    <div class="right-doc-top">
      <div class="right-doc-top-button-del">
        <el-button type="primary" size="small" @click="openDocumentAdd">
          <i class="el-icon-circle-plus-outline"/>
          添加图片
        </el-button>
      </div>
    </div>
    <div class="image-list">
      <el-row :gutter="12">
        <!-- 图片列表 -->
        <div v-if="imagesDocList.length===0" class="no-data">
          <el-empty :image-size="50"/>
        </div>
        <el-col :span="5" v-for="(item, index) in imagesDocList" style="margin-bottom: 10px">
          <el-card :body-style="{ padding: '0px' }" shadow="hover">
            <image-preview :src="item.url" class="image" alt=""/>
            <div class="card-text">
              <span>{{ item.name }}</span>
              <el-tooltip placement="top">
                <div slot="content" style="width: 200px;background-color: #2b2f3a;line-height: 16px">{{ item.caption }}</div>
                <p>{{ item.caption }}</p>
              </el-tooltip>

              <div class="bottom">
                <time class="time">{{ unixTimestampToReadable(item.create_time) }}</time>
                <div class="bottom-right">
                  <div class="button-edit">
                    <el-button icon="el-icon-edit" type="text" @click="handleDocumentUpdate(item)"/>
                  </div>
                  <div class="button-del">
                    <el-button icon="el-icon-delete" type="text" @click="handleDocumentDelete(item)"/>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>

      </el-row>
    </div>

    <!-- 添加-知识库图片 -->
    <el-dialog title="创建知识库图片" :visible.sync="openAdd" width="820px" append-to-body>
      <div v-loading="updateLoading" element-loading-text="数据处理中">
        <el-form ref="addForm" :model="addForm" :rules="addRules" label-position="top" label-width="120px">
          <el-form-item label="上传图片（自动标注描述）" prop="document_list">
            <image-upload v-model="addForm.document_list" :limit="10" :fileSize="1"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addSubmitForm">确 定</el-button>
          <el-button @click="addCancel">取 消</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 修改-更新描述 -->
    <el-dialog title="更新描述" :visible.sync="openUpdateCaption" width="500px" append-to-body>
      <div v-loading="updateLoading" element-loading-text="数据处理中">
        <el-form ref="updateCaptionForm" :model="updateCaptionForm" :rules="updateCaptionRules" label-position="top" label-width="120px">
          <el-form-item label="图片描述信息" prop="caption">
            <el-input
              type="textarea"
              v-model="updateCaptionForm.caption"
              placeholder="请输入图片描述信息"
              :rows="5"
              maxlength="2000"
              show-word-limit/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="upCaptionSubmitForm">确 定</el-button>
          <el-button @click="upCaptionCancel">取 消</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addImage, deleteDocument, listKnowledgeDocImages, updateImageCaption} from "@/api/business/aimodel/knowledgeDocument"

export default {
  name: "imagesDoc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 图片列表
      imagesDocList: [],
      oldDatasetId: null,
      updateLoading: false,

      //创建
      openAdd: false,
      addForm: {},
      addRules: {
        document_list: [
          {required: true, message: "请上传图片", trigger: "blur"},
        ]
      },

      //重命名
      openUpdateCaption: false,
      updateCaptionForm: {},
      updateCaptionRules: {
        caption: [
          {required: true, message: "请输入图片描述信息", trigger: "blur"},
          {min: 1, max: 100, message: "长度在 1 到 2000 个字符", trigger: "blur"}
        ]
      },

    };
  },
  methods: {
    selectedDatasetDoc(datasetId) {
      this.oldDatasetId = datasetId;
      this.getImagesDocList(datasetId);
    },
    /** 查询知识库图片列表 */
    getImagesDocList(datasetId) {
      listKnowledgeDocImages({"dataset_id": datasetId}).then(response => {
        this.imagesDocList = response.data;
        this.loading = false;
      });
    },
    //格式化时间戳
    unixTimestampToReadable(timestamp) {
      const date = new Date(timestamp * 1000);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },


    /** 创建-表单重置 **/
    addReset() {
      this.addForm = {
        dataset_id: null,
        document_list: null,
      };
      this.resetForm("addForm");
    },
    /** 创建-打开窗口 */
    openDocumentAdd() {
      this.addReset();
      this.addForm.dataset_id = this.oldDatasetId;
      this.openAdd = true;
    },
    /** 创建-取消按钮 */
    addCancel() {
      this.openAdd = false;
      this.addReset();
    },
    /** 创建-修改提交 */
    addSubmitForm() {
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          this.updateLoading = true;
          addImage(this.addForm).then(response => {
            this.$modal.msgSuccess("创建成功");
            this.openAdd = false;
            this.updateLoading = false;
            this.selectedDatasetDoc(this.oldDatasetId);
          });
        }
      });
    },

    /** 删除知识库-图片 **/
    handleDocumentDelete(imageInfo) {
      this.$modal.confirm('是否确认删除"' + imageInfo.name + '"知识库图片？').then(function () {
        return deleteDocument({'document_id': imageInfo.document_id});
      }).then(() => {
        this.selectedDatasetDoc(this.oldDatasetId);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    /** 修改描述-重置 */
    upCaptionReset() {
      this.updateCaptionForm = {
        dataset_id: null,
        document_id: null,
        caption: null,
      };
      this.resetForm("updateCaptionForm");
    },
    /** 修改描述-打开窗口 */
    handleDocumentUpdate(knowledgeDocInfo) {
      this.upCaptionReset();
      this.updateCaptionForm.dataset_id = this.oldDatasetId;
      this.updateCaptionForm.document_id = knowledgeDocInfo.document_id;
      this.updateCaptionForm.caption = knowledgeDocInfo.caption;
      this.openUpdateCaption = true;
    },
    /** 修改描述-修改提交 */
    upCaptionSubmitForm() {
      this.$refs["updateCaptionForm"].validate(valid => {
        if (valid) {
          this.updateLoading = true;
          updateImageCaption(this.updateCaptionForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openUpdateCaption = false;
            this.updateLoading = false;
            this.selectedDatasetDoc(this.oldDatasetId);
          });
        }
      });
    },
    /** 修改描述-取消按钮 */
    upCaptionCancel() {
      this.openUpdateCaption = false;
      this.upCaptionReset();
    },


  }
};
</script>

<style scoped>

.right-doc-top {
  height: 45px;
  border-bottom: 1px solid #52649a21;
  display: flex;
  align-items: center;
  padding: 0 10px;
  color: #0f1528d1;
  font-size: 14px;
  margin-bottom: 10px;
}

.image {
  width: 100%;
  height: 150px;
  display: block;
}

.no-data {
  height: calc(100vh - 105px) !important;
}

.image-list {
  margin-top: 10px;
  overflow-y: auto;
  overflow-x: hidden;
  height: calc(100vh - 155px) !important;
}

.card-text {
  padding: 14px;
  font-size: 14px;

  span {
    color: #1d1c23;
    font-weight: 600;
    line-height: 20px;
  }

  p {
    font-size: 12px;
    color: rgba(29, 28, 35, 0.6);
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    display: -webkit-box;
    overflow: hidden;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
    align-items: center;
    display: flex;
    height: 24px;
    justify-content: space-between;

    .time {
      font-size: 12px;
      color: #1c1d2359;
      line-height: 16px;
    }

    .bottom-right {
      display: flex;
      align-items: center;

      .button-del {
        margin-left: 10px;
      }
    }
  }
}

</style>
