<template>
  <div>
    <!--内容-->
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form v-show="searchOpen" inline onsubmit="return false" :label-width="60">
          <label class="label-sign" />
          <Form-item class="search-item" label="名称:">
            <el-input v-model="formObj.name" placeholder="请输入名称" clearable @keyup.enter.native="_formSearch" />
          </Form-item>
          <Form-item class="search-btn">
            <el-button type="primary" @click="_formSearch">
              搜索
            </el-button>
          </Form-item>
        </Form>
      </el-col>
      <!--操作-->
      <el-col :span="24">
        <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" @on-result-change="_btnClick" />
      </el-col>
      <!-- 表格 -->
      <el-col :span="24">
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          :table-height="tableHeight"
          :get-page="getPage"
          @on-result-change="_tableResultChange"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            show-overflow-tooltip
            sortable
            :prop="item.key"
            :label="item.title"
            :min-width="item.width"
            :fixed="item.fixed?item.fixed:undefined"
          >
            <template slot-scope="scope">
              <div v-if="item.state">
                <span v-if="scope.row['state'] === 1" class="green-color">激活</span>
                <span v-else class="red-color">挂起</span>
              </div>
              <div v-else>
                {{ scope.row[item.key] }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            slot="col"
            label="操作"
            align="center"
            :width="140"
            fixed="right"
          >
            <template slot-scope="scope">
              <icon-list
                :msg="scope.row.state === 1 ? iconMsg : iconMsgDis"
                :row-data="scope.row"
                @on-result-change="_iconClick"
              />
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
    <!-- 流程图 -->
    <flowBpmn ref="flowBpmnModal" />
    <!-- 部署流程 -->
    <flowImport ref="flowImportModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { flowProcess } from '../../../api'
import global from '../../../api/config'
import flowBpmn from './flowBpmn'
import flowImport from './flowImport'

export default {
  components: {
    flowBpmn,
    flowImport
  },
  data() {
    return {
      searchOpen: false,
      formObj: {
        name: ''
      },
      btn: [{ type: 'success', id: '', name: '部署流程' }],
      iconMsg: [
        { type: 'pt-xml', id: 'workflow-process-xml', name: 'XML' },
        { type: 'pt-image', id: 'workflow-process-flow', name: '流程图' },
        {
          type: 'pt-pulse',
          id: 'workflow-process-suspend',
          name: '挂起'
        },
        {
          type: 'pt-active',
          id: 'workflow-process-active',
          name: '激活',
          disabled: true
        },
        { type: 'pt-trash', id: 'workflow-process-del', name: '删除' }
      ],
      iconMsgDis: [
        { type: 'pt-xml', id: 'workflow-process-xml', name: 'XML' },
        { type: 'pt-image', id: 'workflow-process-flow', name: '流程图' },
        {
          type: 'pt-pulse',
          id: 'workflow-process-suspend',
          name: '挂起',
          disabled: true
        },
        {
          type: 'pt-active',
          id: 'workflow-process-active',
          name: '激活'
        },
        { type: 'pt-trash', id: 'workflow-process-del', name: '删除' }
      ],
      pageColumns: [
        { title: '流程编号', key: 'id', width: 430 },
        { title: '名称', key: 'name', width: 200 },
        { title: '状态', key: 'state', width: 80, state: true },
        { title: '部署 ID', key: 'deploymentId', width: 300 },
        { title: '流程 Key', key: 'key', width: 200 },
        { title: '描述', key: 'description', width: 200 },
        { title: '版本', key: 'version', width: 80 }
      ],
      getPage: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await flowProcess.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _flowImport() {
      this.$refs.flowImportModal._open()
    },
    _flowBpmnModal(id) {
      this.$refs.flowBpmnModal._open(id)
    },
    _suspendProcessConfirm(key) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定挂起该流程，所有实例暂停执行？',
        onOk: () => {
          this._suspendProcess(key)
        }
      })
    },
    _suspendProcess: async function(key) {
      const result = await flowProcess.suspendProcess(key)
      this._resultChange(result, '挂起成功')
    },
    _activateProcessConfirm(key) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定激活该流程，所有实例恢复执行？',
        onOk: () => {
          this._activateProcess(key)
        }
      })
    },
    _activateProcess: async function(key) {
      const result = await flowProcess.activateProcess(key)
      this._resultChange(result, '激活成功')
    },
    _dropProcessConfirm(deploymentId) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该流程，所有执行实例无法恢复？',
        onOk: () => {
          this._dropProcess(deploymentId)
        }
      })
    },
    _dropProcess: async function(deploymentId) {
      const result = await flowProcess.dropProcess(deploymentId)
      this._resultChange(result, '删除成功')
    },
    _resultChange(result, msg) {
      if (result) {
        this._page()
        this.$message.success(msg)
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '部署流程':
          this._flowImport()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case 'XML':
          window.open(
            global.baseURL + '/v1/flow-process/bpmn-xml/' + data.id,
            '_blank'
          )
          break
        case '流程图':
          this._flowBpmnModal(data.id)
          break
        case '挂起':
          this._suspendProcessConfirm(data.key)
          break
        case '激活':
          this._activateProcessConfirm(data.key)
          break
        case '删除':
          this._dropProcessConfirm(data.deploymentId)
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
