<template>
  <div>
    <treePage title="菜单列表" @on-result-change="_refresh">
      <template slot="tree">
        <relResourceTree ref="treeModal" @on-result-change="_treeData" />
      </template>
      <template slot="cont">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form
              v-show="searchOpen"
              onsubmit="return false"
              :label-width="50"
              inline
            >
              <label class="label-sign" />
              <Form-item class="search-item" label="类型:">
                <el-input v-model="formObj.type" placeholder="请输入类型" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <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.title" placeholder="请输入标题" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-item" label="编码:">
                <el-input v-model="formObj.code" 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"
              :icon-msg="iconMsg"
              :opt-col-width="100"
              @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>
      </template>
    </treePage>
    <!--添加编辑-->
    <tableColResourceEdit ref="editModal" @on-result-change="_formSearch" />
  </div>
</template>
<script>
import { sysTableColumn } from '../../../api'
import treePage from '../../../components/base/treePage'
import relResourceTree from '../resource/tree/relResourceTree'
import tableColResourceEdit from './tableColResourceEdit'
export default {
  components: {
    treePage,
    relResourceTree,
    tableColResourceEdit
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [
        { type: 'pt-edit', id: '', name: '编辑' },
        { type: 'pt-copy', id: '', name: '复制' },
        { type: 'pt-trash', id: '', name: '删除' }
      ],
      pageColumns: [
        { title: '类型', key: 'type' },
        { title: '表格名称', key: 'name' },
        { title: '标题', key: 'title' },
        { title: '编码', key: 'code' },
        { title: '所属菜单', key: 'menuName' }
      ],
      formObj: {
        code: '',
        title: '',
        menuId: '',
        type: '',
        name: ''
      },
      getPage: {},
      treeObj: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    // 刷新
    _refresh() {
      this.formObj.menuId = ''
      this.treeObj = {}
      this._formSearch()
    },
    _search() {
      this._formSearch()
      this.$refs.treeModal._open()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysTableColumn.page(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open(this.treeObj, '')
          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.$refs.editModal._open('', data)
          break
        case '复制':
          this._copyModal(data)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 复制
    _copyModal(data) {
      this.$refs.editModal._openCopy('', data)
    },
    _deleteById(id) {
      this.$confirm('确定删除该记录?', '提示', { type: 'warning' }).then(() => {
        this._delOk(id)
      })
    },
    _delOk: async function(id) {
      const result = await sysTableColumn.deleteById(id)
      if (result) {
        this.$message.success('删除成功')
        this._page()
      }
    },
    _treeData(data) {
      this.formObj.menuId = data.id
      this.treeObj = data
      this._formSearch()
    }
  }
}
</script>
