<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search,Plus } from '@element-plus/icons-vue'
import MediaUpload from './components/MediaUpload.vue';
import MediaPlayer from './components/MediaPlayer.vue';

import {apiAudio} from '@/api/media'


const tableQuery = reactive({
    keyWord:'',
    currentPage:1,
    pageSize:10,
    totals:0,
    sortParam:'',
    sortType:''
})

const tableColumns = [
    {label:'音频名称',key:'mediaName',},
    {label:'研究员',key:'authorName',},
    {label:'标签',key:'permission_names',},
    {label:'添加时间',key:'publishedTime',width:250,sortable:true}
]
let mediaUploadShow = ref(false)

const tableLoading = ref(false)
const tableData = ref([])
function getTableData(){
    tableLoading.value = true
    apiAudio.getAudioList({
        PageSize:tableQuery.pageSize,
        CurrentIndex:tableQuery.currentPage,
        SortType:tableQuery.sortType,
        KeyWord:tableQuery.keyWord
    }).then(res=>{
        tableLoading.value = false
        if(res.Ret!==200) return 
        tableData.value = res.Data.List||[]
        tableQuery.totals = res.Data.Paging.Totals||0
    })
}
getTableData()
function handlePageChange(page){
    tableQuery.currentPage = page
    getTableData()
}

let mediaPlayerShow = ref(false)
function handlePreviewAudio(data={}){
    setCurData(data)
    mediaPlayerShow.value = true
}
let curData = ref({})
function setCurData(data={}){
    curData.value = {
        fileUrl:data.src||'',
        mediaName:data.mediaName||'',
        author:data.authorId||'',
        labels:data.permissionIds||'',
        mediaCover:data.coverSrc||'',
        duration:data.mediaPlayMilliseconds||0
    }
}
let modifyType = ref('add')
function handleEdit(data={}){
    setCurData(data)
    modifyType.value = data.mediaId?'edit':'add'
    curData.value.mediaId = data.mediaId||0
    mediaUploadShow.value = true
}
function handleDelete(data){
    ElMessageBox.confirm(
        '删除后不可恢复，是否确认删除该音频？',
        '提示',
        {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        }
    )
    .then(() => {
        apiAudio.deleteAudio({
            AudioId:data.mediaId
        }).then(res=>{
            if(res.Ret!==200) return 
            ElMessage.success('删除成功')
            getTableData()
        })
    }).catch(() => {})
}
function handleSortChange({order,prop}){
    tableQuery.sortType = order==='ascending'?'asc':'desc'
    getTableData()
}
</script>

<template>
    <div class="audio-list-wrap">
        <div class="top-box">
            <el-button type="primary" :icon="Plus" @click="handleEdit" v-permission="'media:uploadAudio'">上传音频</el-button>
            <el-input style="width:400px;margin-left: auto;" placeholder="音频名称" 
                v-model="tableQuery.keyWord" :prefix-icon="Search" clearable 
                @input="handlePageChange(1)"/>
        </div>
        <div class="table-box">
            <el-table stripe border :data="tableData" @sort-change="handleSortChange" element-loading-text="数据加载中..." v-loading="tableLoading">
                <el-table-column 
                    v-for="column in tableColumns" :key="column.key"
                    :prop="column.key" :label="column.label" :sortable="column.sortable" :width="column.width">
                    <template #default="{row}">
                        <span v-if="column.key==='mediaName'" @click="handlePreviewAudio(row)" style="color:#086CE0;cursor: pointer;">
                            {{ row.mediaName }}
                        </span>
                        <span v-else>{{ row[column.key]||'-' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template #default="{row}">
                        <el-link type="primary" :underline="false" @click="handleEdit(row)" style="margin-right: 20px;" v-permission="'media:editAudio'">编辑</el-link>
                        <el-link type="danger" :underline="false" @click="handleDelete(row)" v-permission="'media:deleteAudio'">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                background
                layout="total,prev,pager,next,jumper"
                :current-page="tableQuery.currentPage"
                :page-size="tableQuery.pageSize"
                :total="tableQuery.totals"
                @current-change="handlePageChange"
                style=" justify-content: flex-end;border:1px solid #ebeef5;border-top: none;"
            />
        </div>
    </div>
    <MediaUpload
        v-model:show="mediaUploadShow"
        mediaType="audio"
        :modifyType="modifyType"
        :modifydata="curData"
        @save="getTableData"
    ></MediaUpload>
    <MediaPlayer 
        v-model:show="mediaPlayerShow"
        :title="curData.mediaName"
        :src="curData.fileUrl"
        mediaType="audio"
    >
    </MediaPlayer>
</template>

<style scoped lang="scss">
.audio-list-wrap{
    .top-box{
        display: flex;
        margin-bottom: 20px;
    }
}
</style>
