<template>
  <el-card id="address-book" class="box-card" shadow="hover">
    <div slot="header" class="clearfix">
      <SvgIcon style="color: var(--theme-color);width: 22px;height: 22px;" icon-class="address-book" />
      <span
        style=" padding-left: 15px"
      >通讯录</span>

      <el-autocomplete
        v-model="keyword"
        value-key="name"
        :fetch-suggestions="querySearch"
        placeholder="关键词"
        clearable
        size="small"
        :trigger-on-focus="false"
        style="float: right; width: 46%;"
        @change="filterList"
      />
    </div>

    <div class="body" :style="{ 'height': tableHeight + 'px' }">
      <ul>
        <li v-for="item in lists" :key="item.id">
          <div class="top">
            <span>{{ item.name }}</span>
            <el-tag v-if="item.department" style="margin-left: 6px;" effect="dark" size="small" type="primary">{{ item.department.name }}</el-tag>
          </div>
          <div v-if="item.mobile" class="bottom" @click="copy2(item.mobile)">
            <i class="el-icon-phone" />
            <span style="color: var(--theme-color)">{{ item.mobile }}</span>
          </div>
        </li>
      </ul>
    </div>
  </el-card>
</template>

<script>
import { copy2, debounce } from '@/utils'
import { index } from '@/api/user'
import SvgIcon from '@/components/SvgIcon/index.vue'
import ElementResize from 'element-resize-detector'
export default {
  name: 'AddressBook',
  components: {
    SvgIcon
  },
  layout: {
    x: 0,
    y: 5,
    w: 8,
    h: 5,
    i: 'AddressBook',
    name: '通讯录'
  },
  data() {
    return {
      keyword: '',
      users: [],
      lists: [],
      tableHeight: 200
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  created() {
    this.getUsers()
  },
  methods: {
    async getUsers() {
      try {
        const res = await index({
          page: 1,
          rows: 999
        })
        this.users = res.data
        this.lists = res.data
      } catch (err) {
        console.error(err)
      }
    },

    copy2,
    filterList(e) {
      if (!e) {
        this.lists = this.users
        return
      }
      this.lists = this.users.filter(this.createFilter(e))
    },
    createFilter(queryString) {
      return (user) => {
        return (user.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    querySearch(queryString, cb) {
      const users = this.users
      const results = queryString ? users.filter(this.createFilter(queryString)) : users
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    init() {
      const cardDom = document.getElementById('address-book')
      const cardTitleH = 60
      const elementResize = ElementResize({
        strategy: 'scroll'
      })
      elementResize.listenTo(cardDom, (ele) => {
        this.tableHeight =
          cardDom.getBoundingClientRect().height -
          40 -
          cardTitleH
      })
    }
  }
}
</script>

<style scoped lang="scss">
.body {
  overflow: scroll;

  &::-webkit-scrollbar-track-piece {
    background: #d3dce6;
  }

  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #99a9bf;
    border-radius: 20px;
  }

  ul {
    list-style: none;
    padding-left: 0;
    margin: 0;

    li {
      line-height: 2;
      padding: 8px 10px;
      position: relative;

      .top {
        display: flex;
        align-items: center;
      }
      .bottom {
        cursor: pointer;
      }

      &::before {
        background: repeating-linear-gradient(-45deg,#ff976a 0%,#ff976a 20%,transparent 0,transparent 25%,var(--theme-color)0,var(--theme-color)45%,transparent 0,transparent 50%);
        content: "";
        background-size: 80px;
        height: 2px;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
      }
    }
  }
}
</style>
