<script setup lang="ts">
const modelValue = defineModel<number>({ default: 0 })

const props = defineProps<{
  title: string
  min: number
  max: number
}>()

const sliderId = 'pain-slider'
const labelId = 'pain-title'

function onInput(ev: Event) {
  const target = ev.target as HTMLInputElement
  modelValue.value = Number(target.value)
}
</script>

<template>
  <section>
    <h2 :id="labelId">{{ props.title }}</h2>
    <div class="value">{{ modelValue }}</div>
    <input
      :id="sliderId"
      type="range"
      :min="props.min"
      :max="props.max"
      :value="modelValue"
      :aria-labelledby="labelId"
      :aria-valuemin="props.min"
      :aria-valuemax="props.max"
      :aria-valuenow="modelValue"
      @input="onInput"
    />
    <div class="scale">
      <span>{{ props.min }}</span>
      <span>{{ props.max }}</span>
    </div>
  </section>
</template>

<style scoped>
h2 {
  font-size: 1rem;
  margin-bottom: 0.25rem;
}

.value {
  text-align: center;
  font-size: 2rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
  color: #111827;
}

input[type='range'] {
  -webkit-appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: #e5e7eb;
  outline: none;
}

input[type='range']:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 4px;
}

input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ffffff;
  border: 2px solid #f97316;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  margin-top: -9px;
}

input[type='range']::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ffffff;
  border: 2px solid #f97316;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

.scale {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: #6b7280;
  margin-top: 0.25rem;
}
</style>
