<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="examineName">
        <el-input
          v-model="queryParams.examineName"
          placeholder="请输入流程名称"
          maxlength="60"
          show-word-limit
          clearable v-no-whitespace
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="120px" label="审批流类型" prop="examineType">
        <el-select v-model="queryParams.examineType" placeholder="请选择审批流" clearable >
          <el-option label="专用审批流" value="1"/>
          <el-option label="通用审批流" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="审批业务类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择审批业务类型" clearable >
          <el-option
            v-for="item in flowOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        :editable="false"  range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAuditAdd"
        >新增审批流
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
              :default-sort="defaultSort" @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" align="center" :selectable="isSelectable"/>
      <el-table-column label="审批流名称" align="center" prop="examineName" :show-overflow-tooltip="true" />
      <el-table-column label="审批流编号" align="center" prop="definitionKey" :show-overflow-tooltip="true" />
      <el-table-column label="审批业务类型" align="center" prop="examineType" :show-overflow-tooltip="true" />
      <el-table-column label="审批流类型" align="center" prop="businessType" :show-overflow-tooltip="true" />
      <el-table-column label="业务范围" align="center" prop="proName" :show-overflow-tooltip="true" />
      <el-table-column label="最后修改人" align="center" prop="createUser" :show-overflow-tooltip="true" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" v-if="scope.row.examineType !== '通用审批流'"
                     @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="审批流说明" align="center" prop="examineMark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" width="180" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button @click="handleView(scope.row)" type="text" size="small">查看
          </el-button>
          <el-button @click="handleLoadXml(scope.row)"type="text" size="small">修改
          </el-button>
          <el-button v-if="scope.row.examineType !== '通用审批流'" @click="handleDelete(scope.row)" type="text" size="small">删除
          </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"
    />

  </div>
</template>

<script>
/*
 * 组件名: approvalFlowConfiguration
 * 组件用途: 审批流配置查询页面
 * 创建日期: 2024/8/14
 * 编写者: JJLv
 */
import {changeFlowStatus, delFlowConfiguration, listFlowConfiguration} from "@/api/flow/definition";
import { encryptCBC } from '@/utils/aescbc'
import {queryTheApprovalServiceType } from '@/api/tool/select'

export default {
  name: "approvalFlowConfiguration",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      definitionKeys: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 审批流类型
      flowOptions: [],
      // 表格数据
      list: [],
      // 默认排序
      defaultSort: { prop: 'loginTime', order: 'descending' },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examineName: undefined,
        examineType: undefined,
        businessType: undefined
      },
    };
  },
  created() {
    this.getList();
    this.getFlowOptions()
  },
  methods: {
    /** 查询流程定义列表 */
    getList() {
      this.loading = true
      listFlowConfiguration(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    getFlowOptions(){
      queryTheApprovalServiceType({ }).then(response => {
        this.flowOptions = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.examineId)
      this.definitionKeys = selection.map(item => item.definitionKey)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    isSelectable(row) {
      return row.examineType !== "通用审批流";
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop
      this.queryParams.isAsc = column.order
      this.getList()
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.definitionKey + '"审批流编号吗？').then(function () {
        return changeFlowStatus(row.examineId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 新增按钮操作 */
    handleAuditAdd() {
      this.$router.push("/basics/disposition/auditProcess/" + encryptCBC(JSON.stringify({
        id: '0'
      })));
    },
    handleLoadXml(row) {
      this.$router.push("/basics/disposition/auditProcess/" + encryptCBC(JSON.stringify({
        id: row.examineId,
        version: row.version,
        type: 'edit'
      })));
    },
    handleView(row) {
      this.$router.push("/basics/disposition/auditProcess/" + encryptCBC(JSON.stringify({
        id: row.examineId,
        version: row.version,
        type: 'view'
      })));
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examineId = row.examineId || this.ids;
      const definitionKey = row.definitionKey || this.definitionKeys;
      this.$modal.confirm('是否确认删除编号为"' + definitionKey + '"的数据项？').then(function() {
        return delFlowConfiguration(examineId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  },
}
</script>

<style lang="scss" scoped>

</style>
