{"id":195,"date":"2025-09-04T16:57:48","date_gmt":"2025-09-04T13:57:48","guid":{"rendered":"http:\/\/ambu.shtanko.com.ua\/?page_id=195"},"modified":"2025-09-04T16:58:25","modified_gmt":"2025-09-04T13:58:25","slug":"test","status":"publish","type":"page","link":"https:\/\/integration.ambu.org.ua\/en\/test\/","title":{"rendered":"test"},"content":{"rendered":"\n\n<div class=\"diia-share-container\">\n    <div class=\"diia-share-button-wrapper\">\n        <button id=\"diia-share-button\" class=\"diia-button\" data-type=\"all\">\n            <img src=\"https:\/\/integration.ambu.org.ua\/wp-content\/plugins\/diia-integration\/public\/images\/diia-logo.svg\" alt=\"Diia Logo\" class=\"diia-logo\">\n            Share Document        <\/button>\n    <\/div>\n\n    <div id=\"diia-share-qrcode\" class=\"diia-qrcode-container\" style=\"display: none;\">\n        <div class=\"diia-qrcode-wrapper\">\n            <div id=\"diia-share-qr\"><\/div>\n            <p class=\"diia-qrcode-instruction\">Scan this QR code with your Diia app to share your document<\/p>\n        <\/div>\n        <button id=\"diia-share-cancel\" class=\"diia-button diia-button-secondary\">\n            Cancel        <\/button>\n    <\/div>\n\n    <div id=\"diia-share-result\" class=\"diia-result-container\" style=\"display: none;\"><\/div>\n\n    <div id=\"diia-shared-document\" class=\"diia-shared-document-container\" style=\"display: none;\">\n        <h3>Shared Document<\/h3>\n        <div class=\"diia-document\">\n            <div class=\"diia-document-header\">\n                <h4 class=\"diia-document-title\"><\/h4>\n                <span class=\"diia-document-type\"><\/span>\n            <\/div>\n            <div class=\"diia-document-content\">\n                <div class=\"diia-document-image\">\n                    <img src=\"\" alt=\"\">\n                <\/div>\n                <div class=\"diia-document-details\"><\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n<\/div>\n\n<script type=\"text\/javascript\">\n    \/\/ NOTE: This template uses the same real session mechanics as auth: start, render QR, poll, cancel.\n    jQuery(document).ready(function($) {\n        \/\/ Share button click handler\n        $('#diia-share-button').on('click', function() {\n            initiateDocumentSharing($(this).data('type'));\n        });\n\n        \/\/ Cancel button click handler\n        $('#diia-share-cancel').on('click', function() {\n            cancelDocumentSharing();\n        });\n\n        function initiateDocumentSharing(documentType) {\n            $('.diia-share-button-wrapper').hide();\n            $('#diia-share-result').hide();\n\n            \/\/ Initiate share session on the server and get QR data\n            $.post(diiaIntegration.ajaxUrl, {\n                action: 'diia_start_share',\n                nonce: diiaIntegration.nonce,\n                doc_type: documentType || 'all'\n            }).done(function(resp) {\n                if (!resp || !resp.success) {\n                    showShareError(resp && resp.data && resp.data.message ? resp.data.message : diiaIntegration.i18n.shareError);\n                    return;\n                }\n                const data = resp.data || {};\n                const sessionId = data.session_id;\n                if (!sessionId) {\n                    showShareError('Missing session id');\n                    return;\n                }\n                renderShareQR(data);\n                $('#diia-share-qrcode').show();\n                pollShareStatus(sessionId);\n            }).fail(function() {\n                showShareError(diiaIntegration.i18n.shareError);\n            });\n        }\n\n        function renderShareQR(data) {\n            const $box = $('#diia-share-qr');\n            $box.empty();\n            if (data.session_id) {\n                $box.data('session-id', data.session_id);\n            }\n            if (data.qr_url) {\n                \/\/ data.qr_url is already a fully built URL; don't re-encode\n                $box.append('<img alt=\"Diia QR\" class=\"diia-qr-img\" src=\"' + data.qr_url + '\"\/>');\n                \/\/ Also render a clickable link for devices that do not detect custom schemes\n                if (data.qr_string) {\n                    $box.append('<div class=\"diia-qr-link\"><a href=\"' + data.qr_string + '\" target=\"_blank\" rel=\"noopener\">' + (data.qr_string.length > 64 ? data.qr_string.substring(0,64)+'\u2026' : data.qr_string) + '<\/a><\/div>');\n                }\n                return;\n            }\n            if (data.qr_svg) {\n                $box.html(data.qr_svg);\n                return;\n            }\n            if (data.qr_string) {\n                const src = 'https:\/\/api.qrserver.com\/v1\/create-qr-code\/?size=320x320&qzone=4&ecc=Q&data=' + encodeURIComponent(data.qr_string);\n                $box.append('<img alt=\"Diia QR\" class=\"diia-qr-img\" src=\"' + src + '\"\/>' );\n                return;\n            }\n            $box.html('<div class=\"diia-qrcode-placeholder\"><p>QR not available<\/p><\/div>');\n        }\n\n        function pollShareStatus(sessionId) {\n            let attempts = 0;\n            const maxAttempts = 60;\n            const interval = setInterval(function() {\n                attempts++;\n                $.post(diiaIntegration.ajaxUrl, {\n                    action: 'diia_poll_share',\n                    nonce: diiaIntegration.nonce,\n                    session_id: sessionId\n                }).done(function(resp) {\n                    if (!resp || !resp.success) {\n                        return;\n                    }\n                    const data = resp.data || {};\n                    if (data.status === 'approved' || data.status === 'success' || data.shared) {\n                        clearInterval(interval);\n                        handleSuccessfulShare(data.document || data);\n                    } else if (data.status === 'declined' || data.status === 'expired' || data.status === 'canceled') {\n                        clearInterval(interval);\n                        showShareError(data.message || diiaIntegration.i18n.shareError);\n                        cancelDocumentSharing();\n                    }\n                });\n                if (attempts >= maxAttempts) {\n                    clearInterval(interval);\n                    showShareError('Share timeout');\n                    cancelDocumentSharing();\n                }\n            }, 5000);\n        }\n\n        function handleSuccessfulShare(documentData) {\n            \/\/ Hide QR code\n            $('#diia-share-qrcode').hide();\n\n            \/\/ Show success message\n            $('#diia-share-result')\n                .html('<div class=\"diia-success\">Document shared successfully!<\/div>')\n                .show();\n\n            \/\/ Display the shared document\n            displaySharedDocument(documentData);\n        }\n\n        function displaySharedDocument(doc) {\n            const $container = $('#diia-shared-document');\n\n            $container.find('.diia-document-title').text(doc.name || '');\n            $container.find('.diia-document-type').text(doc.type || '');\n\n            if (doc.image) {\n                $container.find('.diia-document-image img').attr('src', doc.image).attr('alt', doc.name || '');\n            } else {\n                $container.find('.diia-document-image').hide();\n            }\n\n            const $details = $container.find('.diia-document-details');\n            $details.empty();\n\n            \/\/ Display document details\n            if (doc.details) {\n                Object.keys(doc.details).forEach(function(key) {\n                    if (doc.details[key]) {\n                        $details.append('<div class=\"diia-document-detail\"><span class=\"diia-detail-label\">' + key + ':<\/span> <span class=\"diia-detail-value\">' + doc.details[key] + '<\/span><\/div>');\n                    }\n                });\n            }\n\n            $container.show();\n        }\n\n        function cancelDocumentSharing() {\n            $('#diia-share-qrcode').hide();\n            $('.diia-share-button-wrapper').show();\n        }\n    });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/pages\/195\/"}],"collection":[{"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/pages\/"}],"about":[{"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/types\/page\/"}],"author":[{"embeddable":true,"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/users\/1\/"}],"replies":[{"embeddable":true,"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/comments\/?post=195"}],"version-history":[{"count":2,"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/pages\/195\/revisions\/"}],"predecessor-version":[{"id":16645,"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/pages\/195\/revisions\/16645\/"}],"wp:attachment":[{"href":"https:\/\/integration.ambu.org.ua\/en\/wp-json\/wp\/v2\/media\/?parent=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}