<template>
  <div class="right-list-toolbar">
    <el-toolbar>
      <el-button @click="extractedNewWordClickHandler" priv="Search.DicManager.ExtractedNewWord">
        <i class="fa fa-refresh"></i>挖掘新词
      </el-button>
    </el-toolbar>
    <div class="scroll general-dictionary">
      <el-table
        ref="table"
        :data="articleWord"
        data-read-url="/ui/articleinputnewword"
        :page-size="20"
        :auto-get-data="false"
        tooltip-effect="dark"
        @selection-change="onSelectionChange"
        :multi-operates="multiOperates"
        >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="文章中挖掘出来的新词">
          <template slot-scope="scope">
            <span :title="scope.row.rootWord">{{scope.row.rootWord}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="addUser" label="添加人" width="120"></el-table-column>
        <el-table-column prop="addTime" label="添加时间" align="center" width="180">
          <template slot-scope="scope">
            {{scope.row.addTime | fixDateTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="right">
          <template slot-scope="scope">
            <el-button @click.stop="publishExtractedNewWordToGeneralDBClickHandler(scope.row)" priv="Search.DicManager.PublishExtractedNewWordToGeneralDB" type="text" title="发布挖掘出来的新词到通用词库">
              <i class="fa fa-cloud-upload f16"></i></el-button>
            <el-button @click.stop="delArticleWordClickHandler(scope.row)" priv="Search.DicManager.DeleteArticleInputNewWord" type="text" title="删除挖掘出来的新词">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 挖掘文章新词 -->
    <extract-article
      :show.sync="isShowModal"
      @callback="afterExtractArticle">
    </extract-article>
  </div>
</template>
<script>
import './css/dicManager.css'

import util from '@src/common/util.js'
import Privilege from '@src/common/privilege.js'
import extractArticleListDialog from './extractArticleListDialog.vue'
export default {
  props: { tabName: String },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      selectedRows: [], //  选中行
      articleWord: [], //  文章中挖掘新词请求数据
      isShowModal: false,
      publishLoading: false,
      multiOperates: [
        {
          label: '删除',
          value: 'delete',
          fn: rows => {
            this.deleteInputWords(rows)
          }
        },
        {
          label: '发布',
          value: 'publish',
          fn: rows => {
            this.publishInputWords(rows)
          }
        }
      ]
    }
  },
  methods: {
    // 批量删除
    async deleteInputWords(rows) {
      if (Privilege.hasPriv(`Search.DicManager.DeleteArticleInputNewWord`) === false) {
        util.showError('您没有操作权限！')
        return
      }

      this.$confirm(`确认删除选择的<span class='icon-danger'>${rows.length}条</span>内容吗 ？`, {
        title: '提示',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      })
        .then(async r => {
          let IDs = _.map(rows, 'ID').join(',')

          let res = await axios.delete(`/ui/articleinputnewword/${IDs}`)
          util.showMessage(res)
          if (res.data.status === 1) {
            this.selectedRows = []
            this.loadArticleInputWord()
          }
        })
        .catch(err => { console.error(err) })
    },

    // 批量发布
    async publishInputWords(rows) {
      if (Privilege.hasPriv(`Search.DicManager.PublishExtractedNewWordToGeneralDB`) === false) {
        util.showError('您没有操作权限！')
        return
      }

      this.$confirm(`确认发布选择的<span class='icon-danger'>${rows.length}条</span>内容吗 ？`, {
        title: '提示',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      })
        .then(async r => {
          let IDs = _.map(rows, 'ID').join(',')

          let res = await axios.put(`/ui/articleinputnewword/publish/${IDs}`)
          this.publishLoading = false
          util.showMessage(res)
          if (res.data.status === 1) {
            this.selectedRows = []
            this.$refs.table.clearSelection()
            this.loadArticleInputWord()
          }
        })
        .catch(err => { console.error(err) })
    },

    handleCommand(method) {
      if (method && typeof this[method] === 'function') {
        this[method]()
      } else {
        console.warn(`未找到改菜单对应的方法:${method}`)
      }
    },
    // 挖掘新词回调
    afterExtractArticle() {
      this.loadArticleInputWord()
    },
    // 发布挖掘出来的新词到通用词库
    async publishExtractedNewWordToGeneralDBClickHandler(row) {
      let ids = row && row.ID ? [row.ID] : util.getSelectedIDs(this.selectedRows)
      if (!ids || !ids.length) {
        return
      }
      this.publishLoading = true
      try {
        let res = await axios.put(`/ui/articleinputnewword/publish/${ids.join()}`)
        this.publishLoading = false
        util.showMessage(res)
        if (res.data.status === 1) {
          this.selectedRows = []
          this.$refs.table.clearSelection()
          this.loadArticleInputWord()
        }
      } catch (e) {
        this.publishLoading = false
        // util.showErrorNotification(e)
      }
    },
    // 挖掘新词点击事件
    extractedNewWordClickHandler() {
      this.isShowModal = true
    },
    // 删除挖掘出来的新词
    async delArticleWordClickHandler(row) {
      let ids = row && row.ID ? [row.ID] : util.getSelectedIDs(this.selectedRows)
      if (!ids || !ids.length) {
        return
      }
      try {
        await this.$confirm('确认删除？', '系统提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.delete(`/ui/articleinputnewword/${ids.join()}`)
        util.showMessage(res)
        if (res.data.status === 1) {
          this.selectedRows = []
          this.loadArticleInputWord()
        }
      } catch (e) {

      }
    },
    // 文章新词选择事件
    onSelectionChange(selection) {
      this.selectedRows = selection
    },
    // 加载文章挖掘新词
    loadArticleInputWord() {
      this.$refs.table.getData()
    }
  },
  watch: {
    tabName: function(val) {
      if (val === 'articleinput') {
        this.loadArticleInputWord()
      }
    }
  },
  components: {
    'extract-article': extractArticleListDialog
  }
}
</script>
<style scoped>
</style>
