<template>
  <div class="range-slider">
    <div class="label h700" v-if="label">
      {{ label }}
    </div>

    <div class="slider">
      <input
        type="range"
        :min="minValue"
        :max="maxValue"
        :step="step"
        v-model="rangeValue"
        @input="changeValue"
      />

      <div class="rail">
        <div class="fill" :style="{ width: sliderPercent + '%' }"></div>

        <span
          class="division"
          v-for="(n, index) in 6"
          :key="index"
          :style="{ left: 20 * index + '%' }"
          :class="{ blue: sliderPercent > 20 * index }"
        ></span>
      </div>
    </div>

    <div class="range">
      <div class="h700">{{ minValue }}{{ unit }}</div>
      <div class="h700">{{ maxValue }}{{ unit }}</div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, ref, watch } from "vue";

export default defineComponent({
    emits: ['update:modelValue'],
    props: {
        modelValue: {
            type: [ Number, String ]
        },
        label: {
            type: String,
            default: ""
        },
        minValue: {
            type: [ Number, String ]
        },
        maxValue: {
            type: [ Number, String ]
        },
        step: {
            type: Number,
            default: 1
        },
        unit: {
            type: String,
            default: "₾",
        },
    },
    setup(props, { emit }) {
        const rangeValue = ref<number>(null);

        const sliderPercent = computed(() => {
            return (
                ((rangeValue.value - parseInt(props.minValue as string)) / 
                    (parseInt(props.maxValue as string) - parseInt(props.minValue as string))) *
                100
            );
        });

        const changeValue = (e: any) => {
            rangeValue.value = e.target.value;
            emit("update:modelValue", e.target.value);
        };

        onMounted(() => {
            rangeValue.value = parseInt(props.minValue as string);
            if (props.modelValue) {
                rangeValue.value = parseInt(props.modelValue as string);
            }
        });

        watch(
            () => props.modelValue,
            (newVal, oldVal) => {
              if (newVal === rangeValue.value) return;

              if (rangeValue.value && rangeValue.value >= props.minValue) {
                  rangeValue.value = parseInt(oldVal as string);
              } else {
                  rangeValue.value = parseInt(props.minValue as string);
              };
            }
        );

        return {
            sliderPercent,
            changeValue,
            rangeValue
        }
    }
});
</script>

<style scoped lang="scss">
.range-slider {
  .label {
    margin-bottom: var(--spacing-24);
    color: var(--blue);
  }
  .slider {
    position: relative;
    input {
      width: 100%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      margin: 0;
      padding: 0;
      top: 50%;
      z-index: 1;
      background-color: transparent;
      height: 23px;
      &::-webkit-slider-thumb {
        height: 23px;
        width: 23px;
        border: 2px solid var(--blue);
        background-color: var(--grey-6);
        margin: 0;
        padding: 0;
        cursor: pointer;
        -webkit-appearance: none;
        border-radius: 50%;
      }
      &::-moz-range-thumb {
        box-shadow: none;
        border: none;
        height: 3rem;
        width: 3rem;
        border-radius: 50%;
        background: var(--grey-6);
        cursor: pointer;
      }
    }
    .rail {
      width: 100%;
      background-color: var(--grey-4);
      height: 3px;
      position: relative;
      margin: auto;
      .fill {
        max-width: 100%;
        background: var(--blue);
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
      }
      .division {
        width: 2px;
        height: 7px;
        background-color: var(--grey-4);
        border-radius: 6px;
        position: absolute;
        top: -2px;
        margin-left: -1px;
        &.blue {
          background-color: var(--blue);
        }
      }
    }
  }
  .range {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 16px;
    .h700 {
      color: var(--text-color-secondary);
    }
  }
}
</style>
