/*
    选择数量
*/
<template>
    <div class="num-wapper">
        <div class="title">数量</div>
        <div class="count">
            <div class="minus" :class="{'disabled': value <= 1}" @click="count('-')"></div>
            <span class="num">{{value}}</span>
            <div class="add" :class="{'disabled': value >= stock}" @click="count('+')"></div>
        </div>
    </div>
</template>

<script>
    export default {
        props: {
            value:{},
            stock:{},
        },
        methods: {
            count(type){
                if(this.stock){
                    let num = this.value;
                    if(type == '-'){
                        if(num - 1 < 1){
                            this.$layer.msg('数量不能为空！');
                            return false;
                        }
                        num = num - 1 < 1 ? 1: num - 1;
                    }else if(type == '+'){
                        if(num + 1 > this.stock){
                            this.$layer.msg('库存不足！');
                            return false;
                        }
                        num = num + 1 > this.stock ? this.stock : num + 1;
                    }
                    this.$emit('input', num);
                }else{
                    this.$layer.msg('库存不足！');
                    this.$emit('input', 0);
                }
            },
        }
    }
</script>

<style lang="less" scoped>
    .num-wapper{
        display: flex;
        line-height: 30px;

        .title{
            flex: 0 0 auto;
            margin-right: 10px;
            width: 30px;
            color: #666666;
            font-size: 12px;
        }
        .count{
            flex: 1;

            .minus,.add{
                font-size: 12px;
                vertical-align: middle;
                display: inline-block;
                width: 24px;
                height: 24px;
                border-radius: 100%;
                background-position: center center;
                background-repeat: no-repeat;
                background-size: 100% 100%;

                &.minus{
                    background-image: url("./img/minus.png");
                    &.disabled{
                        background-image: url("./img/minus-2.png");
                    }
                }
                &.add{
                    background-image: url("./img/add.png");
                    &.disabled{
                        background-image: url("./img/add-2.png");
                    }
                }
            }
            .num{
                margin: 0 10px;
                font-size: 13px;
                color: #111;
            }
        }
    }
</style>