<template>
  <div>
    <el-dialog title="选择积分操作项" :visible.sync="isShow" class="score-opt-selector" @open="beforeDialogOpen">
      <div :style="{'margin-bottom':'10px'}">
        <el-select clearable class="score-type-div" v-model="search.userType">
          <el-option v-for="item in userSelect" :key="item.key" :label="item.value" :value="item.key" :disabled="item.disabled">
          </el-option>
        </el-select>
        <el-button  type="primary" @click="searchClickHandler">搜索</el-button>
      </div>
      <el-table
        :data="scoreConfig"
        highlight-current-row
        v-loading="tableDataLoading"
        @current-change="handleCurrentChange">
        <el-table-column label="" width="60" align="center">
          <template slot-scope="scope">
            <el-radio class="radio" v-model="checked.code" :label="scope.row.code">{{''}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="name"></el-table-column>
        <el-table-column prop="pluginName" width="100"></el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirmSelectScoreConfigClickHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {show: Boolean, code: String, name: String},
  data() {
    return {
      tableDataLoading: false, // 表格数据加载
      search: {userType: ''}, // 查询条件
      userSelect: [], // 积分操作员下拉框
      scoreConfig: [], // 操作项数据
      checked: {code: '', name: ''} // 保存选中的积分项
    }
  },
  computed: {
    isShow: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  methods: {
    beforeDialogOpen() {
      this.search.userType = ''
      this.scoreConfig = []
      this.checked.name = this.name
      this.checked.code = this.code
      this.loadScoreConfig()
      this.loadScoreOperatePlugins()
    },
    // 查询积分操作项
    searchClickHandler() {
      this.loadScoreConfig()
    },
    // 加载积分操作项
    async loadScoreConfig() {
      this.tableDataLoading = true
      try {
        let res = await axios.get('/ui/scoreoperate/scoreconfig', {params: {pluginid: this.search.userType}})
        this.scoreConfig = res.data.data
      } catch (e) {}
      this.tableDataLoading = false
    },
    // 加载积分操作项用户类型
    async loadScoreOperatePlugins() {
      let res = await axios.get('/ui/scoreoperate/plugins')
      this.userSelect = res.data.data
    },
    handleCurrentChange(val) {
      if (val) {
        this.checked.name = val.name
        this.checked.code = val.code
      }
    },
    // 选择积分操作提交
    confirmSelectScoreConfigClickHandler() {
      if (!this.checked.code) {
        this.$alert('请选择操作项', '系统提示', {
          type: 'warning'
        })
      } else {
        this.$emit('callback', this.checked)
        this.isShow = false
      }
    }
  }
}
</script>
<style>
.score-opt-selector .el-dialog{
  width:500px;
}
@media (max-width:480px){
  .score-opt-selector .el-dialog{
    width:96%;
  }
}
</style>
