<template>
    <div>
        <!-- <el-dialog
            title="请输入账号名称"
            :visible.sync="dialogVisible"
            width="30%"
            :show-close="false"
            :before-close="handleClose">
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </span>
        </el-dialog> -->

    </div>
</template>
<script>
// 广告回调页
import { adAuthSuccess, adtAuthSuccess, adtDoAuth, adtYwdyDoAuth } from "@/api/accounts/adApi";
export default {
    data() {
        return {
            dialogVisible: true,
            accountData: {}
        }
    },
    created() {
        console.log('created', localStorage.getItem('accountData'))
        this.accountData = JSON.parse(localStorage.getItem('accountData'))
        this.handleSubmit()
    },
    methods: {
        handleClose() {

        },
        handleSubmit() {
            if (window.location.hash.indexOf('?') !== -1) {
                let query = {
                    authorizationCode: this.$route.query.authorization_code,
                    callbackPage: encodeURIComponent(window.location.origin + '/adCode'),
                    state: this.$route.query.state
                }
                let sqType = sessionStorage.getItem('ADSQ')
                if (sqType === '1') {
                    adtDoAuth(query).then(response => {
                        let data = response.data
                        if (data) {
                            this.$message({
                                message: '授权成功',
                                type: 'success'
                            });
                            localStorage.setItem("adMp", "ok");
                        } else {
                            this.$message({
                                message: '授权失败',
                                type: 'error'
                            });
                            localStorage.setItem("adMp", "false");
                            return
                        }
                        setTimeout(() => {
                            window.close()
                        }, 200)
                    }).catch(err => {
                        // setTimeout(()=>{
                        //     window.close()
                        // }, 200)
                    });
                } else if (sqType === '2') {
                    adtYwdyDoAuth(query).then(response => {
                        console.log('response-->', response)
                        let data = response.data
                        if (data) {
                            this.$message({
                                message: '授权成功',
                                type: 'success'
                            });
                            localStorage.setItem("adMp", "ok");
                        } else {
                            this.$message({
                                message: '授权失败',
                                type: 'error'
                            });
                            localStorage.setItem("adMp", "false");
                            return
                        }
                        setTimeout(() => {
                            window.close()
                        }, 200)
                    }).catch(err => {
                        // setTimeout(()=>{
                        //     window.close()
                        // }, 200)
                    });
                } else {
                    this.$message({
                        message: '授权失败，请联系管理员',
                        type: 'error'
                    });
                }

                // if(localStorage.getItem('adType') != "oldType"){
                //     localStorage.removeItem('adType')
                //     let accountName = ""
                //     if(this.accountData.type == 1){
                //         accountName = this.accountData.nickName
                //     }else{
                //         accountName = this.accountData.principalName
                //     }
                //     let query = {
                //         authorizationCode: this.$route.query.authorization_code,
                //         callbackPage: window.location.origin + '/admin/adCode',
                //         accountName
                //     }
                //     adAuthSuccess(query).then(response => {
                //         this.dialogVisible = false
                //         let data = response.data
                //         localStorage.removeItem('accountData')
                //         if (data) {
                //             this.$message({
                //                 message: '授权成功',
                //                 type: 'success'
                //             });
                //             localStorage.setItem("adMp", "ok");
                //         } else {
                //             this.$message({
                //                 message: '授权失败',
                //                 type: 'error'
                //             });
                //             localStorage.setItem("adMp", "false");
                //             return
                //         }
                //         setTimeout(()=>{
                //             window.close()
                //         }, 200)
                //     }).catch(err => {
                //         localStorage.removeItem('accountData')
                //         this.dialogVisible = false
                //         // setTimeout(()=>{
                //         //     window.close()
                //         // }, 200)
                //         localStorage.setItem("adMp", "false");
                //     });
                // }else{
                //     localStorage.removeItem('adType')
                //     let query = {
                //         authorizationCode: this.$route.query.authorization_code,
                //         callbackPage: window.location.origin + '/admin/adCode'
                //     }
                //     adtAuthSuccess(query).then(response => {
                //         this.dialogVisible = false
                //         let data = response.data
                //         localStorage.removeItem('accountData')
                //         if (data) {
                //             this.$message({
                //                 message: '授权成功',
                //                 type: 'success'
                //             });
                //             localStorage.setItem("adtMp", "ok");
                //         } else {
                //             this.$message({
                //                 message: '授权失败',
                //                 type: 'error'
                //             });
                //             localStorage.setItem("adtMp", "false");
                //             return
                //         }
                //         setTimeout(()=>{
                //             window.close()
                //         }, 200)
                //     }).catch(err => {
                //         localStorage.removeItem('accountData')
                //         this.dialogVisible = false
                //         // setTimeout(()=>{
                //         //     window.close()
                //         // }, 200)
                //         localStorage.setItem("adMp", "false");
                //     });
                // }


            }
        }
    }
}
</script>