$(document).ready(function() {
    var recordId = requestParam.recordId;
    var hrefStr = requestParam.hrefStr;

    var pdfContainer = document.getElementById("pdf-container");
    pdfContainer.style.width = '100%';
    pdfContainer.style.height = '100vh';
    pdfContainer.style.overflow = 'hidden';
    pdfContainer.style.position = 'relative';

    // 添加缩放按钮和样式
    var zoomControls = document.createElement('div');
    zoomControls.style.position = 'absolute';
    zoomControls.style.top = '10px';
    zoomControls.style.right = '10px';
    zoomControls.style.zIndex = '1000';
    
    var zoomInBtn = document.createElement('button');
    zoomInBtn.innerText = '+';
    zoomInBtn.style.fontSize = '24px';
    zoomInBtn.style.margin = '0 5px';
    
    var zoomOutBtn = document.createElement('button');
    zoomOutBtn.innerText = '-';
    zoomOutBtn.style.fontSize = '24px';
    zoomOutBtn.style.margin = '0 5px';
    
    zoomControls.appendChild(zoomInBtn);
    zoomControls.appendChild(zoomOutBtn);
    pdfContainer.appendChild(zoomControls);

    var scale = 1.5;
    var startX, startY, currentX, currentY, initialDX, initialDY;
    var isDragging = false;
    var numPages;
    var pdf;

    mbos.eas.invokeScript({
        name: "commonOSFservice",
        param: ['getJobRegistrationPackage', JSON.stringify({ recordId: recordId })],
        success: function(res) {
            var base64Data = res.msg;
            var pdfData = atob(base64Data);
            var loadingTask = pdfjsLib.getDocument({data: pdfData});

            loadingTask.promise.then(function(pdfDoc) {
                pdf = pdfDoc;
                numPages = pdf.numPages;
                renderAllPages(scale, true);

                zoomInBtn.addEventListener('click', function() {
                    scale += 0.1;
                    renderAllPages(scale, false);
                });

                zoomOutBtn.addEventListener('click', function() {
                    scale = Math.max(0.5, scale - 0.1);
                    renderAllPages(scale, false);
                });
            }, function(reason) {
                console.error(reason);
            });
        },
        error: function(e) {
            console.log(e);
        }
    });

    function renderAllPages(newScale, isFirst) {
        pdfContainer.innerHTML = '';
        pdfContainer.appendChild(zoomControls);

        for (var i = 1; i <= numPages; i++) {
            pdf.getPage(i).then(function(page) {
                var viewport = page.getViewport({ scale: newScale });
                var canvas = document.createElement('canvas');
                var context = canvas.getContext('2d');
                canvas.id = 'pdf-canvas-' + page.pageNumber;
                canvas.width = viewport.width;
                canvas.height = viewport.height;
                canvas.style.position = 'absolute';
                canvas.style.left = '0';
                canvas.style.top = '0';
                if (isFirst) {
                    canvas.style.width = '100%';
                    canvas.style.height = '100%';
                }

                pdfContainer.appendChild(canvas);

                canvas.addEventListener('touchstart', function(e) {
                    isDragging = true;
                    startX = e.touches[0].clientX;
                    startY = e.touches[0].clientY;
                    initialDX = canvas.offsetLeft;
                    initialDY = canvas.offsetTop;
                });

                canvas.addEventListener('touchmove', function(e) {
                    if (isDragging) {
                        currentX = e.touches[0].clientX;
                        currentY = e.touches[0].clientY;
                        var dx = currentX - startX;
                        var dy = currentY - startY;
                        canvas.style.left = (initialDX + dx) + 'px';
                        canvas.style.top = (initialDY + dy) + 'px';
                    }
                });

                canvas.addEventListener('touchend', function() {
                    isDragging = false;
                });

                var renderContext = {
                    canvasContext: context,
                    viewport: viewport
                };
                page.render(renderContext);
            });
        }
    }

    if (!hrefStr) {
        $('#myButton').hide();
    }

    $('#myButton').click(function() {
        window.location.href = hrefStr + "/judge_page?recordId=" + encodeURIComponent(recordId);
    });
});