<template>
    <view class="component-widget-count-modify" :class="[number > 0 ? 'between' : 'end']">
        <view v-if="number > 0" class="btn sub" @click="sub">
            <text class="iconfont icon-jianhao"></text>
        </view>
        <view v-if="number > 0" class="num">{{ number }}</view>
        <view class="btn add" @click="add">
            <text class="iconfont icon-jiahao-"></text>
        </view>
    </view>
</template>

<script>
export default {
    name: "component-widget-count-modify",
    data() {
        return {
            number: 0,
        };
    },
    props: {
        min: {
            type: [Number, String],
            default: 0,
        },
        max: {
            type: [Number, String],
            default: 0,
        },
        init: {
            type: Number,
            default: 0,
        },
    },
    components: {},
    created() {
        this.number = this.init;
    },
    mounted() {},
    destroyed() {},
    watch: {
        init(value) {
            this.number = value;
        }
    },
    methods: {
        sub() {
            if (this.number > this.min) {
                this.number--;
            } else {
                this.number = this.min;
            }
            this.$emit("change", {
                type: "sub",
                value: this.number,
            });
        },
        add() {
            if (this.max == 0 || this.number < this.max) {
                this.number++;
            } else {
                this.number = this.max;
            }
            this.$emit("change", {
                type: "add",
                value: this.number,
            });
        },
    },
};
</script>

<style lang="less" scoped>
.component-widget-count-modify {
    width: 130rpx;
    height: 35rpx;
    display: flex;
    align-items: center;
    .num {
        color: #2d2d2d;
        font-size: 30rpx;
    }
    .btn.sub .iconfont,
    .btn.add .iconfont {
        color: #4b65ed;
        font-size: 35rpx;
    }
}
.component-widget-count-modify.between {
    justify-content: space-between;
}
.component-widget-count-modify.end {
    justify-content: flex-end;
}
</style>