<template>
  <div>
    <!--tag浏览-->
    <el-dialog :title="title" :visible.sync="showFlag" size="tiny" class="word-dialog" @open="getWords">
      <el-form>
        <el-form-item label="通过名称查询：">
          <el-input v-model="searchWord" style="width:180px"></el-input>
        </el-form-item>
        <el-form-item label="最近使用过的：">
          <span v-for="word in words" :key="word.index">
            <a class="link" :title="word" @click="wordClick(word)">{{word | charWidth(10)}}&nbsp;&nbsp;</a>
          </span>
        </el-form-item>
      </el-form>

      <div v-loading="wordLoading">
        <div v-for="item in allWords" :key="item.index" class="letter-item">
          <div class="shadow_region">
            <a :id="item.letter">{{item.letter}}</a>
          </div>
          <div v-for="i in item.words" :key="i.index">
            <a class="link" @click="wordClick(i.word)">{{i.word}}</a>
          </div>
        </div>
        <aside class="letter-aside">
          <ul>
            <li @click="naver(letter)" v-for="letter in letters" :key="letter.index">{{letter}}</li>
          </ul>
        </aside>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    contentType: {
      type: String, // 内容类型，如：Article，Image，File，Special，Video等
      default: ''
    },
    wordFlag: {
      type: Boolean, // 弹窗状态
      default: false
    },
    title: {
      type: String, // 弹窗标题
      default: ''
    },
    type: {
      type: String, //  类型，如：tag为tag词source为来源词
      default: ''
    }
  },
  data() {
    return {
      allWords: [],
      searchWord: '',
      words: [],
      letters: [
        '#',
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z'
      ],
      confirmLoading: false,
      wordLoading: false
    }
  },
  computed: {
    showFlag: {
      set(val) {
        this.$emit('update:wordFlag', val)
      },
      get() {
        return this.wordFlag
      }
    }
  },
  watch: {
    searchWord(val) {
      this.getWords()
    }
  },
  methods: {
    async getWords() {
      let resp
      if (this.type === 'tag') {
        resp = await axios.get(`/ui/tagwords/init/${this.contentType}`, {params: {searchWord: this.searchWord}})
      } else if (this.type === 'source') {
        resp = await axios.get('/ui/sourcewords/init', {params: {searchWord: encodeURIComponent(this.searchWord)}})
      }
      if (resp.data.status === 1) {
        this.allWords = resp.data.data
      }
    },
    wordSearch() {
      this.getWords()
    },
    wordClick(val, index) {
      this.$emit('wordCallBack', val)
      this.showFlag = false
    },
    naver(id) {
      try {
        throw document.getElementById(id).scrollIntoView()
      } catch (e) { }
    }
  },
  created() {
    // 最近使用过的，从localStorage中获取
    let ls
    if (this.type === 'tag') {
      // let tagname = 'TagNames_' + this.contentType
      ls = localStorage.tagname
    } else if (this.type === 'source') {
      ls = localStorage.SourceWords
    }
    if (ls) {
      this.words = ls.split(',')
    }
  }
}
</script>
<style scoped>
  .shadow_region {
    text-shadow: 0 1px 1px #3e6790;
    background-color: #e0e8f1;
  }

  .selfIcon {
    font-size: 20px;
    padding: 0 5px;
    cursor: pointer;
  }

  .content-form {
    height: 600px;
    overflow: auto;
  }

  .font-border {
    width: 20px;
    height: 16px;
    margin: 1px;
    text-align: center;
    border: 1px solid #bfcbd9;
    background-color: transparent;
    border-radius: 2px;
  }

  .font-border:hover {
    border: 1px solid #ffb820;
  }

  .active {
    background-color: #eed2c1;
    border-color: #b39;
  }

  .summary-font-counter {
    font-family: Constantia, Corbel;
    font-size: 26px;
    line-height: 12px;
    margin-right: 5px;
  }

  .letter-aside {
    position: fixed;
    right: 23px;
    top: 10.3rem;
  }

  .letter-aside ul {
    line-height: 1.4em;
    font-size: 14px;
    color: #bbb;
  }

  .letter-aside li {
    font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, sans-serif;
    font-size: 12px;

    text-align: center;
    cursor: pointer;
    list-style: none;
  }

  .letter-item .link {
    font-size: 14px;
    font-weight: bold;
    color: #2f3e46;
  }

  .letter-item>div {
    padding-right: 6%;
    padding-left: 6%;
    border-bottom: 1px solid #d2e2f9;
    margin-right: 10px;
    margin-left: 10px;
    line-height: 2em;
  }

  .letter-item>div:first-child {
    font-size: 16px;
    line-height: 2em;
    background: #d2e2f9;
    background-image: linear-gradient(#bcd0eb, #d2e2f9);
    margin-bottom: 2px;
    margin-right: 10px;
    margin-left: 10px;
  }

  .divider-current {
    background: #B6D6F4;
    background-image: linear-gradient(#9cc8f3, #b6d6f4);
  }

  .letter-item>div:first-child a {
    color: #fff;
    text-shadow: 0 1px 1px #3e6790;
  }

  .colors {
    color: #003399;
  }

  .upload-images {
    margin-top: -10px;
    margin-left: 8px;
  }

  .upload-images i {
    margin: 0 3px;
  }

  .el-input input {
    color: inherit !important;
    /*从父元素继承颜色*/
    text-decoration: inherit !important;
    /*从父元素继承下划线*/
  }
  /**控制Dialog不应高度变化而改变位置**/
  .word-dialog{
    top: 30px;
    bottom: auto;
  }
  .word-dialog .el-dialog__body {
    height: 600px;
    overflow: auto;
  }
</style>
