<template>
  <div>
      <el-dialog
        v-dialogDrag
        :visible.sync="showModal"
        :close-on-click-modal="false"
        title="选择表单"
        width="1000px"
      >
      <div>
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form id="search-report-xml" inline onsubmit="return false" :label-width="70">
              <label class="label-sign"></label>
              <Form-item label="表单名称:">
                <el-input v-model="formObj.name" placeholder="请输入表单名称" @keyup.enter.native="_search" style="width: 200px" clearable/>
              </Form-item>
              <Form-item label="表单标识:">
                <el-input
                  v-model="formObj.uri"
                  placeholder="请输入表单标识"
                  @keyup.enter.native="_search"
                  style="width: 200px"
                  clearable/>
              </Form-item>
              <Form-item label="备注:">
                <el-input v-model="formObj.remark" placeholder="请输入备注" @keyup.enter.native="_search" style="width: 200px" clearable/>
              </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
              :tableHeight="tableHeight"
              @on-result-change="_tableResultChange"
              ref="pageTable"
              :getPage="getPage">
              <el-table-column
                show-overflow-tooltip
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :fixed="item.fixed?item.fixed:undefined"
                v-for="item in pageColumns"
                :key="item.key">
                <template slot-scope="scope">
                  <div>{{scope.row[item.key]}}</div>
                </template>
              </el-table-column>
            </radioElementTable>
          </el-col>
        </el-row>
      </div>
      <div slot="footer">
        <modalFooter @on-result-change="_footerResult" :footer="footerList" ref="footerModal"></modalFooter>
      </div>
      </el-dialog>
  </div>
</template>
<script>
/**
 * 选择表单
 */
import { sysForm } 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: 'uri' },
        { title: '备注', key: 'remark' }
      ],
      formObj: {
        name: '',
        uri: '',
        remark: ''
      },
      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.formObj = this.$resetFields(this.formObj)
      this.showModal = true
      this.selectData = []
      this.$nextTick(() => {
        this._hideLoading()
        this._page()
        this.$refs.pageTable._clear()
      })
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysForm.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>
