<template>
  <div>
    <!-- <el-toolbar>
      <el-button @click="publishNewWordToGeneralDBClickHandler" :disabled="selectedRows.length===0">
        <i class="fa fa-cloud-upload"></i>发布新词到通用词库
      </el-button>
      <el-button @click="delClickHandler" :disabled="selectedRows.length===0" type="danger">
        <i class="fa fa-minus-circle"></i>删除用户输入的新词
      </el-button>
    </el-toolbar> -->
    <div class="padding-vertical-max scroll general-dictionary">
      <el-table
        :data="userInput"
        ref="table"
        data-read-url="/ui/userinputword"
        :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="添加时间" width="180" align="center">
          <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="publishNewWordToGeneralDBClickHandler(scope.row)" priv="Search.DicManager.PublishNewWordToGeneralDB" type="text" title="发布新词到通用词库">
              <i class="fa fa-cloud-upload f16"></i></el-button>
            <el-button @click.stop="delClickHandler(scope.row)" priv="Search.DicManager.DeleteUserInputNewWord" type="text" title="删除用户输入的新词">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import './css/dicManager.css'
import util from '@src/common/util.js'
import Privilege from '@src/common/privilege.js'
export default {
  props: { tabName: String },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      selectedRows: [], //  选中行
      submitLoading: false,
      userInput: [], //   用户输入新词请求数据
      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.DeleteUserInputNewWord`) === 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/userinputword/${IDs}`)
          util.showMessage(res)
          if (res.data.status === 1) {
            this.selectedRows = []
            this.loadUserInputWord()
          }
        })
        .catch(err => { console.error(err) })
    },

    // 批量发布
    async publishInputWords(rows) {
      if (Privilege.hasPriv(`Search.DicManager.PublishNewWordToGeneralDB`) === 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/userinputword/publish/${IDs}`)
          util.showMessage(res)
          if (res.data.status === 1) {
            this.selectedRows = []
            this.loadUserInputWord()
          }
        })
        .catch(err => { console.error(err) })
    },

    // 删除用户新词
    async delClickHandler(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/userinputword/${ids.join()}`)
        util.showMessage(res)
        if (res.data.status === 1) {
          this.selectedRows = []
          this.loadUserInputWord()
        }
      } catch (e) {

      }
    },
    // 发布用户新词到通用词库点击事件
    async publishNewWordToGeneralDBClickHandler(row) {
      let ids = row && row.ID ? [row.ID] : util.getSelectedIDs(this.selectedRows)
      if (!ids || !ids.length) {
        return
      }
      try {
        let res = await axios.put(`/ui/userinputword/publish/${ids.join()}`)
        util.showMessage(res)
        if (res.data.status === 1) {
          this.selectedRows = []
          this.loadUserInputWord()
        }
      } catch (e) {
        // util.showErrorNotification(e)

      }
    },
    // 用户输入新词选中事件
    onSelectionChange(selection) {
      this.selectedRows = selection
    },
    // 加载用户输入新词
    loadUserInputWord() {
      this.$refs.table.getData()
    }
  },
  watch: {
    tabName(val) {
      if (val === 'userinput') {
        this.loadUserInputWord()
      }
    }
  }
}
</script>
<style scoped>
</style>
