<!-- @author zhengjie -->
<template>
  <div>
    <a-modal
      :title="title"
      :visible.sync="open"
      @cancel="cancel"
      :footer="null"
      width="70%"
    >
      <div class="icon-body">
        <a-input
          v-model="name"
          style="position: relative"
          clearable
          placeholder="请输入图标名称"
          @clear="filterIcons"
          @input.native="filterIcons"
        >
          <i slot="suffix" class="el-icon-search el-input__icon" />
        </a-input>
        <div class="icon-list">
          <div
            v-for="(item, index) in iconList"
            :key="index"
            @click="selectedIcon(item)"
          >
            <i :class="`iconfont ${item.icon}`"></i>
            <span style="margin-left: 5px">{{ item.name }}</span>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { customTools } from "@/utils/topology/registerIconssetting.js";
export default {
  name: "IconSelect",
  data() {
    return {
      name: "",
      iconList: [],
      title: "",
      open: false,
    };
  },
  methods: {
    cancel() {
      this.open = false;
    },
    init() {
      this.title = "选择图标";
      this.open = true;
      this.filterIcons();
      this.name = "";
    },
    filterIcons() {
      this.iconList = [];
      for (let i of customTools) {
        if (i.children) {
          for (let j = 0; j < i.children.length; j++) {
            this.iconList.push(i.children[j]);
          }
        }
      }
      if (this.name) {
        this.iconList = this.iconList.filter((item) =>
          item.name.includes(this.name)
        );
      }
    },
    selectedIcon(item) {
      this.$emit("selected", item);
      this.cancel();
    },
  },
};
</script>

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