<template>
  <div class="doc_manage_container">
    <div class="apass_breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="/technical_support">
          技术支持
        </el-breadcrumb-item>
        <el-breadcrumb-item to="/technical_support/doc_manage">
          开发文档管理
        </el-breadcrumb-item>
        <el-breadcrumb-item> 文档编辑 </el-breadcrumb-item>
        <el-breadcrumb-item> {{ title }} </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="editpage">
      <wang-e v-model="content"></wang-e>
      <div class="up_load_box">
        <el-upload
          action="/apaas/static/image/upload"
          :on-success="getNewListOne"
          :on-remove="handleRemove"
          :data="anotherData"
          :file-list="doc_file">
          <el-button size="small" class="upload_ben">点击上传附件</el-button>
        </el-upload>
      </div>
      <div class="apaas_button">
        <el-button type="defalut" size="mini" @click="cancelAction">
          取消
        </el-button>
        <el-button type="primary" size="mini" @click="submitAction">
          保存
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import wangE from "@/components/wangE";
import helper from "@/services/helper.js";
export default {
  components: {
    wangE,
  },
  data() {
    return {
      title: "",
      content: "",
      doc_file: [],
      anotherData: {
        directory: "file",
      }
    };
  },
  computed: {
    id() {
      return parseFloat(this.$route.params.id);
    },
  },
  created() {
    if (this.id) {
      this.$http
        .get("/apaas/support/document/get", {
          params: {
            id: this.id,
          },
        })
        .then(({ data }) => {
          if (data.success === 1) {
            this.title = data.data.title;
            this.content = data.data.content;
            let arr = [];
            if (data.data.doc_file && data.data.doc_file != "") {
              arr = data.data.doc_file.split(",")
              arr.forEach(e => {
                this.doc_file.push({
                  name: helper.downloadFileFormatNew(e),
                  url: e
                })
              })
            }
          }
        });
    }
  },
  methods: {
    cancelAction() {
      this.$router.push("/technical_support/doc_manage");
    },
    submitAction() {
      let arr = [];
      if (this.doc_file && this.doc_file.length > 0 ) {
        this.doc_file.forEach(e => {
          arr.push(e.url)
        })
      }
      let doc_file = arr.join(",") || ""
      this.$http
        .put("/apaas/support/document/put", {
          id: this.id,
          content: this.content,
          doc_file: doc_file,
        })
        .then(({ data }) => {
          if (data.success === 1) {
            this.$message.success("保存成功");
            this.$router.push("/technical_support/doc_manage");
          } else {
            this.$message.error(data.errMsg || "保存失败");
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("保存失败");
        });
    },
    getNewListOne(res,file) {
      if (res.success == 1) {
        this.doc_file.push(
          {
            name: file.name,
            url: res.data
          }
        )
      }
    },
    handleRemove(file) {
      this.doc_file.forEach((item, i) => {
        if (item.url === file.url) {
          this.doc_file.splice(i, 1);
        }
      });
    }
  },
};
</script>

<style scoped>
.doc_manage_container {
  /* height: 100%; */
  padding: 0 20px 20px;
  box-sizing: border-box;
}
.main_container {
  height: calc(100% - 53px);
  background-color: #fff;
  border-radius: 10px;
}
.editpage {
  background-color: #fff;
  /* height: calc(100% - 45px); */
  /* margin-bottom: 20px; */
  box-shadow: 0px 3px 6px 0px #f4f7fc;
  border-radius: 12px;
}
.editpage .wangeditor_class {
  height: calc(100vh - 300px);
  padding: 0 20px;
  box-sizing: border-box;
}
.apaas_button {
  border-top: 1px solid #e3e5ef;
  box-sizing: border-box;
  text-align: right;
  padding: 30px 0;
}
.apaas_button .el-button + .el-button {
  margin-right: 30px;
}
.up_load_box {
  padding: 20px 60% 20px 20px;
}
.upload_ben {
  background-color: #0f2683;
  border-color: #0f2683;
  color: #fff;
}
</style>
