
<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"
    width="35%"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="流程KEY" prop="proKey" style="width:450px">
            <el-input v-model="formModel.proKey" placeholder="请输入五位ID编号" ></el-input>
        </el-form-item>
        <el-form-item label="流程名称" prop="name" style="width:450px">
            <el-input v-model="formModel.name" placeholder="请输入流程名称" ></el-input>
        </el-form-item>
        <el-form-item label="流程说明" prop="remark" style="width:450px">
            <el-input v-model="formModel.remark" placeholder="请输入流程说明" ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" style="width:450px">
          <el-switch
            v-model="formModel.status"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </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 processSetApi from "@/api/workflow/processSet";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        proKey: [
          { required: true, message: "流程KEY不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "流程说明不能为空", trigger: "blur" }
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      canQuery: true,
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children"
      },
    };
  },
  created() {
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

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

            self.loading = true;

            if (id == null || id.length == 0) {
              return processSetApi.add(self.formModel);
            } else {
              return processSetApi.update(self.formModel);
            }
          })().then(function(response) {
            self.loading = false;
            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() {
      self.loading = true;

      if (self.businessKey.length == 0) {
        return processSetApi.create();
      } else {
        return processSetApi.edit(self.businessKey);
      }
    })()
      .then(response => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
            self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch(error => {
        self.$message.error(error + "");
      });
  },
};
</script>