<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" size="mini" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="流程定义编号">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="流程名称">
      </el-table-column>
      <el-table-column
        prop="flowKey"
        header-align="center"
        align="center"
        label="流程KEY">
      </el-table-column>

      <el-table-column
        prop="suupensionState"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scoped">
          <span v-if="scoped.row.suupensionState">挂起</span>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="deploymentId"
        header-align="center"
        align="center"
        label="部署id">
      </el-table-column>
      <el-table-column
        prop="deploymentDate"
        header-align="center"
        align="center"
        label="部署时间">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="图片" >
        <template slot-scope="scope">
              <img :src="$imgBasePath+'downloadFlowImg?defId='+scope.row.id" width="200" height="100">
          </template>
      </el-table-column>

      <el-table-column
        prop="version"
        header-align="center"
        align="center"
        label="版本号">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.suupensionState" size="small" @click="suupensHandle(scope.row.id,1)">激活</el-button>
          <el-button type="text" v-if="!scope.row.suupensionState" size="small" @click="suupensHandle(scope.row.id,2)">挂起</el-button>

          <el-button type="text"  size="small" @click="showFlowImgHandle(scope.row.id)">查看流程图</el-button>
          <el-button type="text"  size="small" @click="showFlowXMLHandle(scope.row.id)">查看流程XML</el-button>
          <el-button type="text" v-if="!scope.row.suupensionState"  size="small" @click="startFlowHandle(scope.row.id)">发起流程</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog
      title="流程图"
      :visible.sync="dialogVisible"
      width="50%">
      <div style="height: auto;margin-bottom: 30px;">
        <img :src="flowImg"  height="auto">
      </div>
    </el-dialog>

    <el-dialog
      title="流程定义XML内容"
      :visible.sync="dialogXMLVisible"
      width="85%">
      <div style="height: auto;margin-bottom: 30px;">
        <pre>{{ flowXML }}</pre>
      </div>
    </el-dialog>

    <el-dialog
      title="发起流程"
      :visible.sync="dialogFormVisible"
      width="30%">
      <div style="height: auto;margin-bottom: 30px;">
        <el-form :model="dynamiForm"  label-width="100px" class="demo-dynamic">

          <el-form-item
            v-for="(form) in dynamiForm"
            :label="form.name"
            :key="form.key"
            :prop="form.key"

          >
              <el-select v-if="form.type == 'assignee'" v-model="form.value" placeholder="请选择">
                <el-option
                  v-for="item in users"
                  :key="item.userId"
                  :label="item.nickname+'['+item.username+']'"
                  :value="item.userId">
                </el-option>
              </el-select>
              <el-select v-if="form.type == 'candidateUsers'" v-model="form.value" placeholder="请选择">
                <el-option
                  v-for="item in users"
                  :key="item.userId"
                  :label="item.nickname+'['+item.username+']'"
                  :value="item.userId">
                </el-option>
              </el-select>
              <el-select v-if="form.type == 'candidateGroups'" v-model="form.value" placeholder="请选择">
                <el-option
                  v-for="item in roles"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  @click="submitStartFlow()">提交</el-button>
          </el-form-item>
      </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import vkbeautify from 'vkbeautify';
  export default {
    data () {
      return {
        id:'',
        dataForm: {
          key: ''
        },dynamiForm:[],
        domain:{},
        flowForm:{

        },
        flowImg:'',
        flowXML:'',
        users:[],
        roles:[],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dialogVisible:false,
        dialogXMLVisible:false,
        dialogFormVisible:false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/flow/deploy/listProcDef'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },submitStartFlow(){
        for( var item of this.dynamiForm){
            this.flowForm[item.key] = item.value
        }
        this.flowForm.id = this.id
        // 提交表单数据
        this.$http({
              url: this.$http.adornUrl(`/flw/instance/startFlowInstance`),
              method: 'get',
              params: this.$http.adornParams(this.flowForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.flowForm = {}
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },startFlowHandle(id){
        this.id = id
        // 查询出相关的动态的流程定义信息和相关的动态的数据。然后加载动态的表单
        this.flowForm = {}
        this.$http({
              url: this.$http.adornUrl(`/flow/deploy/flowDef/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dynamiForm = data.data
                this.users = data.users
                this.roles = data.roles
                // 需要动态的给 flowForm 绑定元素
                this.dialogFormVisible = true
              }
            })
      },showFlowImgHandle(id){
        this.flowImg = this.$imgBasePath + "downloadFlowImg?defId="+id
        this.dialogVisible = true
      },showFlowXMLHandle(id){
       this.$http({
              url: this.$http.adornUrl(`/flow/deploy/flowXML/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                // 获取返回的图片信息
                this.flowXML = vkbeautify.xml(data.flowXML)
                this.dialogXMLVisible = true
              }
            })
      },
      // 删除
      suupensHandle (id,state) {

        this.$confirm(`确定对该记录进行[${state==2 ? '挂起' : '激活'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/flow/deploy/updateState'),
            method: 'get',
            params: this.$http.adornParams({
              'id': id
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
