
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align:left;"
    :width="'800px'"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formModel.title" placeholder="请输入" style="width:100%"></el-input>
        </el-form-item>
        <el-form-item label="发送单位" prop="noticeCompany">
          <el-select
            v-model="formModel.noticeCompany"
            filterable
            multiple
            placeholder="请选择"
            style="width:100% "
          >
            <el-option 
              v-for="company in companyResult"
              :key="company.id"
              :label="company.name"
              :value="company.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <mce-editor ref="editor"
          uploadName="upfile" v-model="formModel.content"
          :url="uploadUrl" :config="editorConfig"></mce-editor>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="formModel.status">
            <el-radio :label="true">已审核</el-radio>
            <el-radio :label="false">未审核</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import messageNoticeApi from "@/api/base/messageNotice";
import companyInfoApi from "@/api/base/companyInfo";
import { getToken } from '@/utils/auth'
import MceEditor from "@/components/Tinymce";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      ruleValidate: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        noticeCompany: [{ required: true, message: "发送单位不能为空", trigger: "blur" }]
      },
      showDialog: true,
      loading: false,
      submitting: false,
      formModel: {},
      companyResult: "",
      editorConfig: {
        height: 300,
        width: 660
      },
      uploadUrl: Constant.serverUrl + "/tinymce/upload?token=" + getToken()
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate(valid => {
        if (valid) {
          (function() {
            var id = self.formModel.id;

            self.formModel.content = self.$refs.editor.getContent();

            //子单位
            var noticeCompany = self.formModel.noticeCompany.join(",");
            self.formModel.noticeCompany = noticeCompany;

            if (id == null || id.length == 0) {
              return messageNoticeApi.add(self.formModel);
            } else {
              return messageNoticeApi.update(self.formModel);
            }
          })().then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              self.$emit("close", false);
            }
          });
        }
      });
    }
  },
  mounted: function() {
    console.log("detail mounted");

    var self = this;

    companyInfoApi.list().then(function(response) {
      var jsonData = response.data;
      if (jsonData.result) {
        self.companyResult = jsonData.data;
      }
    });
    (function() {
      if (self.businessKey.length == 0) {
        return messageNoticeApi.create();
      } else {
        return messageNoticeApi.edit(self.businessKey);
      }
    })()
      .then(response => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;

          if(self.formModel.content!=null){
            self.$refs.editor.setContent(self.formModel.content);
          }

          if (self.businessKey.length == 0) {
            self.formModel.status = false;
          }
          
          var noticeCompany = self.formModel.noticeCompany;

          if (noticeCompany != null) {
            self.formModel.noticeCompany = noticeCompany.split(",");
          }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch(error => {
        self.$message.error(error + "");
      });
  },
  components: {
    "mce-editor": MceEditor
  }
};
</script>
<style>
.ql-editor {
  height: 300px;
}
</style>