<template>
  <div class="app-container">
    <!-- 添加或修改流程定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px"  :disabled="disabled">
        <el-form-item label="流程编码" prop="flowCode">
          <el-input v-model="form.flowCode" placeholder="请输入流程编码" maxlength="40" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程名称" prop="flowName">
          <el-input v-model="form.flowName" placeholder="请输入流程名称" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入流程版本" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入流程类别" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="是否发布" prop="isPublish" v-if="disabled">
          <el-select v-model="form.isPublish" placeholder="请选择是否开启流程">
            <el-option
              v-for="dict in dict.type.is_publish"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="审批表单是否自定义" prop="formCustom">
          <el-radio-group v-model="form.formCustom">
            <el-radio label="N" >否</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="审批表单路径" prop="formPath">
          <el-input v-model="form.formPath" placeholder="请输入审批表单路径" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="监听器类型" prop="formPath">
          <el-select v-model="form.listenerType" multiple>
            <el-option label="任务创建" value="create"></el-option>
            <el-option label="任务开始办理" value="start"></el-option>
            <el-option label="分派监听器" value="assignment"></el-option>
            <el-option label="任务完成" value="finish"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监听器路径" prop="formPath">
          <el-input type="textarea" v-model="form.listenerPath" rows="8"></el-input>
          <el-tooltip class="item" effect="dark" content="输入监听器的路径，以@@分隔，顺序与监听器类型一致">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
        <el-button @click="cancel" v-if="disabled">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDefinition, addDefinition, updateDefinition } from "@/api/flow/definition";

export default {
  name: "Dialog",
  dicts: ['sys_yes_no', 'is_publish'],
  data() {
    return {
      // 是否禁用表单
      disabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flowCode: [
          { required: true, message: "流程编码不能为空", trigger: "blur" }
        ],
        flowName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        isPublish: [
          { required: true, message: "是否开启流程不能为空", trigger: "change" }
        ],
        formCustom: [
          { required: true, message: "请选择审批表单是否自定义", trigger: "change" }
        ],
      }
    };
  },
  methods: {
    /** 打开流程定义弹框 */
    async show(id, disabled) {
      this.reset();
      this.disabled = disabled
      if (id) {
        await getDefinition(id).then(response => {
          this.form = response.data;
          if (this.form.listenerType) {
            this.form.listenerType = this.form.listenerType.split(",")
          }
        });
      }
      this.open = true
      if (this.disabled) {
        this.title = "详情"
      } else if (id) {
        this.title = "修改"
      } else {
        this.title = "新增"
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flowCode: null,
        flowName: null,
        version: null,
        isPublish: null,
        formCustom: null,
        formPath: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.listenerType = this.form.listenerType.join(",")
          if (this.form.id != null) {
            updateDefinition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh');
            });
          } else {
            addDefinition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh');
            });
          }
        }
      });
    },
  }
};
</script>
