<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>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:menu:add']">
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="modelerList"
      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" 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="version" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTimeT(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTimeT(scope.row.lastUpdateTime) }}</span>
        </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="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="handleDeploy(scope.row)"
            v-hasPermi="['system:menu:add']"
          >部署
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            v-hasPermi="['system:menu:add']"
          >导出
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row,scope.$index)"
            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 id="editModeler" title="修改流程" :visible.sync="editOpen" width="90%" top="10" fullscreen>
      <iframe :src="src" id="activiti_main" frameborder="no" :style="'width: 100%;height:'+ iframe.height"
              scrolling="auto"/>
    </el-dialog>
  </div>
</template>

<script>
  import { listModeler, deployModeler, delModeler } from '@/api/activiti/modeler/index'

  export default {
    name: 'modeler',
    data() {
      return {
        src: '#',
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 表格数据
        modelerList: [],
        // 查询参数
        queryParams: {
          name: undefined,
          key: undefined,
          pageNum: 1,
          pageSize: 10
        },
        // 是否显示弹出层
        editOpen: false,
        // 表单参数
        form: {},
        iframe: {
          height: document.documentElement.clientHeight - 126.5 + 'px;'
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: '流程名称不能为空', trigger: 'blur' }
          ],
          key: [
            { required: true, message: '唯一标识符Key不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询登录日志列表 */
      getList() {
        this.loading = true
        listModeler(this.queryParams).then(response => {
          this.modelerList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 表单重置
      reset() {
        this.form = {
          name: undefined,
          key: undefined,
          description: undefined
        }
        this.resetForm('form')
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.src = process.env.VUE_APP_BASE_API + '/modeler/create?r=' + Math.random()
        this.editOpen = true
      },
      /** 编辑按钮操作 */
      handleUpdate(row) {
        this.src = process.env.VUE_APP_BASE_API + '/modeler/activiti/index?modelId=' + row.id
        document.getElementById('activiti_main').contentWindow.location.reload()
        this.editOpen = true
      },
      /** 修改按钮操作 */
      handleDeploy(row) {
        const name = row.name
        this.$confirm('是否确认部署名称为"' + name + '"的流程模型?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return deployModeler(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess('部署成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport(row) {
        window.location.href = process.env.VUE_APP_BASE_API + '/activiti/modeler/export?modelId=' + row.id
      },
      /** 删除按钮操作 */
      handleDelete(row, index) {
        const name = row.name
        this.$confirm('是否确认删除名称为"' + name + '"的流程模型?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delModeler(row.id)
        }).then(() => {
          if (this.modelerList.length - 1 === 0) {
            this.queryParams.pageNum = this.queryParams.pageNum - 1 === 0 ? 1 : this.queryParams.pageNum - 1
          }
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      }
    }
  }
</script>

