<template>
   <div class="pane-up-wrap" v-loading="dataLoading">
      <div class="padding-vertical-max">
        <el-table :data="distanceWeightData" tooltip-effect="dark" ref="distanceWeightTable" style="width: 100%" @selection-change="onWeightsSelectionChange">
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column type="selection" align="center" width="80"></el-table-column>
          <el-table-column label="名称">
            <template slot-scope="scope">
              <el-button priv="Search.WeightsConfig.EditDistanceWeights" type="text" class="table-title-button" :title="scope.row.name" @click.stop="editWeightsClickHandler(scope.row)">{{scope.row.name}}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="subType" label="字段代码"></el-table-column>
          <el-table-column prop="weights" label="距离权重" width="200"></el-table-column>
          <el-table-column label="操作" align="right" width="80">
            <template slot-scope="scope">
              <el-button @click.stop="editWeightsClickHandler(scope.row)" priv="Search.WeightsConfig.EditDistanceWeights" type="text" title="编辑">
                <i class="fa fa-edit f16"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
</template>

<script>
export default {
  data() {
    return {
      selectedRows: [],
      distanceWeightData: [], // 字段权重数据
      dataLoading: false,
      currentWeightsdata: {}, // 当前权重临时数据（字段，均衡，距离）
      dialogParams: {
        type: 'DistanceWeights',
        currentWeightsdata: {}
      }
    }
  },
  methods: {
    // 加载距离权重表格数据
    async loadDistanceWeightData() {
      this.dataLoading = true
      try {
        let res = await axios.get('/ui/distanceweightsconfig')
        let data = res.data
        if (data.status === 1) {
          let distanceWeightData = data.data || []
          for (let item of distanceWeightData) {
            let weights = item['weights']
            item['weights'] = !weights ? weights : Number(weights).toLocaleString().replace(/,/g, '')
          }
          this.distanceWeightData = distanceWeightData
        }
      } catch (e) {
      }
      this.dataLoading = false
      this.selectedRows = []
    },

    // 编辑权重按钮点击事件
    editWeightsClickHandler(row) {
      let ID = !row.ID ? 'new' : row.ID
      let path = `${this.$route.path}/${ID}`.replace(/\/\//g, '/')
      if (ID === 'new') {
        let params = {
          subType: row.subType,
          name: row.name,
          weights: row.weights,
          rangeOfAction: row.rangeOfAction,
          enabled: row.enabled
        }
        this.$router.push({path: path, query: params})
      } else {
        this.$router.push({path: path})
      }
    },

    // 权重表格数据行选中事件
    onWeightsSelectionChange(selection) {
      this.selectedRows = selection
      this.currentWeightsdata = this.selectedRows.length ? Object.assign({}, this.selectedRows[0]) : {}
    }

  }
}
</script>
