<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>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="名称">
            <el-input v-model="formData.name" clearable placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="powerSearch">搜索</el-button>
          <el-button type="primary" @click="rest">重置</el-button>
        </el-col>
      </el-row>
    </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 { queryDomaininfo } from '@/api/esb_service'

export default {
  data() {
    return {
      dialogVisible: false,
      tableLoading: false,
      formData: {},
      tableList: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      selectIndex: -1,
      multipleSelection: [],
      filters: []
    }
  },
  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({
        filters: this.filters,
        orderby: 'createon',
        sort: 'desc',
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize
      }).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() {
      this.page.pageNum = 1
      this.filterData(this.formData)
      this.getData()
    },
    // 重置
    rest() {
      this.formData = {}
      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>
