<template>
    <view :class="'custom-class ' + (value === activeValue ? 'van-switch--on ' : '') + (disabled ? 'van-switch--disabled' : '')"
        :style="'font-size: ' + (size) + '; ' + ((checked ? activeColor : inactiveColor) ? 'background-color: ' + (checked ? activeColor : inactiveColor) : '')"
        @click="onClick">
        <view class="van-switch__node node-class">
            <van-loading v-if="loading" :color="loadingColor" custom-class="van-switch__loading"></van-loading>
        </view>
    </view>
</template>
<script>
import VanLoading from '../loading/index.vue'
import color_1 from '../common/color'
export default {
    components: {
        'van-loading': VanLoading
    },
    props: {
        checked: {
            type: null,
        },
        loading: {
            type: Boolean,
            default: false
        },
        disabled: {
            type: Boolean,
            default: false
        },
        activeColor: {
            type: String,
            default: ''
        },
        inactiveColor: {
            type: String,
            default: ''
        },
        size: {
            type: String,
            default: '30px'
        },
        activeValue: {
            type: null,
            default: true
        },
        inactiveValue: {
            type: null,
            default: false
        }
    },
    watch: {
        checked(newVal) {
            this.checkedObserver(newVal)
        }
    },
    data() {
        return {
            classes: ['node-class'],
        }
    },
    created: function () {
        var value = this.data.checked;
        var loadingColor = this.getLoadingColor(value);
        this.setData({ value: value, loadingColor: loadingColor });
    },
    mounted() {
        this.checkedObserver(this.checked)
    },
    methods: {
        checkedObserver: function (value) {
            var loadingColor = this.getLoadingColor(value);
            this.setData({ value: value, loadingColor: loadingColor });
        },
        getLoadingColor: function (checked) {
            var activeColor = this.activeColor, inactiveColor = this.inactiveColor;
            return checked ? activeColor || color_1.BLUE : inactiveColor || color_1.GRAY_DARK;
        },
        onClick: function () {
            var self = this
            var activeValue = self.activeValue, inactiveValue = self.inactiveValue;
            if (!self.disabled && !self.loading) {
                var checked = self.checked === activeValue;
                var value = checked ? inactiveValue : activeValue;
                self.$emit('input', value);
                self.$emit('change', value);
            }
        }
    }
}
</script>
<style>
@import '../common/index.css';

.van-switch {
    position: relative;
    display: inline-block;
    box-sizing: initial;
    width: 2em;
    width: var(--switch-width, 2em);
    height: 1em;
    height: var(--switch-height, 1em);
    background-color: #fff;
    background-color: var(--switch-background-color, #fff);
    border: 1px solid rgba(0, 0, 0, .1);
    border: var(--switch-border, 1px solid rgba(0, 0, 0, .1));
    border-radius: 1em;
    border-radius: var(--switch-node-size, 1em);
    transition: background-color .3s;
    transition: background-color var(--switch-transition-duration, .3s)
}

.van-switch__node {
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 100%;
    z-index: 1;
    z-index: var(--switch-node-z-index, 1);
    width: 1em;
    width: var(--switch-node-size, 1em);
    height: 1em;
    height: var(--switch-node-size, 1em);
    background-color: #fff;
    background-color: var(--switch-node-background-color, #fff);
    box-shadow: 0 3px 1px 0 rgba(0, 0, 0, .05), 0 2px 2px 0 rgba(0, 0, 0, .1), 0 3px 3px 0 rgba(0, 0, 0, .05);
    box-shadow: var(--switch-node-box-shadow, 0 3px 1px 0 rgba(0, 0, 0, .05), 0 2px 2px 0 rgba(0, 0, 0, .1), 0 3px 3px 0 rgba(0, 0, 0, .05));
    transition: -webkit-transform .3s cubic-bezier(.3, 1.05, .4, 1.05);
    transition: transform .3s cubic-bezier(.3, 1.05, .4, 1.05);
    transition: transform .3s cubic-bezier(.3, 1.05, .4, 1.05), -webkit-transform .3s cubic-bezier(.3, 1.05, .4, 1.05);
    transition: -webkit-transform var(--switch-transition-duration, .3s) cubic-bezier(.3, 1.05, .4, 1.05);
    transition: transform var(--switch-transition-duration, .3s) cubic-bezier(.3, 1.05, .4, 1.05);
    transition: transform var(--switch-transition-duration, .3s) cubic-bezier(.3, 1.05, .4, 1.05), -webkit-transform var(--switch-transition-duration, .3s) cubic-bezier(.3, 1.05, .4, 1.05)
}

.van-switch__loading {
    position: absolute !important;
    top: 25%;
    left: 25%;
    width: 50%;
    height: 50%
}

.van-switch--on {
    background-color: #1989fa;
    background-color: var(--switch-on-background-color, #1989fa)
}

.van-switch--on .van-switch__node {
    -webkit-transform: translateX(1em);
    transform: translateX(1em);
    -webkit-transform: translateX(calc(var(--switch-width, 2em) - var(--switch-node-size, 1em)));
    transform: translateX(calc(var(--switch-width, 2em) - var(--switch-node-size, 1em)))
}

.van-switch--disabled {
    opacity: .4;
    opacity: var(--switch-disabled-opacity, .4)
}</style>