<template>
	<div style="text-align: center;">
		<div style="width:600px;float: left; margin: 10px;">

			<el-input v-model="definitionKey" placeholder="请输入流程定义key" style="width :300px;float: left;"></el-input>

			<el-button type="primary" @click="handleStart">启动实例</el-button>
		</div>




		<el-table :data="tableData" border style="width: 99%">
			<el-table-column prop="id" label="id" v-if="isShow" width="100">
			</el-table-column>
			<el-table-column prop="name" label="流程名称" width="180">
			</el-table-column>
			<el-table-column prop="processDefinitionId" label="流程定义ID" width="180">
			</el-table-column>
			<el-table-column prop="processInstanceId" label="流程实例ID" width="100">
			</el-table-column>
			<el-table-column prop="startUserId" label="流程发起人" width="100">
			</el-table-column>
			<el-table-column prop="assignee" label="当前任务处理人" width="100">
			</el-table-column>
			<el-table-column prop="startTime" label="开始时间" width="100">
			</el-table-column>
			<el-table-column prop="duration" label="耗时" width="100">
			</el-table-column>
			<el-table-column prop="isSuspended" label="状态" width="100">
			</el-table-column>


			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button v-if="scope.row.isSuspended" size="mini" type="success" @click="handleActivate(scope.row)">运行</el-button>
					<el-button v-else size="mini" type="danger" @click="handleSuspend(scope.row)">暂停</el-button>
					<el-button size="mini" type="success"  @click="handleImg(scope.row)">流程图</el-button>
					<!-- <el-button size="mini" type="primary" @click="handleJumpTo(scope.row)">跳转</el-button> -->
					<el-button size="mini" type="" @click="handleHistroty(scope.row)">历史</el-button>
					<el-button size="mini" type="primary" @click="handleDelete(scope.row)">关闭</el-button>

				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
		 :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalRows">
		</el-pagination>



		<el-dialog title="详情" :visible.sync="isShow" width="1000px">
			<img :src="imgUrl"></img>
		</el-dialog>


	</div>
</template>

<script>
	import axios from '@/axios';
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState({
				serverName: state => state.app.serverName
			})
		},
		data() {
			return {
				//表格初始化参数
				tableData: [],
				mi: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [10, 50, 100, 200],
				pageSize: 10,
				//查询条件
				definitionKey: '',
				//录入框
				dialogVisible: false,
				form: {
					processName: '',
					processKey: '',
					processDescription: ''
				},
				isShow: false,
				imgBaseUrl: '/ProcessTask/showImg.do?processInstanceId=',
				imgUrl:''
			}
		},
		//界面渲染完毕调用   初始化表格参数
		mounted() {
			this.getTableData()
		},
		methods: {
			// 获取table数据
			getTableData() {
				 //console.log("获取table数据getTableData")
				var url = 'ProcessInstance/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					key: '',
					userId: sessionStorage.getItem("userId")
				}
				axios.get(url, param).then(response => {
					this.tableData = response.data.list
					this.totalRows = response.data.total

				});
			},
			handleStart() {

				var url = 'ProcessInstance/start.do'
				var param = {
					key: this.definitionKey,
					userId: sessionStorage.getItem("userId")

				}
				axios.get(url, param).then(response => {
					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '启动成功!',
						});
						this.getTableData()
					} else {
						this.$message({
							type: 'error',
							message: '启动失败' + response.data
						});
					}
				});

			},

			// 修改每页条数事件
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.currentPage = val
				this.getTableData()
			},

			handleSuspend(row) {


				var url = 'ProcessInstance/suspend.do'
				var param = {
					instanceId: row.processInstanceId,
					userId: sessionStorage.getItem("userId")

				}
				axios.get(url, param).then(response => {
					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '暂停成功!',
						});
						this.getTableData()
					} else {
						this.$message({
							type: 'error',
							message: '暂停失败' + response.data
						});
					}
				});

			},
			handleActivate(row) {
				var url = 'ProcessInstance/activate.do'
				var param = {
					instanceId: row.processInstanceId,
					userId: sessionStorage.getItem("userId")

				}
				axios.post(url, param).then(response => {
					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '启动成功!',
						});
						this.getTableData()
					} else {
						this.$message({
							type: 'error',
							message: '启动失败' + response.data
						});
					}
				});
			},

			// 删除按钮事件
			handleDelete(row) {
				this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {

					var url = 'ProcessInstance/remove.do'
					var param = {
						instanceId:row.processInstanceId,
						userId: sessionStorage.getItem("userId")

					}
					axios.post(url, param).then(response => {
						if (response.data === 'success') {
							this.$message({
								type: 'success',
								message: '删除成功!',
							});
							this.getTableData()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败' + response.data
							});
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			handleImg(row) {
				//window.open(this.imgUrl + row.deploymentId)
				this.isShow = true
				this.imgUrl = this.serverName +this.imgBaseUrl + row.processInstanceId
			},
			handleHistroty(row) {
			 
				this.$router.push({
					path: '/process/historyActInst',
					query: {
						instanceId: row.processInstanceId,
					},
					mate: {
						keepAlive: false
					}
				})
			},
		},
		beforeRouteLeave(to, from, next) {
			to.meta.keepAlive = false;
			next();
		}
	}
</script>
