<script setup name="PPTDetail">
import { ref,nextTick} from 'vue'
import { useRoute, useRouter } from "vue-router";
import { vElementSize } from '@vueuse/components'
import {apiPPTDetail} from '@/api/ppt'
import {createPPTContent,getTemplate,getPPTConfig} from './hooks/createPPTContent'
import {usePPTPublish} from './hooks/usePPTPublish'
import {useClassify} from './hooks/useClassify'
import {useUserInfo} from '@/hooks/common'
import {useDownLoadFile} from '@/hooks/useDownLoadFile'
import { useWindowScroll } from '@vueuse/core'
import moment from 'moment';
import { showToast } from 'vant';
import { useConfigSettingStore } from '@/store/modules/etaConfig'

const route=useRoute()
const router=useRouter()

const pptId=route.query.id

const userInfo =useUserInfo()
const { y } = useWindowScroll()

// 获取ppt详情
let PPTInfo=ref(null)
let conArr=ref([])
async function getPPTDetail(){
    await useConfigSettingStore().getBaseConfigSetting()
    const res=await apiPPTDetail({PptId:Number(pptId)})
    if(res.Ret!=200) return
    PPTInfo.value=res.Data
    conArr.value=createPPTContent(res.Data)
    nextTick(()=>{
        onResize({width:document.getElementsByClassName('ppt-content-wrap')[0].clientWidth})
        $('.ppt-item-page').css('background-image',`url(${getPPTConfig().pptBgImage})`);
    })
    document.title=res.Data.Title
    handleShowPPTOpt(true)
}
getPPTDetail()

// 监听页面尺寸变化缩放页面
let pptContentHeight=ref(0)//ppt内容缩放后的真实高度
function onResize({width,height}){
    const scale=width/900//固定了ppt大小为900:630
    const el=document.getElementsByClassName('ppt-content-wrap')[0]
    if(!el) return
    pptContentHeight.value=height*scale+60
    el.style.transform=`scale(${scale})`
}

// 点击发布PPT
function handllePublishPPT(){
    usePPTPublish(conArr.value,pptId)
}


const {
    getPPTClassifyData,
    
    PPTOptState,
    handlePPTShare,
    handleShowPPTCopy,
    handlePPTDel,
    handlePPTCopy,
    handlePPTCopyBeforeClose,

    permissionBtn,
    checkAuthBtn

}=useClassify()

getPPTClassifyData()

// 显示更多操作
function handleShowPPTOpt(init){
    PPTOptState.data={
        AdminId: PPTInfo.value.AdminId,
        AdminRealName: PPTInfo.value.AdminRealName,
        IsSingleShare: PPTInfo.value.IsShare,
        IsSingleShareBoolean:PPTInfo.value.IsShare?true:false,
        PptCreateTime: moment(PPTInfo.value.CreateTime).format('YYYY-MM-DD HH:mm:ss'),
        PptId: PPTInfo.value.PptId,
        PptxUrl: PPTInfo.value.PptxUrl,
        Title: PPTInfo.value.Title
    }
    PPTOptState.isCommon=userInfo.value.AdminId!=PPTInfo.value.AdminId?true:false
    if(init===true) return
    PPTOptState.show=true
}

// 播放
const playActions=[
    {name:"从首页播放",type:1},
    {name:"从当前页播放",type:2},
]
let showPlayOpt=ref(false)
function onSelectPlayOpt(e){
    let index=0//当前第几个ppt是在页面顶部
    if(e.type==2){
        const itemH=pptContentHeight.value/conArr.value.length
        index=Math.floor(y.value/itemH)
    }
    // // 本来可以定义属性close-on-click-action自动关闭弹窗的
    // //不知道为啥加了keepalive 返回时他弹窗又会自动出来 所以加了v-if
    // //并且手动控制收起
    // showPlayOpt.value=false
    // teleport="body" 可解决
    if(window.location.pathname.startsWith('/ppten')){
        router.push({
            path:"/ppten/preview",
            query:{
                id:pptId,
                index
            }
        })
    }else{
        router.push({
            path:"/ppt/preview",
            query:{
                id:pptId,
                index
            }
        })
    }
    
}


// 下载文件
const {fileSize,progress,startDownload,cancelDownload}=useDownLoadFile()
let showDownload=ref(false)
function handleDownLoadFile(){
    if(!PPTInfo.value.PptxUrl){
        showToast('请先发布ppt')
        return
    }
    showDownload.value=true
    startDownload(PPTInfo.value.PptxUrl,PPTInfo.value.Title+'.pptx')
} 


const showPopover=ref(false)

</script>

<template>
    <div class="ppt-detail-page" :style="{height:pptContentHeight+'px'}">
        <!-- pad端顶部操作栏 -->
        <div class="pad-top-action-wrap">
            <div class="item-box" @click="handllePublishPPT" v-permission="permissionBtn.ppt_publish">
                <img src="@/assets/imgs/ppt/icon_action_publish.png" alt="">
                <span>发布</span>
            </div>
            <div class="item-box" @click="showPlayOpt=true" v-permission="permissionBtn.ppt_show">
                <img src="@/assets/imgs/ppt/icon_action_play.png" alt="">
                <span>播放</span>
            </div>
            <div class="item-box" @click="handleShowPPTCopy({pptid:$route.query.id})" v-permission="permissionBtn.ppt_copy">
                <img src="@/assets/imgs/ppt/icon_action_copy.png" alt="">
                <span>复制</span>
            </div>
            <div class="item-box" @click="handleDownLoadFile" v-permission="permissionBtn.ppt_download">
                <img src="@/assets/imgs/ppt/icon_action_download2.png" alt="">
                <span>下载</span>
            </div>
            <div class="item-box red" @click="handlePPTDel({back:true})" v-if="!PPTOptState.isCommon&&checkAuthBtn(permissionBtn.ppt_del)">
                <img src="@/assets/imgs/icon_del.png" alt="">
                <span>删除</span>
            </div>
            <div class="item-box" style="flex-direction:row" v-if="!PPTOptState.isCommon&&checkAuthBtn(permissionBtn.ppt_visible)">
                <span style="margin-right:4px">公开</span>
                <van-switch 
                    size="22px" 
                    :model-value="PPTOptState.data.IsSingleShareBoolean"
                    @update:model-value="handlePPTShare"
                />
            </div>
        </div>
        <div class="ppt-content-wrap" v-element-size="onResize">
            <template v-for="(item,index) in conArr" :key="item.id">
                <component 
                    :is="getTemplate(item.modelId)" 
                    :pageData="{...item,pptPageIndex:index}"
                />
            </template>
        </div>
        <div class="mobile-fix-bot-warp">
            <div class="item-box" @click="handllePublishPPT" v-permission="permissionBtn.ppt_publish">
                <img src="@/assets/imgs/ppt/icon_action_publish.png" alt="">
                <span>发布</span>
            </div>
            <div class="item-box" @click="showPlayOpt=true" v-permission="permissionBtn.ppt_show">
                <img src="@/assets/imgs/ppt/icon_action_play.png" alt="">
                <span>播放</span>
            </div>
            <div class="item-box" @click="handleShowPPTCopy({pptid:$route.query.id})" v-permission="permissionBtn.ppt_copy">
                <img src="@/assets/imgs/ppt/icon_action_copy.png" alt="">
                <span>复制</span>
            </div>
            <div class="item-box" @click="handleShowPPTOpt">
                <img src="@/assets/imgs/ppt/icon_action_more.png" alt="">
                <span>更多</span>
            </div>
        </div>
    </div>
    <!-- ppt编辑弹窗 -->
    <van-popup 
        v-model:show="PPTOptState.show" 
        round
        position="bottom"
    >
        <div class="file-opt-wrap ppt-opt-wrap">
            <div class="top-box">
                 <img src="@/assets/imgs/ppt/icon01.png" alt="">
                 <div class="title">
                    <div class="name">{{PPTOptState.data.Title}}</div>
                    <div class="person">
                            {{PPTOptState.data.AdminRealName}}
                            {{PPTOptState.data.PptCreateTime.split(' ')[0]}}
                            创建
                    </div>
                 </div>
                 <div class="share" v-if="!PPTOptState.isCommon&&checkAuthBtn(permissionBtn.ppt_visible)">
                       <span class="share-font">公开</span>
                        <van-switch 
                            size="16px" 
                            :model-value="PPTOptState.data.IsSingleShareBoolean"
                            @update:model-value="handlePPTShare"
                        />
                 </div>
            </div>
            <div class="item border blue" @click="handleDownLoadFile" v-permission="permissionBtn.ppt_download">
                <img src="@/assets/imgs/ppt/icon_action_download.png" alt="">
                <span>下载</span>
            </div>
            <div class="item border red" @click="handlePPTDel({back:true})" v-if="!PPTOptState.isCommon&&checkAuthBtn(permissionBtn.ppt_del)">
                <img src="@/assets/imgs/icon_del.png" alt="">
                <span>删除</span>
            </div>
        </div>
    </van-popup>
    <!-- ppt复制弹窗 -->
    <van-dialog 
        v-model:show="PPTOptState.showCopy" 
        title="PPT目录" 
        show-cancel-button
        confirmButtonText="保存"
        @confirm="handlePPTCopy"
        :before-close="handlePPTCopyBeforeClose"
    >
        <div class="ppt-copy-wrap">
            <span>选择目录</span>
            <van-popover v-model:show="showPopover">
                <ul class="select-copy-val-box">
                    <li 
                        class="item" 
                        v-for="item in PPTOptState.copyActions" 
                        :key="item.GroupId"
                        @click="PPTOptState.copySelectData=item;showPopover=false"
                    >{{item.text}}</li>
                </ul>
                <template #reference>
                    <span 
                        :class="['select-value-box',!PPTOptState.copySelectData.GroupName&&'grey-text']"
                        >{{PPTOptState.copySelectData.GroupName||'请选择目录'}}</span>
                </template>
            </van-popover>
        </div>
    </van-dialog>
    <!-- 播放选项弹窗 -->
    <van-action-sheet
        teleport="body"
        v-model:show="showPlayOpt" 
        :actions="playActions" 
        cancel-text="取消"
        close-on-click-action
        @select="onSelectPlayOpt" 
    />

    <!-- 下载弹窗 -->
    <van-popup 
        v-model:show="showDownload"
        position="center"
        round
    >
        <div class="download-file-wrap">
            <img class="icon" src="@/assets/imgs/ppt/icon01.png" alt="">
            <div class="title">{{PPTInfo.Title}}.pptx</div>
            <div class="size">文件大小：{{fileSize}}MB</div>
            <div class="process-box">
                <van-progress 
                    style="flex:1"
                    :percentage="progress"
                    :show-pivot="false" 
                    :color="progress==100?'#2DA641':'#0052D9'"
                />
                <span v-if="progress<100">{{progress}}%</span>
                <img v-if="progress==100" width="28"  height="28" src="@/assets/imgs/icon_success.png" alt="">
            </div>
            <div class="cancel-btn" v-show="progress<100" @click="cancelDownload();showDownload=false">取消下载</div>
        </div>
    </van-popup>
</template>

<style lang="scss">
@import "./style/common.scss";
.select-copy-val-box{
    min-width: 150PX;
    padding: 10PX;
    max-height: 300PX;
    overflow-y: auto;
    &::-webkit-scrollbar{
        display: none;
    }
    .item{
        border-bottom: 1px solid $border-color;
        font-size: 14PX;
        padding: 10PX 0;
    }
}
</style>
<style lang="scss" scoped>
.ppt-detail-page{
    width: 100%;
    position: relative;
    overflow: hidden;
    .ppt-content-wrap{
        width: 100%;
        position: absolute;
        transform-origin: 0 0;
    }

}
.mobile-fix-bot-warp{
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100px;
    background: #FFFFFF;
    border-top: 1px solid $border-color;
    z-index: 999;
    display: flex;
    .item-box{
        height: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        img{
            width: 44px;
        }
        span{
            font-size: 20px;
        }
    }

}
.file-opt-wrap{
    .item{
        padding: 42px;
        display: flex;
        align-items: center;
        img{
            width: 32px;
            margin-right: 20px;
        }
    }
    .border{
        border-bottom: 1px solid $border-color;
    }
    .blue{
        color: $theme-color;
    }
    .red{
        color: $theme-red;
    }
}
.ppt-opt-wrap{
    .top-box{
        box-sizing: border-box;
        padding: 40px 40px 21px 40px;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        border-bottom: 1px solid $border-color;
        img{
            width: 80px;
            height: 80px;
            margin-right: 23px;
        }

        .title{
            flex: 1;
            .name{
                color: #333;
                font-size: 28px;
                font-weight: 400;
            }
            .person{
                font-size: 24px;
                color: #999999;
                margin-top: 20px;
            }
        }

        .share{
            height: 80px;
            margin-left: 32px;
            display:flex;
            align-items: center;
            .share-font{
                color: #666666;
                font-size: 24px;
                margin-right: 20px
            }
        }
    }
}
.ppt-copy-wrap{
    display: flex;
    align-items: center;
    padding: 50px 30px;
    span{
        flex-shrink: 0;
        margin-right: 20px;
    }
    :deep(.van-popover__wrapper){
        flex: 1;
    }
    .select-value-box{
        display: block;
        height: 70px;
        line-height: 70px;
        padding: 0 30px;
        border-radius: 70px;
        border: 1px solid $border-color;
        min-width: 150px;
        &.grey-text{
            color: $font-grey;
        }
    }
}
.pad-top-action-wrap{
    display: none;
}

.download-file-wrap{
    width: 600px;
    max-width: 500PX;
    min-height: 300PX;
    background-color: #fff;
    text-align: center;
    padding: 0 16PX 30PX 16PX;
    .icon{
        width: 120px;
        max-width: 60PX;
        display: block;
        margin: 50PX auto 20PX auto;
    }
    .title{
        margin-bottom: 10PX;
    }
    .size{
        color: $font-grey_999;
        font-size: 12PX;
    }
    .process-box{
        margin-top: 30PX;
        display: flex;
        align-items: center;
        span{
            color: $theme-color;
            margin-left: 5PX;
            flex-shrink: 0;
        }
    }
    .cancel-btn{
        max-width: 240PX;
        width: 80%;
        margin: 60PX auto 0 auto;
        background-color: #F4F6F8;
        text-align: center;
        line-height: 40PX;
        border-radius: 40PX;
    }
}

@media screen and (min-width:$media-width){
    .ppt-detail-page{
        margin-top: 60px;
    }
    .mobile-fix-bot-warp{
        display: none;
    }
    .pad-top-action-wrap{
        display: flex;
        position: fixed;
        top: 60px;
        left: 0;
        right: 0;
        z-index: 999;
        height: 60px;
        background-color: #fff;
        padding-left: 34px;
        .item-box{
            height: 100%;
            // flex: 1;
            margin-right: 43px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            img{
                width: 20px;
            }
            span{
                font-size: 16px;
            }
            
        }
        .red{
            color: $theme-red;
        }
    }
    .ppt-copy-wrap{
        padding: 50px 30px;
        span{
            flex-shrink: 0;
            margin-right: 10px;
        }
        :deep(.van-popover__wrapper){
            flex: 1;
        }
        .select-value-box{
            display: block;
            height: 40px;
            line-height: 40px;
            padding: 0 30px;
            border-radius: 70px;
            border: 1px solid $border-color;
            min-width: 120px;
        }
    }
}
</style>