
<template>
  <div class="page">
    <LayoutTable :data="tableData" :config="tableConfig" notAdd />
  </div>
</template>

<script>
import { finishedList, getDeployment, delDeployment, addDeployment, updateDeployment, exportDeployment, revokeProcess } from "@/api/flowable/finished";

import table from "@/assets/mixins/table";

export default {
  mixins: [table],
  methods: {
    /** 查询流程定义列表 */
    getList() {
    //   this.loading = true;
    //   finishedList(this.queryParams).then(response => {
    //     this.finishedList = response.data.records;
    //     this.total = response.data.total;
    //     this.loading = false;
    //   });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null
      };
      this.resetForm("form");
    },
    setIcon(val){
      if (val){
        return "el-icon-check";
      }else {
        return "el-icon-time";
      }

    },
    setColor(val){
      if (val){
        return "#2bc418";
      }else {
        return "#b3bdbb";
      }

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程定义";
    },
    /** 流程流转记录 */
    handleFlowRecord(row){
      this.$router.push({ path: '/flowable/task/record/index',
        query: {
          procInsId: row.procInsId,
          deployId: row.deployId,
          taskId: row.taskId,
          finished: false
      }})
    },
    /** 撤回任务 */
    handleRevoke(row){
      const params = {
        instanceId: row.procInsId
      }
      revokeProcess(params).then( res => {
        this.msgSuccess(res.msg);
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeployment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程定义";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeployment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeployment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除流程定义编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDeployment(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有流程定义数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportDeployment(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  },
  data() {
    return {
      config: {
        search: [
          {
            name: "name",
            type: "text",
            label: "名称",
          },
        ],
        columns: [
          {
            type: "selection",
            width: 60,
          },

          {
            prop: "taskId",
            label: "任务编号",
          },

          {
            prop: "procDefName",
            label: "流程名称",
          },

          {
            prop: "taskName",
            label: "任务节点",
          },

          {
            prop: "procDefVersion",
            label: "流程版本",
            formatter: (row) => {
              return <el-tag size="medium">v{row.procDefVersion}</el-tag>;
            },
          },

          {
            prop: "startUserName",
            label: "流程发起人",
            formatter: (row) => {
              return (
                <label>
                  {row.startUserName}
                </label>
              );
            },
          },

          {
            prop: "createTime",
            label: "接收时间",
            width: 140,
            formatter: this.formatterDate,
          },

          {
            prop: "finishTime",
            label: "审批时间",
            width: 140,
            formatter: this.formatterDate,
          },

          {
            prop: "duration",
            label: "耗时",
          },

          {
            label: "操作",
            width: 180,
            formatter: (row) => {
              return (
                <el-row>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-tickets"
                    onClick={() => {
                      this.handleFlowRecord(row);
                    }}
                  >
                    流转记录
                  </el-button>

                  <span></span>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-tickets"
                    onClick={() => {
                      this.handleRevoke(row);
                    }}
                  >
                    撤回
                  </el-button>
                </el-row>
              );
            },
          },
        ],
      },
    };
  },
};
</script>
