<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" @getNewList="getNewList" :fileArray="fileArray"></wang-e>
      <div class="apaas_button pt_max">
        <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: "",
      fileList: [],
      fileArray: []
    };
  },
  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.fileArray.push({
                  name: helper.downloadFileFormatNew(e),
                  url: e
                })
              })
            }
          }
        });
    }
  },
  methods: {
    cancelAction() {
      this.$router.push("/technical_support/doc_manage");
    },
    submitAction() {
      let arr = [];
      if (this.fileList && this.fileList.length > 0 ) {
        this.fileList.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("保存失败");
        });
    },
    getNewList(fileList) {
      this.fileList = fileList;
    },
  },
};
</script>

<style scoped>
.doc_manage_container {
  height: 100%;
  padding: 0 20px 30px;
  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(100% - 100px);
  padding: 0 20px;
  box-sizing: border-box;
}
.apaas_button {
  border-top: 1px solid #e3e5ef;
  box-sizing: border-box;
  text-align: right;
  padding-top: 30px;
}
.apaas_button .el-button + .el-button {
  margin-right: 30px;
}
.editpage /deep/ .wangeditor_class {
  height: calc(100% - 200px);
}
.pt_max {
  padding-top: 130px;
}
</style>
