<?php
namespace core_next;
$url = Config::$auth_url;
wp_enqueue_media();
$auth_data = Options::getAuthSetData();
$ajax_url = admin_url('admin-ajax.php?action=' . Config::$theme_name);
global $core_next_set;
?>
<iframe id="auth_iframe" src="<?php echo $url ?>"></iframe>
<script>
    let auth_iframe = document.querySelector('#auth_iframe');
    auth_iframe.onload = () => {
        updateIframeHeight();
    }
    let auth_str = '<?php echo $auth_data?>';

    let auth_data = auth_str;

    window.addEventListener('message', function (event) {
        let iframe_data = event.data;
        if (iframe_data.type === 'setHeight') {
            auth_iframe.style.height = iframe_data.data + 'px';
        }
        if (iframe_data.type === 'postSet') {
            postMessageToAuth('postSet', auth_data);
        }
        if (iframe_data.type === 'saveSet') {
            let data = iframe_data.data;
            jQuery.post('<?php echo $ajax_url?>', {data, fun: 'saveSet'}, e => {
                let obj = JSON.parse(e);
                let send_data = {code: 200}
                if (obj.code !== 200) {
                    send_data = {code: 500}
                }
                postMessageToAuth('returnSave', send_data);
            })
        }
        if (iframe_data.type === 'setImgUrl') {
            let hash = iframe_data.data;
            let upload_frame = wp.media({
                title: '选择图片',
                button: {
                    text: '插入',
                },
                multiple: false
            });
            upload_frame.on('select', () => {  //里面是选择图片后的动作，把图片地址赋值给input
                let attachment = upload_frame.state().get('selection').first().toJSON();
                let url = attachment.url;
                postMessageToAuth('setImgUrl', {url, hash});
            });
            upload_frame.open();
        }
        if (iframe_data.type === 'testMail') {
            let data = iframe_data.data;
            //console.log('邮箱数据', data);
            data.fun = 'testSendMail';
            jQuery.post('<?php echo $ajax_url?>', data, e => {
                let obj = JSON.parse(e);
                let send_data = {code: 200}
                if (obj.code !== 200) {
                    send_data = {code: 500}
                }
                postMessageToAuth('testMail', send_data);
            })
        }
        if (iframe_data.type === 'testLocation') {
            let data = iframe_data.data;
            data.fun = 'testLocation';
            jQuery.post('<?php echo $ajax_url?>', data, e => {
                let obj = JSON.parse(e);
                let send_data = {code: 200,data:obj.data}
                if (obj.code !== 200) {
                    send_data = {code: 500}
                }
                postMessageToAuth('testLocation', send_data);
            })
        }
    }, false);

    function postMessageToAuth(type, data) {
        auth_iframe.contentWindow.postMessage(createdPostMessage(type, data), '<?php echo $url ?>');
    }

    function createdPostMessage(type, data) {
        return {
            type, data
        }
    }

    function updateIframeHeight() {
        let update_nag = 0;
        if (jQuery('.update-nag').height() != undefined) {
            update_nag = jQuery('.update-nag').height();
        }
        auth_iframe.height = jQuery('#wpwrap').height() - jQuery('#wpfooter').height() - update_nag - jQuery('#wpadminbar').height() - 65;
    }

    jQuery(window).resize(function () {
        updateIframeHeight();
    });
</script>
<style>
    #auth_iframe {
        width: 100%;
        padding: 0;
        margin: 0;
    }
</style>