<template>
    <div class="wct">
        <div class="wct--header">
            <div v-for="(item, key) in cols" :key="key" class="wct--header-col" :style="{flex: item.span}">
                {{item.label}}
            </div>
        </div>
        <div class="wct--content">
            <div class="wct--content-group" v-for="(group, groupIndex) in data" :key="groupIndex">
                <div class="wct--content-group--header">
                  <el-checkbox v-if="check" @change="handleCheckAllChange(group)" v-model="group.checked" :disabled="group.disabled"></el-checkbox>
                  <div @click="handleGroupHeaderClick(group)">
                    <el-icon class="el-icon-caret-bottom"></el-icon>
                  </div>
                  <slot name="group-header" v-bind="{group, groupIndex}">-</slot>
                  <slot name="group-header-right" v-bind="{group, groupIndex}"></slot>
                </div>
                <el-collapse-transition>
                    <div ref="content" class="wct--content-group--content" v-show="isShowGroup(group)">
                        <div class="wct--content-group--list" v-for="(row, rowIndex) in group.children" :key="rowIndex">
                            <slot></slot>
                        </div>
                    </div>
                </el-collapse-transition>
            </div>
        </div>
    </div>
</template>

<script>

export default {
    name: 'WsClassTable',
    props: {
        data: {
            type: Array,
            default: function(){
                return []
            }
        },
        check: {
            type: String,
            default: ''
        }
    },
    provide(){
        return {
            table: this,
        }
    },
    data(){
        return {
            cols: {},
            colRefs: [],

            hideGroupIndexs: []
        }
    },
    computed: {

    },
    created(){
        window.ttt = this;
    },
    mounted(){

    },
    methods: {
        getSlots(){
            // console.log(this.$scopedSlots.default, this.$slots.default);
            return (this.$scopedSlots.default && this.$scopedSlots.default()) || this.$slots.default;
        },

        registerCol(colRef){
            this.colRefs.push(colRef);

            this.updateLabels();
        },
        unRegisterCol(colRef){
            var idx = this.colRefs.indexOf(colRef);
            this.colRefs.splice(idx, 1);

            this.updateLabels();
        },

        updateLabels(){
            this.cols = {};
            for(var i in this.colRefs){
                var col = this.colRefs[i];
                this.cols[col.getColIndex()] = col;
            }
            this.$forceUpdate();
        },

        handleGroupHeaderClick(group){
            if(group.___show___ == undefined){
                this.$set(group, '___show___', false);
                // group.___show___ = false;
            }else{
                group.___show___ = !group.___show___;
            }
        },
        isShowGroup(group){
            if(group.___show___ == true || group.___show___ == undefined){
                return true;
            }
            return false;
        },
        handleCheckAllChange(group){
            this.$emit('handleCheckAllChange', group)
        }
    }
}
</script>

<style lang="scss" scoped>
@import "../../WsTheme/var.scss";

.wct{
    border: 1px solid $color-light-shade;

    &--header{
        display: flex;
        background: $color-light-tint;
        height: 40px;
        align-items: center;

        &-col{
            font-size: $font-size-small;
            color: $color-primary-shade;
            font-weight: bold;
            flex: 1;
            height: 100%;
            display: flex;
            align-items: center;
            padding-left: 10px;
            padding-right: 10px;
            justify-content: center;
            
            &+&{
                border-left: 1px solid $color-light-shade;
            }
        }
    }

    &--content{
        &-group{
            &--header{
                border-top: 1px solid $color-light-shade;
                display: flex;
                align-items: center;
                padding-left: 10px;
                padding-right: 10px;
                background: $color-light;
                height: 40px;
                font-size: $font-size-small;
                color: $color-primary;
                font-weight: bold;
                cursor: pointer;
            }
            &--content{
                border-top: 1px solid $color-light-shade;
                background: #fff;
            }
            &--list{
                display: flex;
                height: 40px;

                &:not(:first-of-type){
                    border-top: 1px solid $color-light-shade;
                }
            }
            &--item{
                flex: 1;
                display: flex;
                align-items: center;
            }
        }
    }
}
</style>
