<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <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="开始时间" prop="deployTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.deployTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="small"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ownProcessList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="流程编号"
        align="center"
        prop="procInsId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流程名称"
        align="center"
        prop="procDefName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流程类别"
        align="center"
        prop="category"
        :formatter="categoryFormat"
      />
      <el-table-column label="流程版本" align="center" width="80px">
        <template #default="{ row }">
          <el-tag size="default">v{{ row.procDefVersion }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="提交时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column label="流程状态" align="center" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.finishTime == null" size="small">进行中</el-tag>
          <el-tag type="success" v-if="row.finishTime != null" size="small"
            >已完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="耗时"
        align="center"
        prop="duration"
        width="180"
      />
      <el-table-column label="当前节点" align="center" prop="taskName" />
      <el-table-column label="办理" align="center">
        <template #default="{ row }">
          <label v-if="row.assigneeName"
            >{{ row.assigneeName }}
            <el-tag type="info" size="small">{{ row.deptName }}</el-tag></label
          >
          <label v-if="row.candidate">{{ row.candidate }}</label>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            size="small"
            icon="tickets"
            @click="handleFlowRecord(row)"
            v-hasPermi="['flowable:process:query']"
            >详情</el-button
          >
          <el-button
            type="primary"
            link
            size="small"
            icon="delete"
            @click="handleDelete(row)"
            v-hasPermi="['flowable:process:remove']"
            >删除</el-button
          >
          <el-button
            type="primary"
            link
            size="small"
            icon="circle-close"
            @click="handleStop(row)"
            v-hasPermi="['flowable:process:cancel']"
            >取消</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script setup name="Own">
import {
  listOwnProcess,
  stopProcess,
  delProcess,
} from "@/api/flowable/process";
import { download } from "@/utils/request";
import { listAllCategory } from "@/api/flowable/category";
import { ref, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const queryFormRef = ref();
// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
const categoryOptions = ref([]);
// 我发起的流程列表数据
const ownProcessList = ref([]);
// 弹出层标题
// 是否显示弹出层
const data = reactive({
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null,
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {},
});
const { queryParams } = toRefs(data);

/** 查询流程分类列表 */
function getCategoryList() {
  listAllCategory().then((response) => (categoryOptions.value = response.rows));
}
/** 查询流程定义列表 */
function getList() {
  loading.value = true;
  listOwnProcess(queryParams.value).then((response) => {
    ownProcessList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.procInsId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/**  取消流程申请 */
function handleStop(row) {
  const params = {
    procInsId: row.procInsId,
  };
  stopProcess(params).then((res) => {
    ElMessage.success(res.msg);
    getList();
  });
}

const router = useRouter();
/** 流程流转记录 */
function handleFlowRecord(row) {
  router.push({
    path: "/flowable/process/detail/" + row.procInsId,
    query: {
      definitionId: row.procDefId,
      deployId: row.deployId,
      taskId: row.taskId,
      finished: false,
    },
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  // const ids = row.procInsId;
  const deleteIds = row.procInsId ? [row.procInsId] : ids.value;
  ElMessageBox.confirm(
    '是否确认删除流程定义编号为"' + deleteIds.join(",") + '"的数据项?',
    "警告",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(function () {
      return delProcess(deleteIds.join(","));
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    });
}
/** 导出按钮操作 */
function handleExport() {
  // const queryParams = queryParams.value;
  ElMessageBox.confirm("是否确认导出所有流程定义数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(function () {
      // return exportDeployment(queryParams);
    })
    .then((response) => {
      download(response.msg);
    });
}
function categoryFormat(row, column) {
  return (
    categoryOptions.value.find((k) => k.code === row.category)?.categoryName ??
    ""
  );
}
getCategoryList();

defineExpose({
  getList,
});
</script>
<script>
export default {
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.getList();
    });
  },
};
</script>
