<template>
  <el-form ref="form" :model="form" label-width="120px">
    <el-form-item label="新闻名称">
      <el-input v-model="form.name" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="新闻标题">
      <el-input v-model="form.title" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="发布时间">
      <el-input v-model="form.time" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="新闻描述">
      <el-input
        v-model="form.describe"
        size="mini"
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4}"
      ></el-input>
    </el-form-item>
    <el-form-item label="顺序号">
      <el-input v-model="form.sort" type="number" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="阅读数">
      <el-input v-model="form.wacth" type="number" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="新闻图片">
      <el-upload
        class="avatar-uploader"
        :action="uploadAction"
        :show-file-list="false"
        :on-success="onUploadSuccess"
        :before-upload="onBeforeUpload"
      >
        <img v-if="form.icon" :src="form.icon" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <p class="zy">注意：图标比例一定要1:1</p>
    </el-form-item>
    <el-form-item label="新闻内容">
      <div id="bar" class="bar"></div>
      <div id="editor" class="editor"></div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save" size="mini">保存</el-button>
      <el-button @click="goBack" size="mini">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
var E = require("wangeditor");
export default {
  data() {
    return {
      uploadAction: process.env.SERVER_PATH + "upload",
      form: {
        id: 0,
        name: "",
        title: "",
        time: "",
        describe: "",
        sort: 0,
        wacth: 0,
        icon: "",
        status: false,
        recommend: false
      },
      html: "",
      editor: null
    };
  },
  watch: {
    html(newVal) {
      if (this.editor) {
        this.editor.txt.html(newVal);
      }
    }
  },
  methods: {
    onBeforeUpload: function(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";

      if (!isJPG && !isPNG) {
        this.$message.error("上传新闻图片只能是 JPG/PNG 格式!");
        return false;
      } else {
        return true;
      }
    },
    onUploadSuccess: function(res, file) {
      if (res.code != 0) {
        this.$message.error(
          "上传图片失败 原因:" + res.msg + "(" + res.code + ")"
        );
        return;
      } else {
        this.form.icon = res.obj;
      }
    },
    save: function() {
      let base = {
        id: this.form.id,
        name: this.form.name,
        title: this.form.title,
        icon: this.form.icon,
        time: this.form.time,
        describe: this.form.describe,
        sort: this.form.sort,
        wacth: this.form.wacth,
        html: this.html
      };
      let that = this;
      this.$http.editNewBase(base, this).then(data => {
        if (data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1000,
            onClose: function() {
              that.$router.push("/newsManager");
            }
          });
        }
      });
    },
    goBack: function() {
      this.$router.back();
    }
  },
  mounted: function() {
    var id = this.$route.query.id;
    var that = this;
    this.form.id = id;
    var editor = new E("#bar", "#editor");
    editor.customConfig.uploadImgShowBase64 = true;
    //editor.customConfig.uploadImgServer = process.env.SERVER_PATH + 'editupload'
    //editor.customConfig.uploadFileName = 'file'
    editor.customConfig.onchange = function(html) {
      that.html = html;
    };
    editor.customConfig.colors = [
        '#00000000',
        '#000000',
        '#eeece0',
        '#1c487f',
        '#4d80bf',
        '#c24f4a',
        '#8baa4a',
        '#7b5ba1',
        '#46acc8',
        '#f9963b',
        '#ffffff'
    ];
    editor.create();
    this.editor = editor;
    console.log(id);
    if (id === 0) {
      return;
    }
    this.$http.getOneNew({ id: id }, this).then(res => {
      if (res.code === 0) {
        var base = res.obj;
        this.form.name = base.name;
        this.form.title = base.title;
        this.form.icon = base.icon;
        this.form.time = base.time;
        this.form.describe = base.describe;
        this.form.sort = base.sort;
        this.form.wacth = base.wacth;
        this.html = base.html;
      }
    });
  }
};
</script>


<style scoped>
.w-e-text-container,
.w-e-toolbar {
  border: 1px solid #dcdfe6;
}
.w-e-toolbar {
  border-bottom: none;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader img {
  height: 100%;
  width: 100%;
}
.el-form-item {
  margin-bottom: 0px;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.zy {
  color: red;
}
</style>