<script setup>
import {ref,watch} from 'vue'
const props=defineProps({
    url:{
        type:String,
        defalut:''
    },
    time:0
})

const emits=defineEmits(['updateDuration'])

watch(
    ()=>props.url,
    ()=>{
        if(audioIns.value){
            audioIns.value.src=props.url
        }
        console.log(props.time);
        duration.value=props.time
    }
)

// 格式化音频时间
function formatDuration(e){
    let minus = parseInt(e / 60);
    let sec = parseInt(e % 60);
    return `${minus > 9 ? minus : "0" + minus}分${sec > 9 ? sec : "0" + sec}秒`;
}

function formatTimeNum(e){
    let minus = parseInt(e / 60);
    let sec = parseInt(e % 60);
    return `${minus > 9 ? minus : "0" + minus}:${sec > 9 ? sec : "0" + sec}`;
}

const currentTime=ref(0)//当前播放时间
const play=ref(false)//是否在播放
const audioIns=ref(null)//audio实例
const duration=ref(0)//audio时长


function audioCanplay(e){
    duration.value=e.target.duration
    emits('updateDuration',duration.value)
}

function audioPlay(){
    play.value=true
}

function audioPause(){
    play.value=false
}

function audioTimeupdate(e){
    currentTime.value=e.target.currentTime
}

function audioEnded(){
    currentTime.value=0
    play.value=false
}

function SliderChange(e){
    audioIns.value.currentTime=e
}

function handleChangeAudioStatus(){
    if(play.value){
        audioIns.value.pause()
    }else{
        audioIns.value.play()
    }
}

function handleAudioTimeChange(type){
    if(type==='prev'){
        if(currentTime.value<15){
            audioIns.value.currentTime=0
        }else{
            audioIns.value.currentTime=currentTime.value-15
        }
    }else{
        if(duration.value-currentTime.value<15){
            audioIns.value.currentTime=duration.value
        }else{
            audioIns.value.currentTime=currentTime.value+15
        }
    }
}

</script>

<template>
    <div class="audio-box">
        <audio
            :src="url"
            @canplay="audioCanplay"
            @error="audioError"
            @play="audioPlay"
            @pause="audioPause"
            @timeupdate="audioTimeupdate"
            @ended="audioEnded"
            ref="audioIns"
        />
        <div class="time">时长：{{formatDuration(duration)}}</div>
        <div class="controls-box">
            <img class="time-btn" src="@/assets/imgs/icon_audio_time_prev.png" alt="" @click="handleAudioTimeChange('prev')">
            <div class="status-btn" @click="handleChangeAudioStatus">
                <svg v-show="play" class="play-btn" width="36" height="40" viewBox="0 0 36 40" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <rect width="8" height="40" fill="white"/>
                    <rect x="28" width="8" height="40" fill="white"/>
                </svg>
                <svg v-show="!play" class="play-btn" style="transform: rotate(-180deg);" width="38" height="42" viewBox="0 0 38 42" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M36.8138 19.2476C38.1953 20.0074 38.1953 21.9926 36.8138 22.7524L2.96383 41.3699C1.63093 42.103 5.42246e-07 41.1387 6.0874e-07 39.6175L2.23633e-06 2.38254C2.30282e-06 0.861333 1.63093 -0.102989 2.96384 0.630109L36.8138 19.2476Z" fill="white"/>
                </svg>

            </div>
            <img class="time-btn" src="@/assets/imgs/icon_audio_time_next.png" alt="" @click="handleAudioTimeChange('next')">
        </div>
        <van-slider
            v-model="currentTime"
            :max="duration"
            active-color="#0052D9" 
            inactive-color="#DCDFE6"
            bar-height="3px"
            @change="SliderChange"
            class="slider" 
        >
            <template #button>
                <div class="slider-btn">{{formatTimeNum(currentTime)}}</div>
            </template>
        </van-slider>
    </div>
</template>

<style lang="scss" scoped>
.audio-box{
    width: 100%;
    height: 240px;
    background: #FFFFFF;
    box-shadow: 0px 4px 24px rgba(75, 74, 96, 0.16);
    border-radius: 24px;
    padding: 20px;
    .time{
        font-size: 28px;
        color: $font-grey_999;
    }
    .controls-box{
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 10px;
        margin-bottom: 30px;
        .time-btn{
            width: 60px;
            height: 60px;
        }
        .status-btn{
            margin: 0 62px;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: linear-gradient(180deg, #0052D9 0%, #3682FF 100%);
            transform: rotate(-180deg);
            display: flex;
            justify-content:center;
            align-items: center;
            .play-btn{
                width: 36px;
                height: 40px;
            }
        }
    }
    .slider{
        width: 95%;
        margin: 0 auto;
        .slider-btn{
            background: #333333;
            border-radius: 999px;
            color: #FFFFFF;
            font-size: 18px;
            padding: 0 14px;
            height: 34px;
            display: flex;
            align-items: center;
            line-height: 1;
        }
    }
}
@media screen and (min-width:$media-width){
    .audio-box{
        height: 120px;
        border-radius: 12px;
        padding: 10px;
        box-shadow: 0px 2px 12px rgba(75, 74, 96, 0.16);
        .time{
            font-size: 14px;
        }
        .controls-box{
            margin-top: 5px;
            margin-bottom: 15px;
            .time-btn{
                width: 30px;
                height: 30px;
            }
            .status-btn{
                margin: 0 31px;
                width: 50px;
                height: 50px;
                .play-btn{
                    width: 18px;
                    height: 20px;
                }
            }
        }
        .slider{
            .slider-btn{
                font-size: 10px;
                padding: 0 7px;
                height: 17px;
            }
        }
    }
}
</style>