<script setup>
import { showToast,showDialog } from 'vant';
import apiReport from '@/api/report'
import {useCachedViewsStore} from '@/store/modules/cachedViews'
const cachedViewsStore=useCachedViewsStore()

const props=defineProps({
    reportData:{
        default:''
    }
})

const emits=defineEmits(['close'])

// 确认发布报告
// type 1 仅发布 2发布且推送
function handleConfirmPublish(type){
    if(type==2&&props.reportData.MsgIsSend==1) return
    apiReport.reportPublish({
        ReportIds:props.reportData.Id.toString(),
        ReportUrl:generatePdfLinks(props.reportData.ReportCode)
    }).then(res=>{
        if(res.Ret==200){
            cachedViewsStore.removeCaches('ReportList')
            if(type===2){
                handleReportMessageSend(true)
            }else{
                if(res.Data){
                    showDialog({
                        title: '发布提示',
                        message: res.Data,
                    }).then(()=>{

                    })
                }else{
                    showToast('发布成功')
                }
                emits('close','refresh')
            }
        }
    })
}

function generatePdfLinks(Code){
    return `${publicSettingStore.publicSetting.ReportViewUrl}/reportshare_pdf?code=${Code}&flag=${waterMarkStr.value}`
}

// 推送消息
function handleReportMessageSend(publish){
    apiReport.reportMessageSend({
        ReportId:Number(props.reportData.Id)
    }).then(res=>{
        if(res.Ret===200){
            if(publish){
                showToast('发布且推送成功')
            }else{
                showToast('推送成功')
            }
            emits('close','refresh')
        }
    })
}
</script>

<template>
    <div class="publish-report-pop-box">
        <div class="title">发布提示</div>
        <p class="tips">{{reportData.PrePublishTime?'该报告已设置定时发布，是否立即发布报告并推送模板消息？':'是否立即发布报告，并推送模板消息？'}}</p>
        <div class="btns">
            <div :class="['btn blue',reportData.MsgIsSend===1?'disabled':'']" @click="handleConfirmPublish(2)">发布&推送</div>
            <div class="btn" @click="handleConfirmPublish(1)">仅发布</div>
            <div class="btn" @click="emits('close')">取消</div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.publish-report-pop-box{
    padding: 48px;
    .title{
        font-size: 36px;
        text-align: center;
        margin-bottom: 32px;
    }
    .tips{
        color: $font-grey;
        margin-bottom: 48px;
    }
    .btns{
        .btn{
            line-height: 96px;
            border-radius: 12px;
            text-align: center;
            font-size: 32px;
            font-weight: 600;
            margin-bottom: 24px;
            background-color: #F2F3FF;
            color: $theme-color;
        }
        .blue{
            background-color: $theme-color;
            color: #fff;
        }
        .disabled{
            background-color: #a8b0fc;
        }
    }
}

@media screen and (min-width:$media-width){
    .publish-report-pop-box{
        padding: 24px;
        .title{
            font-size: 18px;
            margin-bottom: 16px;
        }
        .tips{
            margin-bottom: 24px;
        }
        .btns{
            .btn{
                line-height: 48px;
                border-radius: 12px;
                font-size: 16px;
                margin-bottom: 12px;
            }
        }
    }
}
</style>