<template>
  <div class="table-wrap">
    <el-table
      ref="proxyList"
      :data="proxyList"
      v-loading="tableLoadingProxy"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
      highlight-current-row
      style="width: 100%; margin-bottom: 10px"
      size="mini"
      @current-change="handleCurrentChange($event, 'proxyList')">
      <el-table-column
        type="index"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="type"
        label="资源类型"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="选择" width="60" align="center">
        <template slot-scope="scope">
          <el-radio v-model="proxyListSelectIndex" :label="scope.$index" class="radioEmpty">{{''}}</el-radio>
        </template>
      </el-table-column>
    </el-table>
    <pagination :page="pageProxy" @requestData="queryProxyService"/>
  </div>
</template>

<script>
import { queryProxyService, queryBusinessService, queryFlow } from '@/api/esb_service'
export default {
  name: 'ChooseGoodsDialog',
  data() {
    return {
      dialogVisible: false,
      activeName: 'proxy',
      tableLoadingProxy: false,
      tableLoadingBusiness: false,
      tableLoadingFlow: false,
      formData: {},
      tableList: [],
      proxyList: [],
      businessList: [],
      flowList: [],
      pageProxy: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      pageBusiness: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      pageFlow: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      proxyListSelectIndex: -1,
      businessListSelectIndex: -1,
      flowListSelectIndex: -1,
      selectList: null
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.queryProxyService()
        this.queryBusinessService()
        this.queryFlow()
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    // 搜索
    powerSearch() {
      this.pageProxy.pageNum = 1
      this.pageBusiness.pageNum = 1
      this.pageFlow.pageNum = 1
      this.filterData(this.formData)
      this.queryProxyService()
      this.queryBusinessService()
      this.queryFlow()
    },
    // 重置
    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)
        }
      })
    },
    queryProxyService() {
      this.tableLoadingProxy = true
      this.proxyListSelectIndex = -1
      queryProxyService({
        filters: this.filters,
        orderby: 'createon',
        sort: 'desc',
        pageNum: this.pageProxy.pageNum,
        pageSize: this.pageProxy.pageSize
      }).then((rt) => {
        this.tableLoadingProxy = false
        if (rt.status === '200') {
          this.proxyList = rt.data.list || []
          this.pageProxy.total = rt.data.total
        }
      }).catch(e => {
        this.tableLoadingProxy = false
      })
    },
    queryBusinessService() {
      this.tableLoadingBusiness = true
      this.businessListSelectIndex = -1
      queryBusinessService({
        filters: this.filters,
        orderby: 'createon',
        sort: 'desc',
        pageNum: this.pageBusiness.pageNum,
        pageSize: this.pageBusiness.pageSize
      }).then((rt) => {
        this.tableLoadingBusiness = false
        if (rt.status === '200') {
          this.businessList = rt.data.list || []
          this.pageBusiness.total = rt.data.total
        }
      }).catch(e => {
        this.tableLoadingBusiness = false
      })
    },
    queryFlow() {
      this.tableLoadingFlow = true
      this.flowListSelectIndex = -1
      queryFlow({
        filters: this.filters,
        orderby: 'createon',
        sort: 'desc',
        pageNum: this.pageFlow.pageNum,
        pageSize: this.pageFlow.pageSize
      }).then((rt) => {
        this.tableLoadingFlow = false
        if (rt.status === '200') {
          this.flowList = rt.data.list || []
          this.pageFlow.total = rt.data.total
        }
      }).catch(e => {
        this.tableLoadingFlow = false
      })
    },
    // 单选
    handleCurrentChange(val, list) {
      if (val) {
        this.selectList = list
        this.proxyListSelectIndex = -1
        this.businessListSelectIndex = -1
        this.flowListSelectIndex = -1
        if (list === 'proxyList') {
          this.$refs.businessList.setCurrentRow()
          this.$refs.flowList.setCurrentRow()
        } else if (list === 'businessList') {
          this.$refs.proxyList.setCurrentRow()
          this.$refs.flowList.setCurrentRow()
        } else if (list === 'flowList') {
          this.$refs.proxyList.setCurrentRow()
          this.$refs.businessList.setCurrentRow()
        }
        this[`${this.selectList}SelectIndex`] = this[this.selectList].findIndex(item => item.id === val.id)
      }
    },
    submit() {
      let data
      if (this.proxyListSelectIndex >= 0) {
        data = JSON.parse(JSON.stringify(this[this.selectList][this.proxyListSelectIndex]))
      } else if (this.businessListSelectIndex >= 0) {
        data = JSON.parse(JSON.stringify(this[this.selectList][this.businessListSelectIndex]))
      } else if (this.flowListSelectIndex >= 0) {
        data = JSON.parse(JSON.stringify(this[this.selectList][this.flowListSelectIndex]))
      } else {
        this.$message({
          message: '请选择',
          type: 'error'
        })
        return
      }
      this.$emit('confirm', data)
      this.dialogVisible = false
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
</style>
