/**
 * 活动模块 公共申请权限
 * @parms res 接口返回数据
 * @param type 活动\会议
 * @param source 来源：我的1、活动2、图库3、研报4
 * @param fromPage 来源: '活动列表'、'活动详情'等
 */
import {apiApplyPermission} from '@/api/user'
import { ElMessage,ElMessageBox } from 'element-plus'
import router from '@/router'
export const applyAuth=(res,type,source,fromPage)=>{
    if(res.code===200){
        const htmlStr=`<h4 style="text-align:center;margin-bottom:5px;font-size:16px;margin-top:0">设置成功</h4>
                    <p style="text-align:center;">关注【弘则研究】公众号，接收会前15分钟微信提醒，并及时获取活动信息变更通知</p>`
        ElMessageBox({
            title:'会议提醒',
            message:htmlStr,
            center: true,
            dangerouslyUseHTMLString: true,
            confirmButtonText:'知道了',
            confirmButtonClass:'self-elmessage-confirm-btn'
        })
    }else if(res.code===4001){
        if(res.data.type=='time'){
            const htmlStr=`<p style="text-align:center;">会议开始前15分钟内无法设置会议提醒</p>`
            ElMessageBox({
                title:'会议提醒',
                message:htmlStr,
                center: true,
                dangerouslyUseHTMLString: true,
                confirmButtonText:'知道了',
                confirmButtonClass:'self-elmessage-confirm-btn'
            })
        }else if(res.data.type=='full'){
            const htmlStr=`<p style="text-align:center;">此活动报名人数已满，请留意下期活动</p>`
            ElMessageBox({
                title:`${type}提醒`,
                message:htmlStr,
                center: true,
                dangerouslyUseHTMLString: true,
                confirmButtonText:'知道了',
                confirmButtonClass:'self-elmessage-confirm-btn'
            })
        }
    }else if(res.code===403){
        if(res.data.type=='contact'){
            const htmlStr=`<p>您暂无权限参加此${type}，若想参加请联系<p>对口销售${res.data.name}：${res.data.mobile}</p></p>`
            ElMessageBox({
                title:`${type}提醒`,
                message:htmlStr,
                center: true,
                dangerouslyUseHTMLString: true,
                confirmButtonText:'知道了',
                confirmButtonClass:'self-elmessage-confirm-btn',
                showCancelButton:true,
                cancelButtonText:'取消',
                cancelButtonClass:'self-elmessage-cancel-btn'
            }).then(()=>{
                // navigator.clipboard.writeText(res.data.mobile)
                // ElMessage('复制成功')
            })
            if(!res.data.customer_info.has_apply){
                if(res.data.customer_info.status=='冻结'||(res.data.customer_info.status=='试用'&&res.data.customer_info.is_suspend==1)){
                    apiApplyPermission({
                        company_name:res.data.customer_info.company_name,
                        real_name:res.data.customer_info.name,
                        source:source,
                        from_page:fromPage
                    }).then((res)=>{
                        if(res.code===200){
                            console.log('主动申请成功');
                        }
                    })
                }
            }
    
        }else if(res.data.type=='apply'){//需要申请
            const htmlStr=`<p>您暂无权限参加此${type}，若想参加可以申请开通哦</p>`
            ElMessageBox({
                title:`${type}提醒`,
                message:htmlStr,
                center: true,
                dangerouslyUseHTMLString: true,
                confirmButtonText:'立即申请',
                confirmButtonClass:'self-elmessage-confirm-btn',
                showCancelButton:true,
                cancelButtonText:'取消',
                cancelButtonClass:'self-elmessage-cancel-btn'
            }).then(()=>{
                if(res.data.customer_info.has_apply){//已经申请过
                    const htmlStr=`<p>您已提交过申请，请耐心等待</p>`
                    ElMessageBox({
                        title:`${type}提醒`,
                        message:htmlStr,
                        center: true,
                        dangerouslyUseHTMLString: true,
                        confirmButtonText:'知道了',
                        confirmButtonClass:'self-elmessage-confirm-btn'
                    })
                }else{
                    if(!res.data.customer_info.status||res.data.customer_info.status!='流失'||res.data.customer_info.status!='关闭'){
                        console.log('跳转申请页');
                        router.push({
                            path:'/apply/permission',
                            query:{
                                source:2,
                                fromPage:'活动列表'
                            }
                        })
                    }else{//主动调一次申请权限接口 
                        apiApplyPermission({
                            company_name:res.data.customer_info.company_name,
                            real_name:res.data.customer_info.name,
                            source:source,
                            from_page:fromPage
                        }).then((res)=>{
                            if(res.code===200){
                                console.log('主动申请成功');
                                const htmlStr=`<p>申请已提交</p><p>请等待销售人员与您联系</p>`
                                ElMessageBox({
                                    title:`${type}提醒`,
                                    message:htmlStr,
                                    center: true,
                                    dangerouslyUseHTMLString: true,
                                    confirmButtonText:'知道了',
                                    confirmButtonClass:'self-elmessage-confirm-btn'
                                })
                            }
                        })
                    }
                }
            })
        }
    }
}