<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="流程模型的编号" prop="modelId">
                      <el-input v-model="formData.modelId" placeholder="请输入流程模型的编号" />
                    </el-form-item>
                    <el-form-item label="流程定义的编号" prop="processDefinitionId">
                      <el-input v-model="formData.processDefinitionId" placeholder="请输入流程定义的编号" />
                    </el-form-item>
                    <el-form-item label="流程任务定义的key" prop="taskDefinitionKey">
                      <el-input v-model="formData.taskDefinitionKey" placeholder="请输入流程任务定义的key" />
                    </el-form-item>
                    <el-form-item label="规则类型" prop="type">
                      <el-select v-model="formData.type" placeholder="请选择规则类型">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BPM_TASK_CANDIDATE_STRATEGY)"
                                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="规则值，JSON 数组" prop="options">
                      <el-input v-model="formData.options" placeholder="请输入规则值，JSON 数组" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as TaskAssignRuleApi from '@/api/bpm/task';
      export default {
    name: "TaskAssignRuleForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            modelId: undefined,
                            processDefinitionId: undefined,
                            taskDefinitionKey: undefined,
                            type: undefined,
                            options: undefined,
        },
        // 表单校验
        formRules: {
                        modelId: [{ required: true, message: '流程模型的编号不能为空', trigger: 'blur' }],
                        processDefinitionId: [{ required: true, message: '流程定义的编号不能为空', trigger: 'blur' }],
                        taskDefinitionKey: [{ required: true, message: '流程任务定义的key不能为空', trigger: 'blur' }],
                        type: [{ required: true, message: '规则类型不能为空', trigger: 'change' }],
                        options: [{ required: true, message: '规则值，JSON 数组不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await TaskAssignRuleApi.getTaskAssignRule(id);
            this.formData = res.data;
            this.title = "修改Bpm 任务规则";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增Bpm 任务规则";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await TaskAssignRuleApi.updateTaskAssignRule(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await TaskAssignRuleApi.createTaskAssignRule(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            modelId: undefined,
                            processDefinitionId: undefined,
                            taskDefinitionKey: undefined,
                            type: undefined,
                            options: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
