<template>
  <view class="music-container">

    <!-- 音色列表 -->
    <view class="voice-list">
      <view 
        v-for="(voice, index) in voiceList" 
        :key="voice.voiceType"
        class="voice-item"
        :class="{ 'selected': voice.voiceType === selectedVoiceId }"
        @click="selectedVoiceId = voice.voiceType"
      >
        <view class="voice-avatar">
          <view class="play-icon">
            <uv-icon 
              :name="voice.voiceType === selectedVoiceId ? 'pause' : 'play-right-fill'" 
              size="24" 
              color="#666666"
            ></uv-icon>
          </view>
        </view>
        <view class="voice-info">
          <view class="voice-name">{{ voice.name }}</view>
          <view class="voice-desc">{{ voice.info }}</view>
        </view>
        <view v-if="voice.voiceType === selectedVoiceId" class="selected-tag">
          已选择
        </view>
      </view>
    </view>

    <!-- 底部固定确认按钮 -->
    <view class="bottom-bar">
      <uv-button 
        type="primary" 
        text="确定" 
        :custom-style="{
          width: '100%',
          height: '82rpx',
          borderRadius: '44rpx',
          backgroundColor: '#06DADC',
          fontSize: '36rpx',
          fontWeight: '500',
          border: '1px solid #06DADC'
        }"
        @click="confirmSelection"
      ></uv-button>
      <uv-safe-bottom></uv-safe-bottom>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectedVoiceId: '', // 默认选择智小虎
      voiceList: [

      ]
    }
  },
  computed: {
    // 从Vuex获取音色列表
    storeVoiceList() {
      return this.$store.state.voiceList;
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    selectVoice() {
      // console.log('选择音色:', this.selectedVoiceId)
      uni.$emit('selectVoice', this.selectedVoiceId)
      uni.navigateBack()
    },
    confirmSelection() {
      this.selectVoice(this.selectedVoiceId)
      // 保存选择的音色到Vuex
      this.$store.dispatch('setDefaultVoice', this.selectedVoiceId);
    },
    async getVoice(){
      // 优先从Vuex获取音色列表
      if (this.storeVoiceList && this.storeVoiceList.length > 0) {
        console.log('从Vuex获取音色列表:', this.storeVoiceList);
        this.voiceList = this.storeVoiceList;
        return;
      }
      
      // 如果Vuex中没有数据，则从API获取（兜底方案）
      console.log('Vuex中无音色数据，从API获取...');
      try {
        const listRes = await this.$api.music.list()
        if (listRes.code === 200) {
          this.voiceList = listRes.result
          // 更新Vuex中的音色列表
          this.$store.commit('setVoiceList', listRes.result);
        } else {
          console.error('获取音色列表失败:', listRes);
        }
      } catch (error) {
        console.error('获取音色列表异常:', error);
      }
    }
  },
  onLoad(options) {
    this.selectedVoiceId = Number(options.voiceId) 
    // console.log( 'options.voiceId', options.voiceId );
    
    this.getVoice()
  }
}

</script>

<style scoped lang="scss">
.music-container {
  background: #fff;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: 120rpx;
}

.voice-list {
  flex: 1;
  padding: 40rpx 32rpx 120rpx;
  display: flex;
  flex-direction: column;
  gap: 32rpx;
}

.voice-item {
  display: flex;
  align-items: center;
  padding: 32rpx 0;
  border-bottom: 4rpx solid #F8F8F8;
  position: relative;
  background: #F8F8F8;
  gap: 12px;
  opacity: 1;
  border-radius: 8px;
  padding: 8px;
  transition: all 0.3s ease;
  &:last-child {
    border-bottom: none;
  }
  
  &.selected {
    border-bottom: 4rpx solid $primary-color;
  }
}

.voice-avatar {
  width: 136rpx;
  height: 136rpx;
  border-radius: 50%;
  background: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  // margin-right: 32rpx;
  position: relative;
}

.play-icon {
  display: flex;
  width: 60rpx;
  height: 60rpx;
  border-radius: 100rpx;
  background: white;
  align-items: center;
  justify-content: center;
}

.voice-info {
  flex: 1;
}

.voice-name {
  font-family: PingFang SC;
  font-weight: 600;
  font-size: 36rpx;
  color: #262626;
  margin-bottom: 8rpx;
}

.voice-desc {
  font-family: PingFang SC;
  font-weight: 400;
  font-size: 28rpx;
  color: #999;
}

.selected-tag {
  background: $primary-color;
  color: #fff;
  font-size: 24rpx;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-family: PingFang SC;
  font-weight: 500;
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 24rpx 50rpx;
  z-index: 999;
  border-top: 2rpx solid #F1F1F1
}
</style>