<?php
$data = urldecode($_REQUEST['data']);
$d = explode("|", $data);
$path = $_REQUEST['path'] ?? null;

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `reportitems` WHERE `repnumber` = ? AND `itemnumber`='" . $d[2] . "'") or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i', $_REQUEST['repnumber']);
$stmt->execute();
$crs = $stmt->get_result();
$stmt->close();
$cd = mysqli_fetch_array($crs);
$type = getItemReportCount($_REQUEST['repnumber']);
$d = explode('|', $_REQUEST['data']);

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` WHERE `clientnumber`=? ") or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i', $d[0]);
$stmt->execute();
$cr = $stmt->get_result();
$stmt->close();
$cdx = mysqli_fetch_array($cr);
$email_values = [
    0 => $cdx['clientnumber'], //client
    1 => $cdx['clientname'], //type
    2 => $cdx['email'], //recipient
    3 => 'emailing documents', //subject
    4 => '', //message
    5 => 'output', //attachment
    6 => 'reports/' . $d[0] . '/' . $_REQUEST['repnumber'] . '_' . $d[2] . '.pdf' //source
];
$email64_values = base64_encode(json_encode($email_values));
?>
<script src="<?= ROOT_URL ?>/src/js/pdfobject.min.js?<?= rand(); ?>"></script>
<?php if ($path == "outstanding") { ?>
<script>
$('#navBox').html(
    '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a> <i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $d[0] ?>&view=asset"><?= getClientName($d[0]) ?></a> <i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master/&p=reports_outstanding.php&clientnumber=<?= $d[0] ?>">Delivery Outstanding</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Viewing Report: <?= $_REQUEST['repnumber'] ?></li>'
)
</script>

<?php } else { ?>
<script>
$('#navBox').html(
    '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a> <i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $d[0] ?>&view=asset"><?= getClientName($d[0]) ?></a> <i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">Viewing Report: <?= $_REQUEST['repnumber'] ?></li>'
)
</script>

<?php } ?>
<style>
:root {
    --internal-preview-nav-height: 60px;
    --internal-preview-footer-height: 34px;
}

#contentRoot {
    background: #f4f6f9;
    bottom: var(--internal-preview-footer-height);
    display: flex;
    flex-direction: column;
    left: 0;
    overflow: hidden;
    position: fixed;
    right: 0;
    top: var(--internal-preview-nav-height);
}

.internal-preview-toolbar {
    background: #fff;
    border-bottom: 1px solid #d9e2ec;
    box-shadow: 0 8px 20px rgba(15, 23, 42, .06);
    flex: 0 0 auto;
    padding: 12px 16px;
}

.internal-preview-title {
    font-size: 16px;
    margin: 0;
}

.internal-preview-back {
    cursor: pointer;
    white-space: nowrap;
}

.internal-preview-actions {
    align-items: center;
    display: inline-flex;
    gap: 6px;
    justify-content: flex-end;
}

.internal-preview-stage {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    padding: 16px;
}

#previewContainer {
    background: #fff;
    height: 100%;
    margin: 0 auto;
    max-width: 1380px;
    min-height: 0;
    overflow: hidden;
    width: 100%;
}

#previewContainer iframe,
#previewContainer embed,
#previewContainer object,
#previewContainer .pdfobject-container {
    background: #fff;
    border: 0;
    display: block;
    height: 100% !important;
    width: 100% !important;
}

#loadingObject {
    align-items: center;
    background: rgba(244, 246, 249, .92);
    bottom: var(--internal-preview-footer-height);
    display: none;
    flex-direction: column;
    justify-content: center;
    left: 0;
    position: fixed;
    right: 0;
    top: var(--internal-preview-nav-height);
    z-index: 1040;
}

@media (max-width: 767.98px) {
    :root {
        --internal-preview-nav-height: 54px;
    }

    .internal-preview-toolbar {
        padding: 10px 12px;
    }

    .internal-preview-toolbar .row {
        flex-wrap: nowrap;
    }

    .internal-preview-back-col {
        flex: 0 0 auto;
        width: auto;
    }

    .internal-preview-title-col {
        flex: 1 1 auto;
        min-width: 0;
        width: auto;
    }

    .internal-preview-actions-col {
        flex: 0 0 auto;
        width: auto;
    }

    .internal-preview-title {
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .internal-preview-stage {
        padding: 8px;
    }
}
</style>
<div id="contentRoot">
    <div class="internal-preview-toolbar">
        <div class="row align-items-center g-2">
            <div class="col-12 col-md-4 internal-preview-back-col">
                <span onclick="history.back()" class="internal-preview-back"><i
                        class=" fa-duotone fa-square-caret-left"></i> Back</span>
            </div>
            <div class="col-12 col-md-4 text-md-center internal-preview-title-col">
                <h3 class="lead internal-preview-title"><span class="text-muted">Previewing report</span>
                <strong>#<?= $_REQUEST['repnumber'] ?></strong>
                </h3>
            </div>
            <div class="col-12 col-md-4 text-md-end internal-preview-actions-col">
                <div class="internal-preview-actions">
                <a href="output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $d[2] ?>.pdf?<?= rand() ?>"
                    target="_blank" class="btn btn-secondary noprint" download data-mdb-tooltip-init
                    data-mdb-placement="top" data-mdb-trigger="hover" title="Download Report"><i
                        class="fad fa-file-pdf"></i></a>


                <?php if ($_SESSION['delivery'] != 0) { ?>
                <?php if (file_exists('output/reports/' . $cd['clientnumber'] . '/' . $_REQUEST['repnumber'] . '_' . $cd['itemnumber'] . '.pdf')) { ?>
                <a href="javascript:void(0)" class="btn btn-secondary noprint emailButton"
                    data-values="<?= $email64_values ?>" data-href="<?= ROOT_URL ?>/includes/master/mailer.php"
                    data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Email Client"><i
                        class="fad fa-envelope"></i></a>
                <?php } else { ?>
                <a href="javascript:void(0)" class="btn btn-secondary noprint emailButton"
                    data-values="<?= $email64_values ?>" data-href="<?= ROOT_URL ?>/includes/master/mailer.php"
                    data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Email Client"><i
                        class="fad fa-envelope"></i></a>
                <?php } ?>
            <?php } ?>
                <!--<a id="lock" href="javascript:void(0)" class="btn btn-secondary noprint"><i class="fad fa-lock"></i></a>-->
                </div>
            </div>
        </div>
    </div>

    <div class="internal-preview-stage">
        <div id="previewContainer"></div>
    </div>
</div>
<div class="d-block" style="height:50px"></div>
<div class="col-12">
    <div id="loadingObject">
        <h1 class="text-center"><i class="fad fa-cog fa-spin fa-3x fa-fw ml-5"
                style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i></h1>
        <h1 class="text-center">Generating Reports <br><small>Please wait</small></h1>
    </div>
</div>
<div id="backdrop"></div>
<script>
<?php if (file_exists('output/reports/' . $cd['clientnumber'] . '/' . $_REQUEST['repnumber'] . '_' . $cd['itemnumber'] . '.pdf')) { ?>

if (PDFObject.supportsPDFs) {
    embedReportPdf("output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $cd['itemnumber'] ?>.pdf?<?= rand() ?>");

} else {

    $('#previewContainer').html(
        "<iframe src='https://docs.google.com/viewer?url=https://<?= getCompanyDetails('web') ?>/output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $cd['itemnumber'] ?>.pdf&embedded=true' style='width:100%; height:100%; background:#fff;' frameborder='0'></iframe>"
    )
}
$('#loadingObject').css('display', 'none')
$('#previewContainer').css('display', 'block')
<?php } else { ?>
$.post('<?= ROOT_URL ?>/functions/generators/genPDF2.php', {
    clientnumber: '<?= $d[0] ?>',
    repnumber: '<?= $_REQUEST['repnumber'] ?>',
    items: '<?= $cd['itemnumber'] ?>',
    form: '<?= getItemReportType($_REQUEST['repnumber']) ?>',
    tenant: TENANT_URL
}, function(data) {
    //debug(data)
    if (PDFObject.supportsPDFs) {
        embedReportPdf("output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $cd['itemnumber'] ?>.pdf?<?= rand() ?>");
    } else {
        $('#previewContainer').html(
            "<iframe src='https://docs.google.com/viewer?url=https://<?= getCompanyDetails('web') ?>/output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $cd['itemnumber'] ?>.pdf&embedded=true' style='width:100%; height:100%; background:#fff;' frameborder='0'></iframe>"
        )
    }

    $('#loadingObject').css('display', 'none')
    $('#previewContainer').css('display', 'block')

})

<?php } ?>

function embedReportPdf(url) {
    PDFObject.embed(pdfFitUrl(url), "#previewContainer", {
        pdfOpenParams: {
            view: "FitH",
            zoom: "page-width",
            pagemode: "none"
        }
    });
}

function pdfFitUrl(url) {
    const separator = url.indexOf('#') === -1 ? '#' : '&';
    return `${url}${separator}view=FitH&zoom=page-width&pagemode=none`;
}
</script>
