<template>
  <div class="group-check-cnt">
    <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全部</el-checkbox>
    <el-checkbox-group v-model="checkedVal" @change="handleCheckedChange">
      <el-checkbox v-for="item in groupArr" :label="item.value" :key="item.value">
        {{ item.label }}
        <el-tooltip
          v-if="item.desc"
          placement="top"
          trigger="hover"
          :content="item.desc"
          width="200"
        >
          <span class="quesiton"><i class="el-icon-question"></i></span>
        </el-tooltip>
      </el-checkbox>
    </el-checkbox-group>
  </div>
</template>

<script>
export default {
  props: {
    groupArr: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      checkedVal: [],
      isIndeterminate: false,
      checkAll: false
    }
  },
  methods: {
    handleCheckedChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.groupArr.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.groupArr.length
      this.$emit('onGroupChange', this.checkedVal)
    },
    handleCheckAllChange(val) {
      this.checkedVal = val ? this.groupArr?.map((ot) => ot.value) : []
      this.isIndeterminate = false
      this.$emit('onGroupChange', this.checkedVal)
    }
  }
}
</script>

<style lang="scss" scoped>
.group-check-cnt {

  > .el-checkbox {
    margin: 10px 0 ;
  }
  ::v-deep .el-checkbox__label {
    color: #3d3d3d;
    font-size: 16px;
    font-weight: 400;
    padding-left: 6px;
    line-height: 20px;
    position: relative;
  }
  .quesiton{    
    display: block;
    font-size: 13px;
    color: #b9b9b9;
    position: absolute;
    right: -14px;
    top: -6px;
  }
}
</style>