<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form
          v-show="searchOpen"
          inline
          onsubmit="return false"
          :label-width="70"
        >
          <label class="label-sign" />
          <Form-item class="search-item" label="表单名称:">
            <el-input v-model="formObj.name" placeholder="请输入表单名称" clearable @keyup.enter.native="_formSearch" />
          </Form-item>
          <Form-item class="search-item" label="表单标识:">
            <el-input v-model="formObj.uri" 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" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
      </el-col>
      <!--table-->
      <el-col :span="24">
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          :table-height="tableHeight"
          :get-page="getPage"
          :opt-col-width="80"
          :icon-msg="iconMsg"
          @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>{{ scope.row[item.key] }}</div>
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
    <!--添加编辑-->
    <formManageEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { sysForm } from '../../../api'
import formManageEdit from './formManageEdit'

export default {
  components: { formManageEdit },
  data() {
    return {
      searchOpen: true,
      formObj: {
        name: '',
        uri: ''
      },
      btn: [{ type: 'success', id: '', name: '添加' }],
      pageColumns: [
        { title: '表单名称', key: 'name' },
        { title: '表单标识', key: 'uri' },
        { title: '备注', key: 'remark' }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-edit', id: '', name: '编辑' },
        { type: 'pt-trash', id: '', name: '删除' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysForm.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    _deleteById(id) {
      this.$confirm('确定要删除?', '提示', {
        type: 'warning'
      }).then(() => {
        this._delOk(id)
      })
    },
    _delOk: async function(id) {
      const result = await sysForm.deleteById(id)
      if (result) {
        this.$message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await sysForm.getById(id)
      this.$refs.editModal._open(result)
    }
  }
}
</script>
