<template>
  <el-dialog :close-on-click-modal="false" title="选择域" :visible.sync="dialogVisible" width="800px" top="5vh" append-to-body @close="closeDialog">
    <el-form :model="formData" label-width="50px" @submit.native.prevent>

      <div class="search-header">
        <div class="tool-common-wrap">
          <div class="search-wrap">
            <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="powerSearch" />
          </div>
          <div class="btn-wrap">
            <el-button type="primary" @click="rest()"> 刷新</el-button>
          </div>
        </div>
      </div>
    </el-form>
    <div class="tableWrap">
      <el-table
        :data="tableList"
        v-loading="tableLoading"
        highlight-current-row
        style="width: 100%; margin-bottom: 10px"
        size="mini"
        @current-change="handleCurrentChange">
        <el-table-column type="index" label="序号" width="60" align="center">
        </el-table-column>
        <el-table-column prop="name" label="名称" align="center" width="200px" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="description" label="说明" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="选择" width="60" align="center">
          <template slot-scope="scope">
            <el-radio v-model="selectIndex" :label="scope.$index" class="radioEmpty">{{''}}</el-radio>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="page.pageSize"
        :current-page.sync="page.pageNum"
        background
        @current-change="getData()"
        :total="page.total"></el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import QuickQuery from '@/components/quickquery/QuickQuery'
import { queryDomaininfo } from '@/api/esb_service'

export default {
  components: {
    QuickQuery
  },
  data() {
    return {
      dialogVisible: false,
      tableLoading: false,
      formData: {},
      tableList: [],
      page: {
        pageSize: 5,
        pageNum: 1,
        total: 0
      },
      selectIndex: -1,
      multipleSelection: [],
      filters: [],
      quickQueryKeys: ['name', 'description'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称、描述',
      logic: 'or'
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    connectorId: {
      type: String,
      default: ''
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.getData()
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  mounted() {
  },
  methods: {
    getData() {
      this.tableLoading = true
      this.selectIndex = -1
      queryDomaininfo({
        logic: this.logic,
        filters: this.filters,
        ...this.sort,
        ...this.page
      }).then((rt) => {
        this.tableLoading = false
        if (rt.status === '200') {
          this.tableList = rt.data.list || []
          this.tableList.forEach((item, index) => {
            if (this.connectorId && this.connectorId === item.id) {
              this.selectIndex = index
            }
          })
          this.page.total = rt.data.total
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 搜索
    powerSearch(data) {
      this.filters = []
      this.page.pageNum = 1
      this.quickQueryKeys.forEach(key => {
        const filter = {
          key,
          opt: 'LIKE',
          type: 'S',
          value: data.searchData
        }
        if (filter.value) {
          this.filters.push(filter)
        }
      })
      this.getData()
    },
    // 重置
    rest() {
      if (this.$refs.quick) {
        this.$refs.quick.quickReset() // 清除快速查询
      }
      this.powerSearch({})
    },
    // 搜索过滤
    filterData(obj) {
      this.filters = []
      Object.keys(obj).forEach(key => {
        const filter = {
          key,
          opt: 'LIKE',
          type: 'S',
          value: obj[key]
        }
        if (filter.value) {
          this.filters.push(filter)
        }
      })
    },
    // 单选
    handleCurrentChange(val) {
      if (val) {
        this.connectorId = ''
        this.selectIndex = this.tableList.findIndex(item => item.id === val.id)
      }
    },
    submit() {
      if (this.selectIndex >= 0) {
        const data = JSON.parse(JSON.stringify(this.tableList[this.selectIndex]))
        this.$emit('confirm', data)
        this.dialogVisible = false
      } else {
        this.$message({
          message: '请选择',
          type: 'error'
        })
      }
    },
    // 关闭dislog
    closeDialog() {
      this.formData = {}
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
</style>
