<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form
          inline
          onsubmit="return false"
          :label-width="80"
          v-show="searchOpen">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="模块名称:">
            <el-input v-model="formObj.moduleName" placeholder="请输入模块名称" @keyup.enter.native="_formSearch" clearable></el-input>
          </Form-item>
          <Form-item class="search-item" label="执行SQL:">
            <el-input v-model="formObj.sql" placeholder="请输入执行SQL" @keyup.enter.native="_formSearch" clearable></el-input>
          </Form-item>
          <Form-item class="search-item" label="模板HTML:">
            <el-input v-model="formObj.html" placeholder="请输入模板HTML" @keyup.enter.native="_formSearch" clearable></el-input>
          </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" @on-result-change="_btnClick" showSearchBtn="true"/>
      </el-col>
      <!--table-->
      <el-col :span="24">
        <element-table
          :pageColumns="pageColumns"
          :tableHeight="tableHeight"
          @on-result-change="_tableResultChange"
          ref="pageTable"
          :getPage="getPage"
          :optColWidth="80"
          :iconMsg="iconMsg">
          <el-table-column
            show-overflow-tooltip
            sortable
            :prop="item.key"
            :label="item.title"
            :width="item.width"
            :min-width="200"
            :fixed="item.fixed?item.fixed:undefined"
            v-for="item in pageColumns"
            :key="item.key">
            <template slot-scope="scope">
              <a v-if="item.detail" @click.stop="_detailModal(scope.row.id)">{{scope.row[item.key]}}</a>
              <div v-else>{{scope.row[item.key]}}</div>
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
    <!--添加编辑-->
    <printTemplateEdit ref="editModal" @on-result-change="_page"></printTemplateEdit>
    <!--详情-->
    <printTemplateDetail ref="detailModal"></printTemplateDetail>
  </div>
</template>
<script>
import { printTemplate } from '../../../api'
import printTemplateEdit from './printTemplateEdit'
import printTemplateDetail from './printTemplateDetail'
export default {
  components: { printTemplateEdit, printTemplateDetail },
  data() {
    return {
      searchOpen: true,
      formObj: {
        moduleName: '',
        sql: '',
        html: ''
      },
      btn: [{ type: 'success', id: '', name: '添加' }],
      pageColumns: [
        { title: '模块名称', key: 'moduleName', width: 150, detail: true },
        { title: '执行 SQL', key: 'sql' }
      ],
      getPage: {},
      iconMsg: [{ type: 'pt-edit', 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 printTemplate.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
      }
    },
    _editModal: async function(id) {
      const result = await printTemplate.getById(id)
      this.$refs.editModal._open(result)
    },
    // 详情
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    }
  }
}
</script>
