<template>
  <div class="right-list-toolbar">
    <el-toolbar>
      <el-button @click="addWrongClickHandler" priv="WordManage.Wrong">
        <i class="fa fa-plus"></i>新建
      </el-button>
      <el-col :span="10" class="search-box-r tool-search-box-r">
        <el-input v-model="searchWrong.word" @keyup.enter.native="searchWrongClickHandler" placeholder="请输入易错词" class="search-bar-algin-toolbar"></el-input>
        <span class="search-icon"><i class="fa fa-search" @click="searchWrongClickHandler" title="查询"></i></span>
      </el-col>
    </el-toolbar>
    <div>
      <el-table ref="refWrongTable" :data="wrongTable" :data-read-url="wrongTableList" :page-size="10" :auto-get-data="false" tooltip-effect="dark"
        @selection-change="onWrongSelectionChange" :multi-operates="multiOperates">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="65" align="center"></el-table-column>
        <el-table-column label="易错词">
          <template slot-scope="scope">
            <el-button type="text" class="table-title-button" :title="scope.row.word" priv="WordManage.Wrong" @click.stop="edit(scope.row)">{{scope.row.word}}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="replaceWord" label="替换词"></el-table-column>
        <el-table-column prop="addUser" label="添加用户"></el-table-column>
        <el-table-column prop="addTime" label="添加时间">
          <template slot-scope="scope">
            {{scope.row.addTime | fixDateTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="right">
          <template slot-scope="scope">
            <el-button @click.stop="edit(scope.row)" type="text" title="编辑" priv="WordManage.Wrong">
              <i class="fa fa-edit f16"></i></el-button>
            <el-button @click.stop="del(scope.row)" type="text" title="删除" priv="WordManage.Wrong">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--易错词Dialog-->
    <wrong-word-dialog :add-wrong-dialog.sync="addWrongDialog" :tmp-wrong="tmpWrong" @callback="init"></wrong-word-dialog>
  </div>
</template>

<script>
import util from '@src/common/util.js'
import Privilege from '@src/common/privilege.js'
import wrongWordDialog from './wrongWordDialog.vue'

export default {
  components: {
    'wrong-word-dialog': wrongWordDialog
  },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      // 当前选中行
      selectedRows: [],

      // 易错词列表
      wrongTable: [],

      // 易错词新建、编辑对话框
      addWrongDialog: false,

      // 易错词form表单定义
      tmpWrong: {
        word: '',
        replaceWord: ''
      },

      // 易错词搜索
      searchWrong: {
        word: ''
      },

      multiOperates: [
        {
          label: '删除',
          value: 'delete',
          fn: rows => {
            this.deleteWrongWords(rows)
          }
        }
      ]
    }
  },

  methods: {
    // 批量删除
    async deleteWrongWords(rows) {
      if (Privilege.hasPriv(`WordManage.Wrong`) === false) {
        util.showError('您没有操作权限！')
        return
      }

      this.$confirm(`确认删除选择的<span class='icon-danger'>${rows.length}条</span>内容吗 ？`, {
        title: '提示',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      })
        .then(async res => {
          let IDs = _.map(rows, 'ID').join(',')
          let resp = await axios.delete(`/ui/wrongwords/${IDs}`)
          if (resp.data.status === 1) {
            util.showMessage(resp)
            this.init()
          } else {
            util.showErrorMessageBox(resp.data.message)
          }
        })
        .catch(err => { console.error(err) })
    },

    // 数据选中事件
    onWrongSelectionChange(selection) {
      this.selectedRows = selection
    },

    // 易错词搜索事件
    searchWrongClickHandler() {
      this.init()
    },

    // 刷新表格数据
    init() {
      this.$refs.refWrongTable.getData()
    },

    // 新建易错词事件
    addWrongClickHandler() {
      this.tmpWrong = {
        word: '',
        replaceWord: ''
      }
      this.addWrongDialog = true
      this.dataLoading = false
    },

    edit(row) {
      this.tmpWrong = Object.assign({}, row)
      this.addWrongDialog = true
    },

    // 删除易错词事件
    async batchDel() {
      try {
        await this.$confirm('确定删除吗？删除后无法恢复。是否继续删除？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch (e) {
        return
      }
      try {
        let ids = []
        this.selectedRows.forEach(function(row) {
          ids.push(row.ID)
        })
        let res = await axios.delete(`/ui/wrongwords/${ids}`)
        if (res.data.status === 1) {
          util.showMessage(res)
          this.init()
        } else {
          util.showErrorMessageBox(res.data.message)
        }
      } catch (e) {
        util.showErrorNotification(e)
      }
    },

    del(row) {
      this.selectedRows = [row]
      this.batchDel()
    },

    // 易错词列表数据加载
    wrongTableList() {
      return [
        '/ui/wrongwords',
        {
          params: {
            word: encodeURIComponent(this.searchWrong.word)
          }
        }
      ]
    }
  }
}
</script>

<style scoped>
.page {
  float: right;
}
</style>
