<template>
    <view :class="utils.bem('tabbar-item', { active }) + ' custom-class'" :style="'color: ' + (active ? activeColor : inactiveColor)" @tap="onClick">
        <view class="van-tabbar-item__icon">
            <van-icon v-if="icon" :name="icon" custom-class="van-tabbar-item__icon__inner" />
            <block v-else>
                <slot v-if="active" name="icon-active" />
                <slot v-else name="icon" />
            </block>
            <van-info :dot="dot" :info="info" custom-class="van-tabbar-item__info" />
        </view>
        <view class="van-tabbar-item__text">
            <slot />
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
<script>
import { VantComponent } from '../common/component';
export default {
    data() {
        return {
            active: false,
            activeColor: '',
            inactiveColor: ''
        };
    },
    props: {
        info: null,
        name: null,
        icon: String,
        dot: Boolean
    },
    '../tabbar/index': {
        name: 'tabbar',
        type: 'ancestor',
        current: 'tabbar-item'
    },
    methods: {
        onClick() {
            if (this.parent) {
                this.parent.onChange(this);
            }
            this.$emit('click');
        },
        updateFromParent() {
            const { parent } = this;
            if (!parent) {
                return;
            }
            const index = parent.children.indexOf(this);
            const parentData = parent.data;
            const { data } = this;
            const active = (data.name || index) === parentData.active;
            const patch = {};
            if (active !== data.active) {
                patch.active = active;
            }
            if (parentData.activeColor !== data.activeColor) {
                patch.activeColor = parentData.activeColor;
            }
            if (parentData.inactiveColor !== data.inactiveColor) {
                patch.inactiveColor = parentData.inactiveColor;
            }
            return Object.keys(patch).length > 0 ? this.set(patch) : Promise.resolve();
        }
    }
};
</script>
<style>
@import './index.css';
</style>
