<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', 'add')"
          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 class-name="action-column" label="操作" width="120px">
          <template slot-scope="scope">
            <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,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
    <!-- RegistFlowDialog -->
    <regist-flow-dialog
      ref="RegistFlowDialog"
      @modified="getList"
    ></regist-flow-dialog>
  </div>
</template>

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

export default {
  name: "flow-manage",
  components: {
    RegistFlowDialog
  },
  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.RegistFlowDialog.open();
    },
    toPublish(row) {
      if (row.publish) return;

      this.$confirm(`确定要发布流程【${row.name}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await flowPublish(row.id);
          this.$message.success("操作成功！");
          row.publish = !row.publish;
        })
        .catch(() => {});
    },
    toDelete(row) {
      this.$confirm(`确定要删除流程【${row.name}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await flowDelete({ flowId: row.id, enable: false });
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {});
    }
  }
};
</script>
