<template>

  <div class="layout-no-cols right-list-toolbar">
    <el-toolbar>
      <el-col :span="14">
        <el-button @click="addClick" priv="DimensionManage.Add">
          <i class="fa fa-plus"></i>新建
        </el-button>
      </el-col>
      <el-col :span="10" class="search-box-r tool-search-box-r">
        <el-input v-model="searchs.name" placeholder="请输入名称" class="search-bar-algin-toolbar" @keyup.enter.native="searchClickHandler"></el-input>
        <span class="search-icon"><i class="fa fa-search" @click="searchClickHandler" title="搜索"></i></span>
      </el-col>
    </el-toolbar>
    <div>
      <el-table ref="refDimensTable"
        :data-read-url="dimensData"
        class="dimensionality-table"
        :page-size="20"
        tooltip-effect="dark">
        <el-table-column label="序号" type="index" width="60" align="center"></el-table-column>
        <el-table-column label="名称">
          <template slot-scope="scope">
            <a class="list-card-title" href="javascript:void(0)" @click.stop="editClick(scope.row.ID)">
              <i :class="scope.row.icon" aria-hidden="true"></i> {{scope.row.name}}
            </a>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="别名" width="120"></el-table-column>
        <el-table-column prop="isOpenName" label="是否可用" align="center" width="80">
          <template slot-scope="scope">
            <i class="fa fa-check icon-success" v-if="scope.row.isOpen==='Y'"></i>
            <i class="fa fa-times icon-danger" v-else></i>
          </template>
        </el-table-column>
        <el-table-column prop="addUser" label="添加用户" width="180" align="center"></el-table-column>
        <el-table-column prop="addTime" label="添加时间" width="180" align="center">
          <template slot-scope="scope">
            {{scope.row.addTime|fixDateTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="136" align="right">
          <template slot-scope="scope">
            <el-button @click.stop="editClick(scope.row.ID)" type="text" priv="DimensionManage.Edit" title="编辑">
              <i class="fa fa-edit f16"></i></el-button>
            <el-button @click.stop="deleteClick(scope.row.ID)" type="text" priv="DimensionManage.Delete" title="删除">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import util from '@src/common/util.js'
export default {
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      searchs: {
        name: ''
      }, // 搜索定义
      selectedRows: [] // 当前选中行
    }
  },
  methods: {
    // 选中事件
    onSelectionChange(selection) {
      this.selectedRows = selection
    },
    // 搜索事件
    searchClickHandler() {
      this.dimensList()
    },
    // 编辑维度信息
    editClick(ID) {
      this.$router.push({path: `/settings/dimensionalities/${ID}`})
    },
    // 添加维度信息
    addClick() {
      this.$router.push({path: `/settings/dimensionalities/new`})
    },
    // 加载维度管理列表数据
    dimensData() {
      return ['/ui/dimens', {params: {name: encodeURI(this.searchs.name)}}]
    },
    // 刷新表格数据
    dimensList() {
      this.$refs.refDimensTable.getData()
    },
    // 删除维度信息
    async deleteClick(ID) {
      try {
        await this.$confirm('确定删除吗？删除后无法恢复。是否继续删除？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch (e) {
        return
      }
      try {
        let res = await axios.delete(`/ui/dimens/${ID}`)
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.dimensList()
        }
      } catch (e) {
        // util.showErrorNotification(e)

      }
    }
  }
}
</script>
<style scoped>
.dimensionality-table .list-card-title {
  font-size: 14px;
}
</style>
