<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               v-model="form"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menu"
                slot-scope="{row}">
        <el-button v-if="permission.wf_process_start_flow"
                   type="text"
                   size="small"
                   icon="el-icon-video-play"
                   @click="handleStart(row)">发起</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { processList as getList } from "@/api/plugin/workflow/process";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: 'drawer',
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: 'center',
        searchMenuSpan: 6,
        column: [
          {
            label: "流程名称",
            prop: "name",
            overHidden: true,
            search: true
          },
          {
            label: "流程标识",
            prop: "key",
            overHidden: true,
            search: true
          },
          {
            label: "流程分类",
            row: true,
            type: 'tree',
            dicUrl: '/api/blade-workflow/design/category/tree',
            props: {
              label: 'name',
              value: 'id'
            },
            prop: "category",
            search: true,
          },
          {
            label: '版本',
            prop: 'version',
            width: 90
          },
          {
            label: '状态',
            prop: 'status',
            dicData: [{
              label: '激活',
              value: 1
            }, {
              label: '挂起',
              value: 2
            }],
          },
        ]
      },
      data: [],
      row: '',
      categoryVisible: false
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.deployment_add, false),
        viewBtn: this.vaildData(this.permission.deployment_view, false),
        delBtn: this.vaildData(this.permission.deployment_delete, false),
        editBtn: this.vaildData(this.permission.deployment_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleStart(row) {
      const param = {
        processId: row.id
      }
      this.$router.push('/workflow/process/start/' + Buffer.from(JSON.stringify(param)).toString('base64'))
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      done()
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    }
  }
};
</script>

<style>
</style>
