<template>
   <div class="pane-up-wrap right-list-toolbar">
      <el-toolbar>
        <el-button @click="addCustomWeight" priv="Search.WeightsConfig.AddCustomConfig">
          <i class="fa fa-plus"></i> 新建
        </el-button>
      </el-toolbar>
      <div>
        <el-table data-read-url="/ui/customweights" :page-size="10" :auto-get-data="false" pagination-layout="sizes, ->, prev,pager, next, jumper" ref="datatable"  style="width: 100%" @selection-change="onSelectionChange">
          <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.EditCustomConfig" type="text" class="table-title-button" :title="scope.row.name" @click.stop="editCustomWeight(scope.row)">{{scope.row.name}}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="fieldValue" label="字段值" show-overflow-tooltip></el-table-column>
          <el-table-column prop="weights" :formatter="weightsFormatter" label="自定义权重" width="200"></el-table-column>
          <el-table-column label="操作" align="right" width="100">
            <template slot-scope="scope">
              <el-button @click.stop="editCustomWeight(scope.row)" priv="Search.WeightsConfig.EditCustomConfig" type="text" title="编辑">
                <i class="fa fa-edit f16"></i>
              </el-button>
              <el-button @click.stop="deleteCustomWeight(scope.row)" priv="Search.WeightsConfig.DeleteCustomConfig" type="text" title="删除">
                <i class="fa fa-remove f16"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
</template>

<script>
import util from '@src/common/util.js'

export default {
  data() {
    return {
      selectedRows: [],
      customWeightsData: []
    }
  },
  methods: {
    weightsFormatter(row, column, cellValue) {
      return !cellValue ? cellValue : Number(cellValue).toLocaleString().replace(/,/g, '')
    },
    // 加载自定义权重表格数据
    loadCustomWeightsData() {
      !!this.$refs.datatable && this.$refs.datatable.getData()
    },

    // 新建自定义权重按钮点击事件
    async addCustomWeight() {
      let path = `${this.$route.path}/new`.replace(/\/\//g, '/')
      this.$router.push(path)
    },

    // 编辑自定义权重按钮点击事件
    editCustomWeight(row) {
      if (!row.ID) {
        this.$alert('ID错误 !', '系统提示', {type: 'info'})
        return
      }
      let path = `${this.$route.path}/${row.ID}`.replace(/\/\//g, '/')
      this.$router.push(path)
    },

    // 删除自定义权重按钮点击事件
    async deleteCustomWeight(row) {
      let ids = !!row && row.ID ? Array.of(row.ID) : util.getSelectedIDs(this.selectedRows)
      try {
        await this.$confirm('确认删除？', '系统提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.delete(`/ui/customweights/${ids.join()}`)
        util.showMessage(res)
        if (res.data.status === 1) {
          this.loadCustomWeightsData()
        }
      } catch (e) {

      }
    },

    // 自定义权重表格数据行选中事件
    onSelectionChange(selection) {
      this.selectedRows = selection
      this.currentCutomWeightsdata = this.selectedRows.length ? Object.assign({}, this.selectedRows[0]) : {}
    }
  }
}
</script>
