<template>
  <div class="panel-tab__content">
    <div class="panel-tab__content--title">
      <span><a-icon type="appstore" />消息列表</span>
      <a-button size="small" type="primary" icon="plus" @click="openModel('message')">创建新消息</a-button>
    </div>
    <a-table bordered size="small" :pagination="false" :columns="columns" :data-source="messageList"/>
    <!-- <el-table :data="messageList" size="mini" border>
      <el-table-column type="index" label="序号" width="60px" />
      <el-table-column label="消息ID" prop="id" max-width="300px" show-overflow-tooltip />
      <el-table-column label="消息名称" prop="name" max-width="300px" show-overflow-tooltip />
    </el-table> -->
    <div class="panel-tab__content--title" style="padding-top: 8px; margin-top: 8px; border-top: 1px solid #eeeeee">
      <span><a-icon type="appstore" />信号列表</span>
      <a-button size="small" type="primary" icon="plus" @click="openModel('signal')">创建新信号</a-button>
    </div>
    <a-table bordered size="small" :pagination="false" :columns="signacolumns" :data-source="signalList"/>
    <!-- <el-table :data="signalList" size="mini" border>
      <el-table-column type="index" label="序号" width="60px" />
      <el-table-column label="信号ID" prop="id" max-width="300px" show-overflow-tooltip />
      <el-table-column label="信号名称" prop="name" max-width="300px" show-overflow-tooltip />
    </el-table> -->

    <a-modal class="bpmn-design-modal" :visible.sync="modelVisible" :title="modelConfig.title" width="400px" @cancel="() => this.modelVisible = false" destroyOnClose >
      <a-form-model :model="modelObjectForm" :label-col="labelCol" :wrapper-col="wrapperCol" @submit.native.prevent>
        <a-form-model-item :label="modelConfig.idLabel">
          <a-input v-model="modelObjectForm.id" clearable />
        </a-form-model-item>
        <a-form-model-item :label="modelConfig.nameLabel">
          <a-input v-model="modelObjectForm.name" clearable />
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button size="small" @click="modelVisible = false">取 消</a-button>
        <a-button size="small" type="primary" @click="addNewObject">保 存</a-button>
      </template>
    </a-modal>
  </div>
</template>
<script>
export default {
  name: 'SignalAndMassage',
  data() {
    return {
      signalList: [],
      messageList: [],
      modelVisible: false,
      modelType: '',
      modelObjectForm: {},
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      columns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function(text, r, index) {
            return parseInt(index) + 1
          }
        },
        { title: '消息ID', dataIndex: 'id', key: 'id' },
        { title: '消息名称', dataIndex: 'name', key: 'name' }
      ],
      signacolumns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function(text, r, index) {
            return parseInt(index) + 1
          }
        },
        { title: '信号ID', dataIndex: 'id', key: 'id' },
        { title: '信号名称', dataIndex: 'name', key: 'name' }
      ]
    }
  },
  computed: {
    modelConfig() {
      if (this.modelType === 'message') {
        return { title: '创建消息', idLabel: '消息ID', nameLabel: '消息名称' }
      } else {
        return { title: '创建信号', idLabel: '信号ID', nameLabel: '信号名称' }
      }
    }
  },
  mounted() {
    this.initDataList()
  },
  methods: {
    initDataList() {
      this.rootElements = window.bpmnInstances.modeler.getDefinitions().rootElements
      this.messageIdMap = {}
      this.signalIdMap = {}
      this.messageList = []
      this.signalList = []
      this.rootElements.forEach(el => {
        if (el.$type === 'bpmn:Message') {
          this.messageIdMap[el.id] = true
          this.messageList.push({ ...el })
        }
        if (el.$type === 'bpmn:Signal') {
          this.signalIdMap[el.id] = true
          this.signalList.push({ ...el })
        }
      })
    },
    openModel(type) {
      this.modelType = type
      this.modelObjectForm = {}
      this.modelVisible = true
    },
    addNewObject() {
      if (this.modelType === 'message') {
        if (this.messageIdMap[this.modelObjectForm.id]) {
          return this.$message.error('该消息已存在，请修改id后重新保存')
        }
        const messageRef = window.bpmnInstances.moddle.create('bpmn:Message', this.modelObjectForm)
        this.rootElements.push(messageRef)
      } else {
        if (this.signalIdMap[this.modelObjectForm.id]) {
          return this.$message.error('该信号已存在，请修改id后重新保存')
        }
        const signalRef = window.bpmnInstances.moddle.create('bpmn:Signal', this.modelObjectForm)
        this.rootElements.push(signalRef)
      }
      this.modelVisible = false
      this.initDataList()
    }
  }
}
</script>
