<template>
  <div class="app-container">
    <!-- 列表 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      cache-key="systemRoleTable5"
    >
      <template v-slot:name="{ row }">
        <el-button type="text" @click="handleBpmnDetail(row)">
          <span>{{ row.name }}</span>
        </el-button>
      </template>
      <template v-slot:version="{ row }">
        <el-tag size="medium" v-if="row">v{{ row.version }}</el-tag>
        <el-tag size="medium" type="warning" v-else>未部署</el-tag>
      </template>
      <template v-slot:suspensionState="{ row }">
        <el-tag type="success" v-if="row.suspensionState === 1">激活</el-tag>
        <el-tag type="warning" v-if="row.suspensionState === 2">挂起</el-tag>
      </template>
      <template v-slot:action="{ row }">
          <el-button size="mini" type="text" icon="el-icon-s-custom" @click="handleAssignRule(row)" >分配规则</el-button>
      </template>

    </ele-pro-table>

    <!-- 流程模型图的预览 -->
    <el-dialog title="流程图" :visible.sync="showBpmnOpen" width="80%" append-to-body>
      <my-process-viewer key="designer" v-model="bpmnXML" v-bind="bpmnControlForm" />
    </el-dialog>

    <!-- ========== 流程任务分配规则 ========== -->
    <taskAssignRuleDialog ref="taskAssignRuleDialog" />
  </div>
</template>

<script>
import {getProcessDefinitionBpmnXML, getProcessDefinitionPage} from "@/api/bpm/definition";
import dictMixins from '@/mixins/dictMixins';
import taskAssignRuleDialog from "../taskAssignRule/taskAssignRuleDialog";

export default {
  name: "BpmProcessDefinition",
  components: {
    taskAssignRuleDialog
  },
  mixins: [dictMixins],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      // BPMN 数据
      showBpmnOpen: false,
      bpmnXML: null,
      bpmnControlForm: {
        prefix: "flowable"
      },

      columns: [
        {
          type: 'index',
          label: '序号',
          width: 50,
          align: 'center',
          fixed: 'left'
        },
        {
          prop: 'id',
          label: '定义编号',
          align: 'center',
          width: 150,
          showOverflowTooltip: true
        },
        {
          prop: 'name',
          label: '定义名称',
          align: 'center',
          width: 150,
          slot:"name",
          showOverflowTooltip: true
        },
        {
          prop: 'formCustomCreatePath',
          label: '表单信息',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'version',
          label: '流程版本',
          align: 'center',
          width: 80,
          slot: "version",
          showOverflowTooltip: true
        },
        {
          prop: 'suspensionState',
          label: '激活状态',
          align: 'center',
          width: 80,
          slot: 'suspensionState',
          showOverflowTooltip: true
        },
        {
          prop: 'deploymentTime',
          label: '部署时间',
          align: 'center',
          width: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'description',
          label: '定义描述',
          align: 'center',
          width: 160,
          showOverflowTooltip: true
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 450,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],

    };
  },
  created() {
    const key = this.$route.query && this.$route.query.key
    if (key) {
      this.queryParams['key'] = key
    }
  },
  methods: {
    async datasource ({ page, limit }) {
      return await getProcessDefinitionPage({
        pageNo: page,
        pageSize: limit,
      });
    },
    /** 流程图的详情按钮操作 */
    handleBpmnDetail(row) {
      getProcessDefinitionBpmnXML(row.id).then(data => {
        this.bpmnXML = data
        // 弹窗打开
        this.showBpmnOpen = true
      })
    },
    /** 处理任务分配规则列表的按钮操作 */
    handleAssignRule(row) {
      this.$refs['taskAssignRuleDialog'].initProcessDefinition(row.id);
    },
  }
};
</script>

<style lang="scss">
.my-process-designer {
  height: calc(100vh - 200px);
}
</style>
