<template>
<div class="dropdown" :class="open ? 'open' : ''">
    <el-button type="primary" circle class="drop-btn" @mouseup.native="toggleDropdown">
      <span class="glyphicon glyphicon-filter"></span>
    </el-button>
    <ul class="dropdown-menu" v-show="open">
      <el-checkbox-group class="labels" v-model="labels">
        <template v-for="(label, index) in columns">
          <li :key="index">
            <el-checkbox :label="label"></el-checkbox>
          </li>
        </template>
      </el-checkbox-group>
      <li role="separator" class="divider"></li>
      <li>
        <div class="cool-down-operate clearfix">
          <el-button type="primary" round plain size="mini" @mouseup.native="selectAll">全选</el-button>
          <el-button type="primary" round plain size="mini" @mouseup.native="clearAll">清空</el-button>
          <el-button type="primary" round size="mini" @mouseup.native="okFilter">确定</el-button>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'drop-filter',
  props: {
    flag: Boolean,
    columns: {
      type: Array,
      default: () => []
    },
    storeKeys: String,
    storeLabels: Array
  },
  created () {
  },
  mounted () {
    if (!this.storeKeys) return
    if (!this.storeLabels.length) {
      this.labels = [...this.columns] // 默认全选
    }
  },
  watch: {
    storeLabels (val) {
      if (!val) {
        this.labels = [...this.columns] // 默认全选
      } else {
        this.labels = [...val]
      }
    },
    //    flag (val) {
    //      if (!val) {
    //        this.toggleDropdown()
    //      }
    //    }
  },
  data () {
    return {
      open: false,
      // checked: [],
      labels: [], // 操作筛选的数据
      storeKey: ''
    }
  },
  methods: {
    toggleDropdown () {
      if (!this.flag) {
        this.open = !this.open
      }
    },
    // 全选
    selectAll () {
      this.labels = []
      this.columns.forEach((label, i) => {
        this.labels.push(label)
      })
    },
    // 清空
    clearAll () {
      this.labels = []
    },
    // 提交
    okFilter () {
      console.log(this.flag)
      this.open = false
      this.storeKey = this.$route.name
      this.$Store.set(this.storeKey, this.labels)
      this.$emit('doFilter', this.labels)
    },
  },

}
</script>
<style lang="scss" scoped>
.pull-right {
  float: right !important;
}
.dropup,
.dropdown {
  position: absolute;
  display: inline-block;
  z-index: 1000;
  left: 0;
  top: 25%;
  /*opacity: 0.5;*/
  &.open {
    /*opacity: 1;*/
  }
  .drop-btn {
    color: #fff;
    padding: 4px;
    width: 40px;
    height: 40px;
  }
}
.open > .dropdown-menu {
  /*display: block;*/
  li {
    margin-bottom: 5px;
  }
}
.btn {
  display: inline-block;
  padding: 4px ;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  -ms-touch-action: manipulation;
  touch-action: manipulation;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
}
.btn-default {
  color: #333;
  background-color: #fff;
  border-color: #ccc;
}
.glyphicon {
  position: relative;
  top: 1px;
  display: inline-block;
  font-family: 'Glyphicons Halflings';
  font-style: normal;
  font-weight: normal;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.glyphicon-filter{
  background: url('./filter1.png') no-repeat;
  width: 20px;
  height: 20px;
  background-position: center
}
.dropdown-menu {
  position: absolute;
  /*right: 0;*/
  // top: 100%;

  z-index: 1000;
  /*display: none;*/
  padding: 10px;
  float: left;
  min-width: 200px;
  margin: 2px 0 0;
  font-size: 14px;
  text-align: left;
  list-style: none;
  background-color: #fff;
  -webkit-background-clip: padding-box;
  background-clip: padding-box;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
}
.flag-right {
  right: 5px;
}
.flag-left {
  left: 0;
}
.flag-top {
  top: 42px;
}
.flag-bottom {
  bottom: 42px;
}
.labels {
  max-height: 300px;
  overflow-y: auto;
}
.pull-right > .dropdown-menu {
  right: 0;
  left: auto;
}
.dropdown-menu > li > a {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-weight: normal;
  line-height: 1.42857143;
  color: #333;
  white-space: nowrap;
}
.cool-drop-item {
  padding: 3px 8px;
}
.dropdown-menu .divider {
  height: 1px;
  margin: 9px 0;
  overflow: hidden;
  background-color: #e5e5e5;
}
.cool-check {
  margin: 3px 12px 3px 6px;
  border: 1px solid #aaaaaa;
  width: 14px;
  height: 14px;
  display: inline-block;
  float: left;
  &.checked {
    background-color: #449d44;
    border-color: #4cae4c;
  }
}
.cool-down-operate {
  /*padding: 3px 12px;*/
  span {
    padding: 0 6px;
    cursor: pointer;
  }
  .el-button--mini, .el-button--mini.is-round {
    padding: 6px 10px;
  }
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: ' ';
  clear: both;
}
</style>
