<template>
  <div>
    <el-row>
      <el-col>
        <el-button @click="add()" type="success" style="margin-left: 20px;" size="mini"><i
            class="el-icon-plus"></i>
          新增
        </el-button>
        <span style="margin: 8px;font-size: 14px;color: #606266">
          显示敏感词
          <el-switch v-model="showSensitiveWord" @change="refreshPage"></el-switch>
        </span>
        <div style="text-align: right;float:right;">
          <el-input style="width:250px;" size="mini" v-model="queryWord"
                    placeholder="关键词"></el-input>
          <el-select
              v-model="queryCategory"
              placeholder="类别"
              style="width:100px;margin-left:20px"
              size="mini">
            <el-option
                v-for="cate in categoryOptions"
                :key="cate.value"
                :label="cate.label"
                :value="cate.value">
            </el-option>
          </el-select>
          <el-select
              v-model="queryLanguage"
              placeholder="语言"
              style="width:100px;margin-left:20px"
              size="mini">
            <el-option
                v-for="region in languageOptions"
                :key="region.value"
                :label="region.label"
                :value="region.value">
            </el-option>
          </el-select>
          <el-button @click="query" type="primary" size="mini" style="margin-left:20px"><i
              class="el-icon-search"></i> 查询
          </el-button>
          <el-button @click="resetSearch" type="info" size="mini" style="margin-left:20px"><i
              class="el-icon-refresh"></i> 重置
          </el-button>
        </div>
        <myTable :metaData="metadata" :tableData="tableData" :isLoading="isLoading"
                 @selection-change="handleSelectionChange">
          <template #default="{ index }">
            <el-button-group>
              <el-button size="small" icon="fa fa-edit" @click="edit(tableData[index])"></el-button>
              <el-button size="small" icon="fa fa-trash" @click="deleteOne(tableData[index].id)"></el-button>
            </el-button-group>
          </template>
        </myTable>
        <el-pagination
            style="padding: 15px"
            background
            layout="total, prev, pager, next, jumper"
            :total="total"
            :current-page="pageIndex"
            @current-change="onPageChange"
        ></el-pagination>
      </el-col>
    </el-row>
    <el-dialog :title="dialogTitle"
               :visible="dialogVisible"
               width="450px"
               center
               :destroy-on-close="true"
               :show-close="false"
    >
      <div class="dialogForm">
        <el-form :model="form" :label-width="formLabelWidth">
          <el-form-item label="关键词">
            <el-input v-model="form.word" size="small" autocomplete="off" :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select
                v-model="form.category" size="small"
                placeholder="请选择"
                collapse-tags
                :style="{width:formInputWidth}"
            >
              <el-option
                  v-for="item in categoryOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="语言">
            <el-select
                v-model="form.lang" size="small"
                placeholder="请选择"
                collapse-tags
                :style="{width:formInputWidth}"
            >
              <el-option
                  v-for="item in languageOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select
                v-model="form.delFlag" size="small"
                placeholder="请选择"
                collapse-tags
                :style="{width:formInputWidth}"
            >
              <el-option
                  v-for="item in delFlagOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button size="small" type="success" @click="submit" :loading="saveLoading">保存</el-button>
          <el-button style="margin-left: 10px;" size="small" type="primary" @click="btn_close">关闭</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapState, mapActions, mapMutations} from 'vuex'
import Vue from 'vue'
import {Card, Col, Button, Input, Checkbox, Radio, Select, Option, Pagination, Message} from 'element-ui'
import myTable from "../components/CustomTable";
import metaDictApi from "../../js/api/metaDictApi";
import wordBankApi from "../../js/api/wordBankApi"

Vue.use(Card);
Vue.use(Button);
Vue.use(Input);
Vue.use(Checkbox);
Vue.use(Col);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Option);
Vue.use(Pagination);

export default {
  name: "WordBankTab",
  components: {
    'myTable': myTable
  },
  data() {
    return {
      modify: false,
      pageIndex: 1,
      queryWord: "",
      queryCategory: "",
      queryRegion: "",
      queryLanguage: "",
      categoryOptions: [],
      languageOptions: [],
      delFlagOptions: [{
        label: "启用", value: 0
      }, {
        label: "不启用", value: 1
      }],
      multipleSelection: [],
      showSensitiveWord: false,
      dialogVisible: false,
      dialogTitle: "",
      form: {
        word: "",
        category: "",
        lang: "",
        delFlag: 0
      },
      formLabelWidth: "60px",
      formInputWidth: "200px",
      saveLoading: false,
      deleteLoading: false
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    onPageChange(index) {
      this.pageIndex = index;
      let dataBody = {};
      if (this.queryWord.length > 0) {
        dataBody.word = this.queryWord;
      }
      if (this.queryCategory.length > 0) {
        dataBody.category = this.queryCategory;
      }
      if (this.queryLanguage.length > 0) {
        dataBody.lang = this.queryLanguage;
      }
      this.queryPageable({
        index: index - 1,
        size: 10,
        data: dataBody,
        showSensitiveWord: this.showSensitiveWord
      });
    },
    add() {
      this.dialogTitle = "添加关键词";
      this.saveLoading = false;
      this.dialogVisible = true;
      this.resetFormData();
    },
    edit(data) {
      this.dialogTitle = "修改关键词";
      this.form = data;
      this.saveLoading = false;
      this.dialogVisible = true;
    },
    submit() {
      this.saveLoading = true;
      if (this.form.id === undefined) {
        wordBankApi.add(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("添加成功")
          } else {
            this.closeDialog();
            Message.error("添加失败")
          }
        })
      } else {
        wordBankApi.update(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("更新成功")
          } else {
            this.closeDialog();
            Message.error("更新失败")
          }
        })
      }
    },
    deleteOne(id) {
      wordBankApi.deleteByIds([id], (response) => {
        if (response.data.code === 200) {
          Message.success("删除成功")
          this.refreshPage()
        } else {
          this.refreshPage()
        }
      })
    },
    closeDialog() {
      this.saveLoading = false;
      this.dialogVisible = false;
      this.refreshPage();
    },
    query() {
      this.pageIndex = 1;
      let dataBody = {};
      if (this.queryWord.length > 0) {
        dataBody.word = this.queryWord;
      }
      if (this.queryCategory.length > 0) {
        dataBody.category = this.queryCategory;
      }
      if (this.queryLanguage.length > 0) {
        dataBody.lang = this.queryLanguage;
      }
      this.queryPageable({
        index: 0,
        size: 10,
        data: dataBody,
        showSensitiveWord: this.showSensitiveWord
      });
    },
    resetSearch() {
      this.pageIndex = 1;
      this.queryWord = ""
      this.queryLanguage = ""
      this.queryCategory = ""
      this.queryRegion = ""
      this.queryPageable({
        index: 0,
        size: 10,
        data: {},
        showSensitiveWord: this.showSensitiveWord
      });
    },
    resetFormData() {
      this.form = {
        word: "",
        category: "",
        lang: "",
      }
    }
    ,
    btn_close() {
      this.dialogVisible = false;
      this.resetFormData();
    },
    refreshPage() {
      this.saveLoading = false;
      let dataBody = {};
      if (this.queryWord.length > 0) {
        dataBody.word = this.queryWord;
      }
      if (this.queryCategory.length > 0) {
        dataBody.category = this.queryCategory;
      }
      if (this.queryLanguage.length > 0) {
        dataBody.lang = this.queryLanguage;
      }
      this.queryPageable({
        index: this.pageIndex - 1,
        size: 10,
        data: dataBody,
        showSensitiveWord: this.showSensitiveWord
      });
    }
    ,
    ...
        mapMutations({}),
    ...
        mapActions({
          queryPageable: 'wordBank/queryPageable',
          addItem: 'wordBank/add',
          updateItem: 'wordBank/update',
          deleteItem: 'wordBank/delete',
          deleteByIds: 'wordBank/deleteByIds'
        })
  }
  ,
  computed: {
    metadata: function () {
      const temp = [];
      this.$store.state.metadata.metadata.forEach((element) => {
        if (element.tableName === "word_bank") {
          let tempElement = {...element};
          tempElement.minWidth = tempElement.columnWidth + "%";
          tempElement.columnWidth = 0;
          temp.push(tempElement);
        }
      });
      return temp;
    }
    ,
    ...
        mapState({
          tableData: (state) => state.wordBank.words,
          total: (state) => state.wordBank.count,
          isLoading: (state) => state.wordBank.isLoading
        })
  }
  ,
  mounted() {
    this.refreshPage();
    metaDictApi.queryByTableName("word_bank", response => {
      if (response.data.code === 200) {
        const contents = response.data.content;
        for (let i = 0; i < contents.length; ++i) {
          if (contents[i].columnName === "category") {
            this.categoryOptions.push(
                {
                  value: contents[i].dictCode,
                  label: contents[i].dictValue
                }
            )
          }
          if (contents[i].columnName === "lang") {
            this.languageOptions.push(
                {
                  value: contents[i].dictCode,
                  label: contents[i].dictValue
                }
            )
          }
        }
      }
    })
  }
}
</script>

<style scoped>
.height {
  margin-top: 20px;
}

.table {
  margin-top: 20px;
}

.dialogForm {
  display: flex;
  justify-content: center;
}

.demo-form {
  width: 400px;
}

.box-card .text_label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  padding: 0 12px 0 0;
  box-sizing: border-box;
  width: 100%;
  line-height: 28px;
}

.box-card {
  background-color: #f8f8f8;
  margin-bottom: 10px;
  padding-bottom: 15px;
}

.box-card .title {
  text-align: center;
  font-size: 18px;
  padding: 10px;
  color: #666;
}

.box-card .dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin: 0 15px;
  background-color: #0080e5;
}

.el-input__inner {
  height: 28px !important;
  line-height: 28px !important;
}

.card-item {
  height: 41px;
  margin-top: 10px;
}

.el-cascader--mini {
  width: 100%;
}

.logintype_content {
  margin: 5px;
  border: 1px solid rgb(221, 221, 221);
  background-color: rgb(242, 242, 242);
  padding: 10px 10px 0 10px;
}

.el-page-header, .el-page-header__content {
  color: #666 !important;
}

.parse-button-icon {
  text-align: center;
  color: #409EFF;
}

.el-radio {
  margin-top: 5px;
}

.el-button:focus, .el-button:hover {
  margin-right: 0px;
}

.el-pagination {
  display: flex;
  justify-content: center;
}

</style>