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

<script>
import { mapGetters } from "vuex";
export default {
    name: "component-widget-count-modify",
    data() {
        return {
            number: 0,
        };
    },
    props: {
        auth: {
            type: Boolean,
            default: false,
        },
        min: {
            type: [Number, String],
            default: 0,
        },
        max: {
            type: [Number, String],
            default: 0,
        },
        init: {
            type: Number,
            default: 0,
        },
    },
    components: {},
    computed: {
        ...mapGetters({
            isLogin: "user/isLogin",
        }),
    },
    created() {
        this.number = this.init;
    },
    mounted() {},
    destroyed() {},
    watch: {
        init(value) {
            this.number = value;
        },
    },
    methods: {
        calc(type) {
            let event = {};
            if (!this.auth || (this.auth && this.isLogin)) {
                if (type == 1) {
                    if (this.max == 0 || this.number < this.max) {
                        this.number++;
                    } else {
                        this.number = this.max;
                    }
                } else {
                    if (this.number > this.min) {
                        this.number--;
                    } else {
                        this.number = this.min;
                    }
                }
                event = {
                    type: type == 1 ? "add" : "sub",
                    value: this.number,
                };
            } else {
                event = {
                    type: "nologin",
                    value: 0,
                };
            }
            this.$emit("change", event);
        },
    },
};
</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>