<template>
  <div>
    <!-- 组件的内容 -->
    <!-- 网站信息 -->
    <div class="card-content1 shadow-box background-opacity">
      <el-avatar style="margin-top: 20px" class="user-avatar" :size="120" :src="webInfo.avatar"></el-avatar>
      <div class="web-name">{{webInfo.webName}}</div>
      <div class="web-info">
        <div class="blog-info-box">
          <span>博客</span>
          <span class="blog-info-num">{{ $store.getters.articleTotal }}</span>
        </div>
        <div class="blog-info-box">
          <span>分类</span>
          <span class="blog-info-num">{{ sortInfo.length }}</span>
        </div>
        <div class="blog-info-box">
          <span>访问量</span>
          <span class="blog-info-num">{{ webInfo.historyAllCount }}</span>
        </div>
      </div>
      <a class="collection-btn" @click="showTip()">
        <i class="el-icon-star-off" style="margin-right: 2px"></i>朋友圈
      </a>
    </div>

    <!-- 写博客按钮 -->
    <button class="write-blog-btn" @click="showEditor = true">
      ✍️ 写博客
    </button>

    <!-- 编辑框 -->
    <div class="blog-editor" v-if="showEditor">
      <textarea v-model="blogContent"></textarea>
      <div class="editor-btns">
        <button class="cancel-btn" @click="cancelEdit">取消</button>
        <button class="publish-btn" @click="saveBlog">发布</button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  // 组件的逻辑和配置
  data() {
    return {
      pagination: {
        current: 1,
        size: 5,
        recommendStatus: true
      },
      recommendArticles: [],
      admires: [],
      showAdmireDialog: false,
      articleSearch: "",
      showEditor: false,
      blogContent: ""
    }
  },
  computed: {
    webInfo() {
      return this.$store.state.webInfo;
    },
    sortInfo() {
      return this.$store.state.sortInfo;
    }
  },
  created() {
    this.getRecommendArticles();
    this.getAdmire();
  },
  methods: {
    selectSort(sort) {
      this.$emit("selectSort", sort);
    },
    selectArticle() {
      this.$emit("selectArticle", this.articleSearch);
    },
    showAdmire() {
      if (this.$common.isEmpty(this.$store.state.currentUser)) {
        this.$message({
          message: "请先登录！",
          type: "error"
        });
        return;
      }

      this.showAdmireDialog = true;
    },
    getAdmire() {
      this.$http.get(this.$constant.baseURL + "/webInfo/getAdmire")
        .then((res) => {
          if (!this.$common.isEmpty(res.data)) {
            this.admires = res.data;
          }
        })
        .catch((error) => {
          this.$message({
            message: error.message,
            type: "error"
          });
        });
    },
    getRecommendArticles() {
      this.$http.post(this.$constant.baseURL + "/article/listArticle", this.pagination)
        .then((res) => {
          if (!this.$common.isEmpty(res.data)) {
            this.recommendArticles = res.data.records;
          }
        })
        .catch((error) => {
          this.$message({
            message: error.message,
            type: "error"
          });
        });
    },
    showTip() {
      this.$router.push({path: '/weiYan'});
    },
    cancelEdit() {
      // 取消编辑
      this.showEditor = false;
      this.blogContent = "";
    },
    saveBlog() {
      // 发布博客
      // console.log(this.blogContent);
      // this.showEditor = false;
      // this.blogContent = "";
      // let blog = {content:this.blogContent};
      const articleVO = {
        user_id:"sara",
        article_content: this.blogContent ,// 获取博客内容
        // 其他博客相关数据...
        // sort_id:1,
      };
      console.log(articleVO)
      this.$http.post('/article/saveArticle', articleVO) // 发送POST请求，指定保存博客的接口地址
        .then(response => {
          // 请求成功处理逻辑
          console.log('博客保存成功！');
        })
        .catch(error => {
          // 请求失败处理逻辑
          console.error('博客保存失败：', error);
        });

    }
  }
}
</script>

<style scoped>
/* 组件的样式 */
.card-content1 {
  background: linear-gradient(-45deg, #e8d8b9, #eccec5, #a3e9eb, #bdbdf0, #eec1ea);
  background-size: 400% 400%;
  animation: gradientBG 10s ease infinite;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 10px;
  position: relative;
  /*color: var(--white);*/
  overflow: hidden;
}

.card-content1 :not(:first-child) {
  z-index: 10;
}

.web-name {
  font-size: 30px;
  font-weight: bold;
  margin: 20px 0;
}

.web-info {
  width: 80%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}

.blog-info-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  font-size: 24px;
}

.blog-info-num {
  margin-top: 12px;
}

.collection-btn {
  position: relative;
  margin-top: 12px;
  background: var(--lightGreen);
  cursor: pointer;
  width: 65%;
  height: 35px;
  border-radius: 1rem;
  text-align: center;
  line-height: 35px;
  color: var(--white);
  overflow: hidden;
  z-index: 1;
  margin-bottom: 25px;
}

.collection-btn::before {
  background: var(--gradualRed);
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  content: "";
  transform: scaleX(0);
  transform-origin: 0;
  transition: transform 0.5s ease-out;
  transition-timing-function: cubic-bezier(0.45, 1.64, 0.47, 0.66);
  border-radius: 1rem;
  z-index: -1;
}

.collection-btn:hover::before {
  transform: scaleX(1);
}

.write-blog-btn {
  position: absolute;
  left: 470px;
  top: 197px;
  width: 160px;
  height: 63px;
  background-color: var(--gradualRed);
  color: #181313;
  border-radius: 20px;
  font-size: 24px;
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all .2s ease-in-out;
  border: 2px solid #1675b4; /* 添加边框 */
}

.write-blog-btn:hover {
  transform: scale(1.1);
  border: 2px solid #053246; /* 鼠标悬停时修改边框颜色 */
}



.write-blog-btn:hover {
  transform: scale(1.1); /* 鼠标悬停时缩放 */
}



.blog-editor {
  margin: 0 auto;
  align-self: center;
  width: 80%;
  height: 300px;
  background-color: #ba94dc;
  border-radius: 20px;
  margin-top: 20px;
  padding: 10px;
  box-sizing: border-box;
}

.blog-editor textarea {
  margin: 0 auto;
  width: 100%;
  height: 100%;
  resize: none;
  border: none;
  outline: none;
  border-radius: 20px;
  font-size: 16px;
  font-family: "Microsoft YaHei";
}

.editor-btns {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

.cancel-btn,
.publish-btn {
  width: 100px;
  height: 30px;
  color: #fff;
  border-radius: 3px;
  cursor: pointer;
  transition: all .2s linear;
  font-size: 19px;
  font-weight: bold;
}

.cancel-btn {
  background-color: #ccc;
  margin-right: 10px;
}

.publish-btn {
  background-color: var(--gradualRed);
  color: #1d1f21;
}

.cancel-btn:hover,
.publish-btn:hover {
  transform: translateY(-2px);
}
</style>
