<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="标题">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          style="width: 240px"
          @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="info" icon="el-icon-refresh-right" size="mini" @click="reList">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="multipleTable" v-loading="loading" :data="tableData">
      <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" prop="taskId"/>
      <el-table-column label="任务名称" prop="taskName"/>
      <el-table-column label="外置表单" prop="formKey"/>
      <el-table-column label="流程id" prop="instanceId"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success"
                     size="mini"
                     @click="openForm(scope.$index, scope.row)">审批
          </el-button>
          <el-button
            size="mini" type="warning"
            @click="showImage(scope.$index, scope.row)">进度查看
          </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="title" :visible.sync="open" width="1000px">
      <!--历史表单组件-->
      <addEnterprise  ref="addEnterprise"/>
      <el-divider content-position="left">审核</el-divider>
      <template>
        <ele-form
          :isShowSubmitBtn=true
          :isShowBackBtn=false
          :isShowResetBtn=true
          :rules="formObj.rules"
          :form-desc="formObj.formDesc"
          :form-data="formObj.formData"
          :request-fn="handleRequest"
          @request-success="handleRequestSuccess"
        ></ele-form>
      </template>
    </el-dialog>
    <el-dialog :title="activitiImg.title" :visible.sync="activitiImg.open" width="1000px">
      <el-image :src="activitiImg.src"></el-image>
    </el-dialog>
  </div>
</template>

<script>
  import { list,get,add } from '@/api/activiti/pendingNew.js'
  //流程历史表单组件
  import  addEnterprise from "@/views/activiti/formsActiviti/index.vue";
  export default {
    components:{
      addEnterprise
    },
    // 数据模型
    data: function() {
      return {
        instanceId:'',
        // 表格数据
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 分页参数
        total: 0,
        pageNum: 1,
        pageSize: 10,
        tableData: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        //
        taskCommitForm:{
          taskId:undefined,
          myFormDataActiviti:{}
        },
        // 表单数据
        formEntity: {
          fId: {},
          modelKey: {},
          modulId: {},
          fName: {},
          instanceId: {},
          fKey: {},
          fVersion: {},
          applyUser: {},
          jsonData: {}
        },
        forms:{
          title:'查看审核流程',
          open:false,
          data:[]
        },
        // 动态表单数据
        formObj: {
          // 表单数据
          formData: {},
          formDesc: {},
          rules: {}
        },
        activitiImg:{
          title:'查看审核流程图',
          open:false,
          src:"",
        }
      }
    },
    // 初始化方法
    created() {
      // 表格初始化
      this.getList()
    },
    // 定义方法
    methods: {
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      reList() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10
        }
        this.getList()
      },
      // 获取列表数据
      getList() {
        this.loading = true
        list(this.addDateRange(this.queryParams)).then(
          response => {
            if (response.code === 200) {
              this.tableData = response.rows
              this.total = response.total
              this.loading = false
            }else{
              this.msgError(response.msg)
            }

          }
        )
      },

      openForm(index, row) {
        get(row.modelKey,row.formKey,row.modelVersion).then(
          response => {
            this.taskCommitForm.taskId = row.taskId
            this.formEntity = response.data
            this.formEntity.fId = undefined
            this.formEntity.instanceId=row.instanceId;
            let jsonData = response.data.jsonData
            let parse = JSON.parse(jsonData)
            this.formObj.formDesc = parse.formDesc
            this.formObj.rules = parse.rules
            let formData = parse.formData
            if (null != formData) {
              this.formObj.formData = parse.formData
            }
            this.open = true
            this.title = '审批'
            this.$nextTick(()=>{
              this.$refs.addEnterprise.showForms(row.instanceId)
            })
          }
        )

      },/*
      *显示流程图
      * */
      showImage(index, row) {
        let instanceId = row.instanceId
        let modelKey = row.modelKey
        let version = row.modelVersion
        instanceId = process.env.VUE_APP_BASE_URL + '/leaveNew/read-resource?processDefinitionKey=' + modelKey +'&version=' +version+ '&processInstanceId=' + instanceId+ '&t='+Math.random()
        this.activitiImg.open = true
        this.activitiImg.src = instanceId
      },
      handleRequest(data) {
        this.formObj.formData = data
        let s = JSON.stringify(this.formObj)
        this.formEntity.jsonData = s
        this.taskCommitForm.myFormDataActiviti = this.formEntity
        console.log(this.taskCommitForm);
        add(this.taskCommitForm).then(response => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
            this.getList()
          } else {
            this.msgError(response.msg)
          }
        })
        return Promise.resolve()
      },
      handleRequestSuccess() {
        this.$message.success('发送成功')
        this.getList()
        this.open = false
      },
    }
  }
</script>
<style scoped>
</style>
