<template>
    <view :class="'custom-class ' + utils.bem('slider', { disabled })" :style="inactiveColor ? 'background:' + inactiveColor : ''" @tap="onClick">
        <view class="van-slider__bar" :style="barStyle + ';' + computed.barStyle(barHeight, activeColor)">
            <view class="van-slider__button-wrapper" @touchstart="onTouchStart" @touchmove.stop.prevent="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
                <slot v-if="useButtonSlot" name="button" />
                <view v-else class="van-slider__button" />
            </view>
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
<script module="computed" lang="wxs" src="@/node_modules/@vant/weapp/dist/slider/index.wxs"></script>
<script>
import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch';
import { canIUseModel } from '../common/version';
export default {
    data() {
        return {
            barStyle: '',
            valueClone: 0
        };
    },
    mixins: [touch],
    props: {
        disabled: Boolean,
        useButtonSlot: Boolean,
        activeColor: String,
        inactiveColor: String,
        max: {
            type: Number,
            default: 100
        },
        min: {
            type: Number,
            default: 0
        },
        step: {
            type: Number,
            default: 1
        },
        value: {
            type: Number,
            default: 0
        },
        barHeight: {
            type: null,
            default: '2px'
        }
    },
    created() {
        this.updateValue(this.value);
    },
    methods: {
        onTouchStart(event) {
            if (this.disabled) {
                return;
            }
            this.touchStart(event);
            this.startValue = this.format(this.value);
            this.dragStatus = 'start';
        },
        onTouchMove(event) {
            if (this.disabled) {
                return;
            }
            if (this.dragStatus === 'start') {
                this.$emit('drag-start');
            }
            this.touchMove(event);
            this.dragStatus = 'draging';
            this.getRect('.van-slider').then((rect) => {
                const diff = (this.deltaX / rect.width) * 100;
                this.newValue = this.startValue + diff;
                this.updateValue(this.newValue, false, true);
            });
        },
        onTouchEnd() {
            if (this.disabled) {
                return;
            }
            if (this.dragStatus === 'draging') {
                this.updateValue(this.newValue, true);
                this.$emit('drag-end');
            }
        },
        onClick(event) {
            if (this.disabled) {
                return;
            }
            const { min } = this;
            this.getRect('.van-slider').then((rect) => {
                const value = ((event.detail.x - rect.left) / rect.width) * this.getRange() + min;
                this.updateValue(value, true);
            });
        },
        updateValue(value, end, drag) {
            value = this.format(value);
            const { min } = this;
            const width = `${((value - min) * 100) / this.getRange()}%`;
            this.setData({
                valueClone: value,
                barStyle: `
        width: ${width};
        ${drag ? 'transition: none;' : ''}
      `
            });
            if (drag) {
                this.$emit('drag', {
                    value
                });
            }
            if (end) {
                this.$emit('change', value);
            }
            if ((drag || end) && canIUseModel()) {
                this.setData({
                    valueClone: value
                });
            }
        },
        getRange() {
            const { max, min } = this;
            return max - min;
        },
        format(value) {
            const { max, min, step } = this;
            return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
        }
    },
    watch: {
        value: {
            handler: function (value, end, drag) {
                this.valueClone = this.clone(this.value);
                value = this.format(value);
                const { min } = this;
                const width = `${((value - min) * 100) / this.getRange()}%`;
                this.setData({
                    valueClone: value,
                    barStyle: `
          width: ${width};
          ${drag ? 'transition: none;' : ''}
        `
                });
                if (drag) {
                    this.$emit('drag', {
                        value
                    });
                }
                if (end) {
                    this.$emit('change', value);
                }
                if ((drag || end) && canIUseModel()) {
                    this.setData({
                        valueClone: value
                    });
                }
            },

            immediate: true
        }
    }
};
</script>
<style>
@import './index.css';
</style>
