<template>
  <div>
    <el-row>
      <el-col :span="9">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form inline onsubmit="return false" :label-width="60">
              <label class="label-sign" />
              <Form-item label="组名:" style="width:300px">
                <el-input v-model="formObj.name" placeholder="请输入组名" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-btn">
                <el-button type="primary" @click="_formSearch">
                  搜索
                </el-button>
              </Form-item>
            </Form>
          </el-col>
          <!--操作-->
          <el-col :span="24">
            <btn-list :msg="btn" class="contHide" @on-result-change="_btnClick" />
          </el-col>
          <!-- 表格 -->
          <el-col :span="24">
            <element-table
              ref="pageTable"
              :page-columns="pageColumns"
              :table-height="tableHeight"
              :get-page="getPage"
              click
              hide-elevator
              hide-total
              hide-sizer
              :icon-msg="iconMsg"
              :opt-col-width="60"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                show-overflow-tooltip
                sortable
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <div v-if="item.detail">
                    <a @click.stop="_detailModal(scope.row.id)">{{ scope.row[item.key] }}</a>
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="15">
        <flowApprove ref="flowApprove" />
      </el-col>
    </el-row>
    <!-- 添加、编辑 -->
    <flowGroupEdit ref="editModal" @on-result-change="_page" />
    <!-- 查看详情 -->
    <flowGroupDetail ref="detailModal" />
  </div>
</template>
<script>
import { flowGroup } from '../../../api'
import flowApprove from './flowApprove'
import flowGroupEdit from './flowGroupEdit'
import flowGroupDetail from './flowGroupDetail'

export default {
  components: {
    flowGroupEdit,
    flowGroupDetail,
    flowApprove
  },
  data() {
    return {
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [{ type: 'pt-edit', id: '', name: '编辑' }],
      pageColumns: [
        { title: '组名', key: 'name', detail: true },
        { title: '工作流别名', key: 'alias' }
      ],
      getPage: {},
      formObj: {
        name: ''
      }
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('search')
    }
  },
  mounted() {
    this._formSearch()
    this.$refs.flowApprove._open()
  },
  methods: {
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await flowGroup.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal: async function(id) {
      // 查看
      const result = await flowGroup.getById(id)
      this.$refs.detailModal._open(result)
    },
    _editModal: async function(id) {
      // 编辑
      const result = await flowGroup.getById(id)
      this.$refs.editModal._open(result)
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'click':
          this.$refs.flowApprove._open(data.id)
          break
      }
    }
  }
}
</script>
