<template>
    <div class="row h-100 parentContainer">
        <div class="col">
            <loading-component class="fixed-top h-100 bg-white" style="height: 200px; top: 0;" key="1" color="success" v-show="loading"></loading-component>
            <div class="row justify-content-center b-a b-thick h-100 pointer requestModal hide" data-type="crossLoginModal" style="border-color: #ffffff3d">
                <div class="col">
                    <div class="btn btn-block btn-lg btn-primary b-rad-none no-border">Login As Exchange</div>
                </div>
            </div>
            <modal-component styleType="fill-in" type="crossLoginModal">
                <div class="row">
                    <div class="col text-center">
                        <div class="row m-b-20">
                            <div class="col">
                               <iframe style="width: 500px; height: 600px" :src="exchange_url + 'cross-login'"></iframe>   
                            </div>
                        </div>
                        <div class="row">
                            <div class="col p-r-5">
                                <div class="btn btn-sm btn-default btn-block b-rad-none" data-dismiss="modal">Close</div>
                            </div>
                        </div>
                    </div>
                </div>
            </modal-component>
        </div>
    </div>

</template>
<script>
    import authenticationHandler from '../../../general/mixins/accounts/authenticationHandler';

    export default {
        props: {
            section:{
                type: String,
                required: true
            }
        },
        data(){
            return {
                parameters: {
                    access_token: this.email,
                    platform: 1
                },
                exchange_url: process.env.MIX_EXCHANGE_URL,
                loading: false
            }
        },
        methods:{
            iframeEvent(event) {
                // if(event.origin !== this.exchange_url) return;

                if (event.data.data) {
                    this.parameters.access_token = event.data.data.payload.access_token;
                    this.submit(this.route('api.account.authentication.authenticate.cross.attempt'), 'post', this.section, false, false);
                }
            }
        },
        mixins: [authenticationHandler],
        mounted() {
            window.addEventListener("message", this.iframeEvent, false);
        },
    }

</script>