<template>
  <el-dialog
      title="设置字体大小"
      v-model="visible"
      width="30%"
      @close="handleCancel"
  >
    <div class="font-size-control">
      <div class="slider-label">字体缩放：{{ tempScale.toFixed(2) }}</div>

      <el-slider
          v-model="tempScale"
          :show-tooltip="false"
          show-stops
          :min="0.5"
          :max="1.5"
          :step="0.05"
          :marks="marks"
          @input="handlePreview"
      />

      <div class="preview" :style="{ fontSize: previewSize + 'px' }">
        这里是预览文字 (This is preview text)
      </div>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useFontSizeStore } from '@/stores/fontSize'

const visible = ref(false)
const tempScale = ref(1.0) // 默认 1.0
const previewSize = ref(16)

const fontSizeStore = useFontSizeStore()

// 滑块两侧标记
const marks = {
  0.5: '小',
  1.0: '默认',
  1.5: '大'
}

// 打开弹窗
const open = () => {
  tempScale.value = fontSizeStore.scale || 1.0
  visible.value = true
}

// 实时预览文字大小
const handlePreview = (val) => {
  previewSize.value = 16 * val
}

// 点击确定
const handleConfirm = () => {
  fontSizeStore.updateScale(tempScale.value)
  visible.value = false
}

// 点击取消
const handleCancel = () => {
  tempScale.value = fontSizeStore.scale
  visible.value = false
}

// 监听 tempScale 自动更新预览
watch(tempScale, (newVal) => {
  previewSize.value = 16 * newVal
}, { immediate: true })

// 暴露 open 方法给父组件
defineExpose({ open })
</script>

<style scoped>
.font-size-control {
  padding: 1rem;
}
.slider-label {
  margin-bottom: 10px;
  font-weight: 500;
}
.preview {
  margin-top: 1rem;
  border: 1px solid #ddd;
  padding: 10px;
  text-align: center;
}
</style>
