<template>
  <div class="right-list-toolbar">
    <el-toolbar>
      <el-button @click="addSourceClickHandler" priv="WordManage.Source">
        <i class="fa fa-plus"></i>新建
      </el-button>
      <el-col :span="10" class="search-box-r tool-search-box-r">
        <el-input v-model="searchSource.sourceWord" @keyup.enter.native="searchSourceClickHandler" placeholder="请输入来源" class="search-bar-algin-toolbar"></el-input>
        <span class="search-icon"><i class="fa fa-search" @click="searchSourceClickHandler" title="查询"></i></span>
      </el-col>
    </el-toolbar>
    <div>
      <el-table ref="refSourceTable" :data="sourceTable" :data-read-url="sourceTableList" :page-size="10" :auto-get-data="false" tooltip-effect="dark"
        @selection-change="onSourceSelectionChange" :multi-operates="multiOperates">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column width="65" type="index" label="序号" align="center"></el-table-column>
        <el-table-column label="来源">
          <template slot-scope="scope">
            <el-button type="text" class="table-title-button" :title="scope.row.sourceWord" priv="WordManage.Source" @click.stop="edit(scope.row)">{{scope.row.sourceWord}}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="firstLetters" 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.Source">
              <i class="fa fa-edit f16"></i></el-button>
            <el-button @click.stop="del(scope.row)" type="text" title="删除" priv="WordManage.Source">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <source-word-dialog :add-source-dialog.sync="addSourceDialog" :tmp-source="tmpSource" @callback="init"></source-word-dialog>
  </div>
</template>

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

export default {
  components: {
    'source-word-dialog': sourceWordDialog
  },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      // 来源列表
      sourceTable: [],

      // 来源对话框显示状态
      addSourceDialog: false,

      // 来源form表单定义
      tmpSource: {
        sourceWord: '',
        firstLetters: ''
      },

      // 当前选择行
      selectedRows: [],

      // 来源搜索
      searchSource: {
        sourceWord: ''
      },

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

  methods: {
    // 批量删除
    async deleteSourceWords(rows) {
      if (Privilege.hasPriv(`WordManage.Source`) === 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/sourcewords/${IDs}`)
          if (resp.data.status === 1) {
            util.showMessage(resp)
            this.init()
          } else {
            util.showErrorMessageBox(resp.data.message)
          }
        })
        .catch(err => { console.error(err) })
    },

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

    // 新建来源事件
    addSourceClickHandler() {
      this.tmpSource = {
        sourceWord: '',
        firstLetters: ''
      }
      this.addSourceDialog = true
    },

    edit(row) {
      this.tmpSource = Object.assign({}, row)
      this.addSourceDialog = 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 strIds = ids.join(',')
        let res = await axios.delete(`/ui/sourcewords/${strIds}`)
        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()
    },

    // 来源搜索事件
    searchSourceClickHandler() {
      this.init()
    },

    sourceTableList() {
      return [
        '/ui/sourcewords',
        {
          params: {
            word: encodeURIComponent(this.searchSource.sourceWord)
          }
        }
      ]
    },

    // 刷新表格数据
    init() {
      this.$refs.refSourceTable.getData()
    }
  }
}
</script>
