<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="1000px"
      title="选择报表"
    >
      <div>
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form id="search-report-xml" inline onsubmit="return false" :label-width="70">
              <label class="label-sign" />
              <Form-item label="报表名称:">
                <el-input v-model="formObj.name" placeholder="请输入报表名称" style="width: 200px" clearable @keyup.enter.native="_search" />
              </Form-item>
              <Form-item label="服务名称:">
                <el-input
                  v-model="formObj.service"
                  placeholder="请输入服务名称"
                  style="width: 200px"
                  clearable
                  @keyup.enter.native="_search"
                />
              </Form-item>
              <Form-item class="search-btn">
                <el-button type="primary" @click="_search">
                  搜索
                </el-button>
              </Form-item>
            </Form>
          </el-col>
          <!-- 表格 -->
          <el-col :span="24">
            <radioElementTable
              ref="pageTable"
              :table-height="tableHeight"
              :get-page="getPage"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                show-overflow-tooltip
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <div>{{ scope.row[item.key] }}</div>
                </template>
              </el-table-column>
            </radioElementTable>
          </el-col>
        </el-row>
      </div>
      <div slot="footer">
        <modalFooter ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 选择报表（报表生成器生成的）
 */
import { printXml } from '../../../api'
import modalFooter from '../../../components/base/modalFooter'
import radioElementTable from '../../../components/table/radioElementTable'

export default {
  components: {
    modalFooter,
    radioElementTable
  },
  data() {
    return {
      showModal: false,
      getPage: {},
      pageColumns: [
        { title: '报表名称', key: 'name' },
        { title: '服务名称', key: 'service', width: 120 },
        { title: '添加人', key: 'operator', width: 120 }
      ],
      formObj: {
        name: '',
        service: ''
      },
      selectData: [],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this.showModal = false
          break
        case '确定':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open() {
      this.showModal = true
      this.$nextTick(() => {
        this.formObj.name = ''
        this.formObj.service = ''
        this._page()
        this.selectData = []
        this._hideLoading()
        this.$refs.pageTable._clear()
      })
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await printXml.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'radioSelect':
          this.selectData = [data]
          break
        case 'dbSelect':
          this.selectData = [data]
          this._ok()
      }
    },
    _ok() {
      if (this.selectData.length === 0) {
        this.$message.warning('请选择一个报表！')
        this._hideLoading()
      } else {
        this.$emit('on-result-change', this.selectData)
        this.showModal = false
      }
    }
  }
}
</script>
