
<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"
    @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="type">
          <el-radio-group v-model="formModel.type">
            <el-radio-button label="1">我要用工</el-radio-button>
            <el-radio-button label="2">我有工人</el-radio-button>
          </el-radio-group>
        </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>
    </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 labourServicesApi from "@/api/base/labourServices";
import MceEditor from "@/components/Tinymce";
import { getToken } from "@/utils/auth";


export default {
  props: ["businessKey", "title"],
  components: {
    "mce-editor": MceEditor,
  },
  data() {
    return {
      formModel: {
        content:"",
      },
      ruleValidate: {
        content: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      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();

            if (id == null || id.length == 0) {
              return labourServicesApi.add(self.formModel);
            } else {
              return labourServicesApi.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 () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return labourServicesApi.create();
      } else {
        return labourServicesApi.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);
          }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>