<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="表名:">
          <Input v-model="formObj.tableName" placeholder="请输入表名" clearable @on-enter="_formSearch" />
        </Form-item>

        <Form-item class="search-item" label="表类型:">
          <el-select v-model="formObj.tableType" placeholder="请选择">
            <el-option
              v-for="item in tableTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </Form-item>

        <Form-item class="search-item" label="表描述:">
          <Input v-model="formObj.tableName" placeholder="请输入表描述" clearable @on-enter="_formSearch" />
        </Form-item>

        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
          <Button type="primary" @click="_formBlank">
            重置
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <!--      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
      <el-button type="primary" style="margin: 5px" @click="_addForm">
        新增
      </el-button>
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        @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.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>

            <span v-else-if="item.key==='tableType'" class="blue-color">
              <span v-if="scope.row[item.key] === 0" class="blue-color">
                {{ '单表' }}
              </span>
              <span v-else-if="scope.row[item.key] === 1" class="blue-color">
                {{ '主表' }}
              </span>
              <span v-else-if="scope.row[item.key] === 2" class="blue-color">
                {{ '附表' }}
              </span>
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          label="操作"
          align="center"
          :width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <!--            <icon-list-->
            <!--              :msg="iconMsg"-->
            <!--              :row-data="scope.row"-->
            <!--              @on-result-change="_iconClick"-->
            <!--            />-->
            <a style="background: none" @click="_edit(scope.row)">编辑</a>
            <a style="background: none" @click="_synch(scope.row)">同步数据库</a>
            <a style="background: none" @click="_more(scope.row)">更多</a>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <typeEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { onlFormHead } from '../../api'
import typeEdit from './OnlineFormDevEdit'
export default {
  components: { typeEdit },
  data() {
    return {
      formObj: {
        tableName: '',
        tableTxt: '',
        tableType: ''
      },
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '添加' }],
      pageColumns: [
        { title: '表类型', key: 'tableType' },
        { title: '表名', key: 'tableName' },
        { title: '表描述', key: 'tableTxt' },
        { title: '版本', key: 'tableVersion' },
        { title: '同步数据库状态', key: 'isDbSynch' }
      ],
      getPage: {},
      tableTypeList: [
        { label: '单表', value: '0' },
        { label: '主表', value: '1' },
        { label: '附表', value: '2' }
      ],
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ],
      iconMsgDis: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除', disabled: true }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },

    _addForm() {
      this.$refs.editModal._open()
    },

    _formBlank() {
      this.formObj = {}
    },

    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await onlFormHead.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _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.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await onlFormHead.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },

    _edit(row) {
      alert(row.id)
    },
    _synch(row) {
      if (row.isDbSynch === '是') {
        this.$Message.warning('请勿重复生成')
        return false
      }
      this._synchDb(row.id)
    },

    // 添加、编辑保存
    _synchDb: async function(id) {
      // 添加
      const result = await onlFormHead.synchTable(id)
      if (result) {
        this.$Message.success('同步数据库成功')
        this._formSearch()
      }
    },
    _more(row) {
      alert(row.id)
    },
    _editModal: async function(id) {
      const result = await onlFormHead.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
