<!-- @author zhengjie -->
<template>
  <div class="icon-body">
    <el-tabs v-model="activeTab" size="mini">
      <el-tab-pane label="Element图标" name="el-icon">
        <el-input v-model="name" style="position: relative;" clearable placeholder="请输入图标名称" @clear="filterIcons" @input.native="filterIcons" size="mini">
          <i slot="suffix" class="el-icon-search el-input__icon" />
        </el-input>
        <div class="icon-list">
          <div v-for="(item, index) in filteredElIcons" :key="'el-'+index" @click="selectedIcon(item)">
            <i :class="item" style="font-size: 16px; width: 16px; text-align: center;" />
            <span>{{ item.replace('el-icon-', '') }}</span>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="SVG图标" name="svg-icon">
        <el-input v-model="name" style="position: relative;" clearable placeholder="请输入图标名称" @clear="filterIcons" @input.native="filterIcons" size="mini">
          <i slot="suffix" class="el-icon-search el-input__icon" />
        </el-input>
        <div class="icon-list">
          <div v-for="(item, index) in filteredSvgIcons" :key="'svg-'+index" @click="selectedIcon(item)">
            <svg-icon :icon-class="item" style="height: 30px;width: 16px;" />
            <span>{{ item }}</span>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import svgIcons from './requireIcons'
import elIcons from './elIcons'
export default {
  name: 'IconSelect',
  data() {
    return {
      name: '',
      activeTab: 'el-icon',
      svgIconList: svgIcons,
      elIconList: elIcons
    }
  },
  computed: {
    filteredElIcons() {
      if (this.name) {
        return this.elIconList.filter(item => item.includes(this.name))
      }
      return this.elIconList
    },
    filteredSvgIcons() {
      if (this.name) {
        return this.svgIconList.filter(item => item.includes(this.name))
      }
      return this.svgIconList
    }
  },
  methods: {
    filterIcons() {
      // 过滤逻辑已移至 computed
    },
    selectedIcon(name) {
      this.$emit('selected', name)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.activeTab = 'el-icon'
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .icon-body {
    width: 100%;
    padding: 10px;
    .icon-list {
      height: 180px;
      overflow-y: scroll;
      div {
        height: 30px;
        line-height: 30px;
        margin-bottom: -5px;
        cursor: pointer;
        width: 50%;
        float: left;
      }
      span {
        display: inline-block;
        vertical-align: -0.15em;
        fill: currentColor;
        overflow: hidden;
        font-size: 12px;
        margin-left: 4px;
      }
    }
  }
</style>
