<template>
  <div class="right-list-toolbar">
    <el-toolbar>
      <el-col :span="14">
        <el-button @click="addTagClickHandler" priv="WordManage.Tag">
          <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="searchTag.tagWord" @keyup.enter.native="searchTagClickHandler" placeholder="请输入Tag词" class="search-bar-algin-toolbar"></el-input>
        <span class="search-icon"><i class="fa fa-search" @click="searchTagClickHandler" title="查询"></i></span>
      </el-col>

    </el-toolbar>
    <div>
      <el-table ref="refTagTable" :data="tagTable" :data-read-url="tagTableList" :page-size="10" tooltip-effect="dark" @selection-change="onTagSelectionChange" :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="Tag词">
          <template slot-scope="scope">
            <el-button type="text" class="table-title-button" :title="scope.row.tagWord" priv="WordManage.Tag" @click.stop="edit(scope.row)">{{scope.row.tagWord}}
            </el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="contentTypeName" label="内容类型"></el-table-column> -->
        <el-table-column prop="useCount" 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.Tag">
              <i class="fa fa-edit f16"></i></el-button>
            <el-button @click.stop="del(scope.row)" type="text" title="删除" priv="WordManage.Tag">
              <i class="fa fa-times f16"></i></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <tag-word-dialog :add-tag-dialog.sync="addTagDialog" :content-types="contentTypes" :tmp-tag="tmpTag" @callback="init"></tag-word-dialog>

  </div>
</template>

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

export default {
  components: {
    'tag-word-dialog': tagWordDialog
  },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      // tag词对话框显示状态
      addTagDialog: false,

      // tag词列表
      tagTable: [],

      // 内容类型
      contentTypes: [],

      // tag词form表单定义
      tmpTag: {
        tagWord: '',
        contentType: '',
        useCount: ''
      },

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

      // tag词搜索
      searchTag: {
        contentType: '',
        tagWord: ''
      },
      multiOperates: [
        {
          label: '删除',
          value: 'delete',
          fn: rows => {
            this.deleteTagWords(rows)
          }
        }
      ]
    }
  },

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

    // 新建Tag词事件
    addTagClickHandler() {
      this.tmpTag = {
        tagWord: '',
        contentType: '',
        useCount: ''
      }
      this.addTagDialog = true
    },

    edit(row) {
      this.tmpTag = Object.assign({}, row)
      this.addTagDialog = true
    },

    // 删除Tag词事件
    async batchDel() {
      try {
        await this.$confirm('确定删除吗？删除后无法恢复。是否继续删除？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch (e) {
        return
      }
      try {
        let ids = []
        this.tagSelectedRows.forEach(function(row) {
          ids.push(row.ID)
        })
        let strIds = ids.join(',')
        let res = await axios.delete(`/ui/tagwords/${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.tagSelectedRows = [row]
      this.batchDel()
    },

    // Tag词搜索事件
    searchTagClickHandler() {
      this.init()
    },
    // 数据选中事件
    onTagSelectionChange(selection) {
      this.tagSelectedRows = selection
    },

    async getContentTypes() {
      let type = await axios.get('/ui/contentcore/contenttypes')
      this.contentTypes = type.data.data
    },

    // Tag词列表数据加载
    tagTableList() {
      return [
        '/ui/tagwords',
        {
          params: {
            contentType: this.searchTag.contentType,
            word: encodeURIComponent(this.searchTag.tagWord)
          }
        }
      ]
    },

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

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

.search-bar {
  text-align: right;
}

.el-select {
  width: 133px;
}
</style>
