<template>
  <div class="flow-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="名称：">
            <el-input
              style="width: 200px;"
              v-model.trim="filter.name"
              placeholder="请输入内容"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'register')"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >添加流程</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="流程名称"></el-table-column>
        <el-table-column prop="type" label="流程类型" width="160">
          <span slot-scope="scope">{{ scope.row.type | flowTypeFilter }}</span>
        </el-table-column>
        <el-table-column class-name="action-column" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEditDetail(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >重命名</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ModifyFlow -->
    <modify-flow
      ref="ModifyFlow"
      :instance="curFlow"
      @modified="flowModified"
    ></modify-flow>
    <!-- ModifyFlowDetail -->
    <modify-flow-detail
      ref="ModifyFlowDetail"
      :instance="curFlow"
      @modified="getList"
    ></modify-flow-detail>
  </div>
</template>

<script>
import { flowListPage, flowDelete } from "../api";
import ModifyFlow from "../components/ModifyFlow";
import ModifyFlowDetail from "../components/ModifyFlowDetail";

export default {
  name: "flow-manage",
  components: {
    ModifyFlow,
    ModifyFlowDetail
  },
  data() {
    return {
      filter: {
        name: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curFlow: {}
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await flowListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curFlow = {};
      this.$refs.ModifyFlow.open();
    },
    toEdit(row) {
      this.curFlow = { ...row };
      this.$refs.ModifyFlow.open();
    },
    toEditDetail(row) {
      this.curFlow = { ...row };
      this.$refs.ModifyFlowDetail.open();
    },
    flowModified({ isEdit, data }) {
      if (!isEdit) {
        this.curFlow = data;
        this.$refs.ModifyFlowDetail.open();
        return;
      }
      this.getList();
    },
    toDelete(row) {
      this.$confirm(`确定要删除流程【${row.name}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await flowDelete({ id: row.id, enable: 0 });
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {});
    }
  }
};
</script>
