<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入流程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="KEY" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入KEY"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属分类" prop="category">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入分类字符"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search
" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button type="primary" icon="el-icon-upload" size="mini" @click="handleUpload"
                   v-hasPermi="['system:menu:add']">
          部署自定义流程
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="definitionList"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" hidden prop="id" width="100"/>
      <el-table-column label="KEY" align="center" prop="key" width="100"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="描述" align="center" prop="description" width="100"/>
      <el-table-column label="版本" align="center" prop="version" width="100"/>
      <el-table-column label="分类" align="center" prop="category" width="100"/>
      <el-table-column label="部署时间" align="center" prop="deploymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTimeT(scope.row.deploymentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程定义" align="center" prop="resourceName" width="180">
        <template slot-scope="scope">
          <el-link target="_blank" type="success"
                   :href="baseUrl+'/activiti/definition/readResource?pdid='+scope.row.id+'&resourceName='+scope.row.resourceName">
            查看流程定义
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="流程图" align="center" prop="diagramResourceName" width="180">
        <template slot-scope="scope">
          <el-link target="_blank" type="primary"
                   :href="baseUrl+'/activiti/definition/readResource?pdid='+scope.row.id+'&resourceName='+scope.row.diagramResourceName">
            查看流程图
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="流程定义状态" align="center" prop="suspendStateName" width="180">
        <template slot-scope="scope">
          <el-tag
            :type="formatStatus(scope.row.suspendState)"
            effect="dark">
            {{ scope.row.suspendStateName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.suspendState === '2'?'el-icon-video-play':'el-icon-video-pause'"
            @click="handleSuspendOrActiveApply(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >{{scope.row.suspendState === '2' ? '激活' : '挂起'}}
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="handleConvert2Model(scope.row)"
            v-hasPermi="['system:menu:add']"
          >转模型
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".bpmn"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“*.bpmn”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listDefinition, suspendOrActiveApply, convert2Model, delDefinition } from '@/api/activiti/definition/index'
  import { getToken } from '@/utils/auth'

  export default {
    name: 'Definition',
    data() {
      return {
        baseUrl: process.env.VUE_APP_BASE_API,
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 表格数据
        definitionList: [],
        // 查询参数
        queryParams: {
          name: undefined,
          key: undefined,
          category: undefined,
          pageNum: 1,
          pageSize: 10
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/activiti/definition/upload'
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询登录日志列表 */
      getList() {
        this.loading = true
        listDefinition(this.queryParams).then(response => {
          this.definitionList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      formatStatus(status) {
        return status === '2' ? 'warning' : 'success'
      },
      handleUpload(row) {
        this.upload.title = '部署自定义流程'
        this.upload.open = true
      },
      handleSuspendOrActiveApply(row) {
        let suspendOrActive = row.suspendState === '2' ? '激活' : '挂起'
        this.$confirm('是否确认"' + suspendOrActive + '"流程定义?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return suspendOrActiveApply(row.id, row.suspendState)
        }).then(() => {
          this.getList()
          this.msgSuccess(suspendOrActive + '成功')
        }).catch(function() {
        })
      },
      handleConvert2Model(row) {
        this.$confirm('确认要转换成流程模型吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return convert2Model(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess('转换成功')
        }).catch(function() {
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const name = row.name
        this.$confirm('是否确认删除名称为"' + name + '"的流程定义?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delDefinition(row.deploymentId)
        }).then(() => {
          if (this.definitionList.length - 1 === 0) {
            this.queryParams.pageNum = this.queryParams.pageNum - 1 === 0 ? 1 : this.queryParams.pageNum - 1
          }
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit()
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false
        this.upload.isUploading = false
        this.$refs.upload.clearFiles()
        this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
        this.getList()
      }
    }
  }
</script>

