<template>
  <div>
    <treePage title="报表按钮列表" @on-result-change="_refresh">
      <template slot="tree">
        <relReportTree 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="70"
              inline
            >
              <label class="label-sign" />
              <Form-item class="search-item" label="按钮名称:">
                <el-input v-model="formObj.buttonName" placeholder="请输入按钮名称" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-item" label="按钮编码:">
                <el-input v-model="formObj.key" placeholder="请输入按钮编码" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-item" label="服务名称:">
                <el-input v-model="formObj.service" 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 v-if="item.dateTime">
                    {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </template>
    </treePage>
    <!--添加编辑-->
    <reportConfigEdit ref="editModal" @on-result-change="_search" />
  </div>
</template>
<script>
import { printExport } from '../../../api'
import treePage from '../../../components/base/treePage'
import relReportTree from '../resource/tree/relReportTree'
import httpJson from '../../../api/httpJson'
import reportConfigEdit from './reportConfigEdit'
export default {
  components: {
    treePage,
    relReportTree,
    reportConfigEdit
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [
        { type: 'pt-edit', id: '', name: '编辑' },
        { type: 'pt-trash', id: '', name: '删除' }
      ],
      pageColumns: [
        { title: '按钮名称', key: 'buttonName' },
        { title: '按钮编码', key: 'key' },
        { title: '报表名称', key: 'reportName' },
        { title: '导出方式', key: 'mode', width: 120 },
        { title: 'url', key: 'uri' },
        { title: '服务名称', key: 'service', width: 120 },
        { title: '添加人', key: 'operator', width: 120 },
        { title: '添加时间', key: 'ctime', dateTime: true, width: 140 },
        { title: '备注', key: 'remark' }
      ],
      formObj: {
        buttonName: '',
        key: '',
        service: '',
        keyList: []
      },
      getPage: {},
      treeObj: {},
      lastNode: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    // 刷新
    _refresh() {
      this.formObj = this.$resetFields(this.formObj)
      this.treeObj = {}
      this.lastNode = {}
      this.$refs.treeModal._open()
      this._page()
    },
    _search() {
      this._formSearch()
      this.$refs.treeModal._open()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const pages = this.formObj.page === undefined ? 1 : this.formObj.page
      httpJson
        .post(
          '/print/v1/export/page?page=' + pages + '&rows=' + this.formObj.rows,
          this.$serializeFormSearch(this.formObj)
        )
        .then(result => {
          if (result) {
            this.getPage = result
            this.$refs.pageTable._initTable()
          }
        })
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open(this.treeObj, this.lastNode, '')
          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._deleteById(data.id)
          break
      }
    },
    _deleteById(id) {
      this.$confirm('确定删除该记录?', '提示', { type: 'warning' }).then(() => {
        this._delOk(id)
      })
    },
    _delOk: async function(id) {
      const result = await printExport.deleteById(id)
      if (result) {
        this.$message.success('删除成功')
        this._page()
      }
    },
    _treeData(data, lastNode, codes) {
      this.formObj.keyList = codes
      this.treeObj = data
      this.lastNode = lastNode
      this._formSearch()
    }
  }
}
</script>
