<template>
  <el-dialog :close-on-click-modal="false" class="scrollbar-dialog" title="选择服务资源" :visible.sync="dialogVisible" width="900px" top="5vh" append-to-body>
    <el-scrollbar class="layer-content">
      <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" placeholder="请输入" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-button type="primary" @click="powerSearch">搜索</el-button>
            <el-button type="primary" @click="rest">重置</el-button>
          </el-col>
        </el-row>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="代理服务" name="proxy"></el-tab-pane>
          <el-tab-pane label="业务服务" name="business"></el-tab-pane>
          <el-tab-pane label="消息流" name="flow"></el-tab-pane>
        </el-tabs>
        <div class="formGroup">
          <h4 class="margin-b5">代理服务</h4>
          <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>
        </div>
        <div class="formGroup">
          <h4 class="margin-b5">业务服务</h4>
          <div class="table-wrap">
            <el-table
              ref="businessList"
              :data="businessList"
              v-loading="tableLoadingBusiness"
              :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, 'businessList')">
              <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="businessListSelectIndex" :label="scope.$index" class="radioEmpty">{{''}}</el-radio>
                </template>
              </el-table-column>
            </el-table>
            <pagination :page="pageBusiness" @requestData="queryBusinessService"/>
          </div>
        </div>
        <div class="formGroup">
          <h4 class="margin-b5">消息流</h4>
          <div class="tableWrap">
            <el-table
              ref="flowList"
              :data="flowList"
              v-loading="tableLoadingFlow"
              :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, 'flowList')">
              <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="flowListSelectIndex" :label="scope.$index" class="radioEmpty">{{''}}</el-radio>
                </template>
              </el-table-column>
            </el-table>
            <pagination :page="pageFlow" @requestData="queryFlow"/>
          </div>
        </div>
      </el-form>
    </el-scrollbar>
    <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 { 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>
