<template>
  <div class="custom-popover-wrapper">
    <div class="choice-btn" @click.stop="handleChoiceBtnClick">
      <div class="text-music">纯音乐</div>
      <div class="switch-toggle" :class="{ active: isSwitchOn }" @click.stop="toggleSwitch">
        <div class="switch-slider"></div>
      </div>
    </div>

    <Transition name="popover">
      <div v-show="showLyricsPopover && !isSwitchOn" class="custom-popover" @click.stop>
        <div class="input-wrapper">
          <textarea v-model="lyricsText" class="lyrics-input" placeholder="输入歌词" @mousedown.stop></textarea>
        </div>
        <div class="button-wrapper">
          <button class="confirm-btn" @click="confirmLyrics">确定</button>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'

const props = defineProps({
  modelValue: { type: [String, Boolean], default: true },
  lyrics: { type: String, default: '' }
})

const emit = defineEmits(['update:modelValue', 'update:lyrics'])

const showLyricsPopover = ref(false)
const lyricsText = ref('')

const isSwitchOn = computed(() => {
  return props.modelValue === true || props.modelValue === 'true' || props.modelValue === '纯音乐模式'
})

const toggleSwitch = () => {
  if (isSwitchOn.value) {
    emit('update:modelValue', false)
    showLyricsPopover.value = true
  } else {
    emit('update:modelValue', true)
    emit('update:lyrics', '')
    showLyricsPopover.value = false
  }
}

const handleChoiceBtnClick = () => {
  if (isSwitchOn.value) return
  showLyricsPopover.value = !showLyricsPopover.value
}

const confirmLyrics = () => {
  emit('update:lyrics', lyricsText.value)
  showLyricsPopover.value = false
}
</script>

<style lang="less" scoped>
.choice-btn {
  display: flex;
  height: 40px;
  padding: 0 15px;
  justify-content: center;
  align-items: center;
  gap: 5px;
  border-radius: 10px;
  border: 1px solid #E9EAEB;
  background: #f5f6f7;
  cursor: pointer;
  position: relative;
  &:hover { background: #E5E7EB; }
}

.text-music {
  color: #333;
  font-family: "Microsoft YaHei";
  font-size: 14px;
}

.switch-toggle {
  width: 24px;
  height: 14px;
  background: #ccc;
  border-radius: 11px;
  position: relative;
  cursor: pointer;
  transition: background 0.3s ease;
  &.active { background: #000F33; }
  .switch-slider {
    width: 10px;
    height: 10px;
    background: #fff;
    border-radius: 50%;
    position: absolute;
    top: 2px;
    left: 2px;
    transition: transform 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
  &.active .switch-slider { transform: translateX(10px); }
}

.custom-popover-wrapper { position: relative; display: inline-block; }

.custom-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  width: auto;
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.10);
  border-radius: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  z-index: 9999;
  padding: 20px;
}

.input-wrapper { display: flex; align-items: center; gap: 8px; }

.lyrics-input {
  flex: 1;
  height: 200px;
  width: 370px;
  padding: 15px;
  border: none;
  border-radius: 10px;
  background: #F8F9FA;
  font-size: 14px;
  font-family: "Microsoft YaHei";
  color: #333;
  outline: none;
  resize: none;
  &::placeholder { color: #999; }
}

.button-wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 15px;
  padding: 15px;
  background: #F8F9FA;
  border-radius: 0 0 10px 10px;
}

.confirm-btn {
  height: 32px;
  width: 120px;
  padding: 0 20px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-family: "Microsoft YaHei";
  cursor: pointer;
  background: rgba(0, 15, 51, 0.10);
  color: #000F33;
  &:hover { background: #5a62d9; }
}

.popover-enter-active, .popover-leave-active { transition: all 0.3s ease; }
.popover-enter-from, .popover-leave-to { opacity: 0; transform: translateX(-50%) translateY(10px); }
</style>
