<template>
  <div class="ledge_box">
    <!-- 搜索创建dom块 -->
    <div class="data-search">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <!--  <el-form-item label="角色名称:">
          <el-input v-model="queryForm.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色状态:">
          <el-select
            v-model="queryForm.status"
            class="m-2"
            placeholder="请选择角色状态"
            size="large"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <!--  <el-button type="primary" @click="onSubmit">
            <svg-icon icon-class="search" /> 搜索</el-button> -->
          <el-button type="success" @click="onCreate">
            <svg-icon icon-class="edit" />创建知识库</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 卡片模块 -->
    <div class="data-list" v-loading="loading" element-loading-text="加载中...">
      <el-row>
        <el-col
          :span="5"
          v-for="(item, index) in buckList"
          :key="index"
          :offset="index > 0 && index % 4 !== 0 ? 1 : 0"
          class="card-col" 
        >
          <el-card :body-style="{ padding: '0px' }" shadow="never" >
            <div class="card_box">
              <div class="box_content" @click="getInfo(item)">
                <div class="header">
                  <div class="header_left">
                    <!-- <svg-icon
                      icon-class="header"
                      style="width: 34px; height: 34px"
                    /> -->
                  </div>
                  <el-popover
        placement="bottom"
        trigger="click"
        popper-class="custom-popover"
      >
        <div>
          <el-button
            type="success"
            icon="el-icon-edit"
            @click.stop="editInfo(item)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click.stop="delect(item)"
          >删除</el-button>
        </div>
        <div
          class="header_right"
          slot="reference"
          @click.stop
        >
          <svg-icon
            icon-class="sand"
            style="width: 24px; height: 30px"
          />
        </div>
      </el-popover>
                </div>
                <div class="content">
                  <p style="margin: 0">{{ item.name }}</p>
                  <div class="content_info">{{item.description}}</div><!-- item.description -->
                  <div class="content_file">
                    <div class="file">
                      <svg-icon
                      icon-class="file"
                        style="width: 36px; height: 36px; "
                    />
                    <p>WORD</p>
                    <p>{{ item.word_count }}</p>
                    </div>
                    <div class="file">
                      <svg-icon
                      icon-class="file"
                     style="width: 36px; height: 36px; "
                    />
                    <p>EXCEL</p>
                    <p>{{ item.excel_count }}</p>
                    </div>
                    <div class="file">
                      <svg-icon
                      icon-class="file"
                      style="width: 36px; height: 36px; "
                    />
                    <p>PDF</p>
                    <p>{{ item.pdf_count }}</p>
                    </div>
                  </div>
                </div>
                <div class="footer">
                  <div class="file">
                    <!-- <svg-icon
                      icon-class="word"
                      style="width: 18px; height: 18px; filter: grayscale(100%)"
                    />
                    {{ item.doc_num }} -->
                  </div>
                  <div class="date">
                    <svg-icon
                      icon-class="date"
                      style="width: 12px; height: 12px; filter: grayscale(100%)"
                    />
                    {{ item.create_time }}
                  </div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <!-- 创建弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="creatForm" ref="creatRef" :rules="creatRules">
        <el-form-item label="知识库名称:" prop="bucket_name">
          <el-input
            v-model="creatForm.bucket_name"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="description">
          <el-input
            show-word-limit
            type="textarea"
            :rows="5"
            maxlength="250"
            v-model="creatForm.description"
            placeholder="请输入描述内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitBuck">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
import {
  listBuckets,
  createBucket,
  deleteBucket,
  updateKbm,
} from "@/api/knowledge";
import { mapGetters } from "vuex";

export default {
  name: "Dashboard",
  data() {
    // 自定义验证函数
    const validateBucketName = (rule, value, callback) => {
      const regex = /^[a-z0-9-]{3,63}$/;
      if (!regex.test(value)) {
        callback(
          new Error(
            "名称必须在 3-63 个字符之间，只能包含小写字母、数字和连字符"
          )
        );
      } else {
        callback();
      }
    };
    return {
      loading: true,
      queryForm: {},
      buckList: [],
      visible: false,
      dialogVisible: false,
      dialogTitle: "创建知识库",
      creatForm: {
        id: null,
        bucket_name: "",
        description: "",
        user_id: null,
      },//知识库名称
      creatRules: {
        bucket_name: [
          { required: true, message: "请输入知识库名称", trigger: "blur" },
          /*  { validator: validateBucketName, trigger: "blur" }, */
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["roles"]),
  },
  created() {},
  mounted() {
    this.onSubmit();
  },
  methods: {
    editInfo(item) {
      this.dialogTitle = "编辑知识库";
      this.creatForm = {
        id: item.id,
        bucket_name: item.name,
        description: item.description,
        user_id: this.$store.state.user.id,
      };
      this.dialogVisible = true;
    },
    /* 创建知识库 */
    onCreate(e) {
      this.dialogTitle = "创建知识库";
      this.creatForm = {
        id: null,
        bucket_name: "",
        description: "",
        user_id: this.$store.state.user.id,
      };
      this.dialogVisible = true;
    },
    /* 关闭弹窗 */
    handleClose() {
      this.$refs.creatRef.resetFields();
      this.dialogVisible = false;
    },
    onClose(e) {
      this.dialogVisible = false;
      this.onSubmit();
    },
    //搜索
    onSubmit() {
      this.loading = true;
      listBuckets({ user_id: this.$store.state.user.id })
        .then((res) => {
          if (res.status !== 200) return;
          res.data.map((el) => {
            el.visible = false;
          });
          this.buckList = res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /* 删除 */
    delect(val) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteBucket({ bucket_id: val.id }).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.onSubmit();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /* 关闭弹窗 */
  /*   handleClose(done) {
      this.creatForm = {};
      done();
    }, */
    /* 创建知识库 */
    submitBuck() {
      this.$refs.creatRef.validate((valid) => {
        if (valid) {
          const action = this.creatForm.id ? updateKbm : createBucket;
          const successMessage = this.creatForm.id ? "修改成功" : "创建成功";
          this.creatForm.id ? this.creatForm.name=this.creatForm.bucket_name :this.creatForm.bucket_name=this.creatForm.bucket_name ;
          action(this.creatForm).then((res) => {
            if (res.status !== 200) return;
            this.dialogVisible = false;
            this.$message.success(successMessage);
            this.onSubmit();
            this.$refs.creatRef.resetFields();
          });
        } else {
          this.$message.error("请填写必填项");
        }
      });
    },
    /* 跳转到详细页面 */
    getInfo(item) {
      console.log(item.id);
      this.$router.push({
        path: "/knowledge/category/knowledgeSet",
        query: { id: item.id,name:item.name },
      });
    },
  },
};
</script>
  <style lang="scss" scoped>

.el-card{
   /* 鼠标悬停时的更强烈阴影 */
   box-shadow: 0 6px 6px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
}

.el-row {
  width:95%;
  margin-left: 30px;
}
::v-deep .el-popover.custom-popover {
  z-index: 3000 !important;
  
  .el-button {
    display: block;
    margin: 5px 0;
    width: 100%;
  }
  ::v-deep .el-card {
  
  overflow: visible !important; // 确保popover不被截断
}
::v-deep .el-col-offset-1 {
  margin-left: 2%;
}
::v-deep .custom-popover {
  z-index: 3000 !important;
}
}
@import "./index.scss";
</style>
  