<?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>
/* Set your fixed navbar height */
:root {
    --nav-h: 50px;
}

/* tweak for your nav */
@media (min-width:992px) {
    :root {
        --nav-h: 60px;
    }
}

/* Lock this region to the viewport under the navbar */
#contentRoot {
    position: fixed;
    top: var(--nav-h);
    left: 0;
    right: 0;
    bottom: 0;
    /* => full viewport height minus navbar */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* keep internal scrolling only */
}

/* Preview fills remaining space and scrolls */
#previewContainer {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

/* Injected viewer fills the container */
#previewContainer iframe,
#previewContainer embed,
#previewContainer object {
    display: block;
    width: 100% !important;
    height: 95% !important;
    border: 0;
}
</style>
<div id="contentRoot">
    <div class="row">
        <div class="col-12 col-md-6 mt-0 mb-3">
            <span onclick="history.back()" style="cursor:pointer" class="ms-3 mt-n2 mb-2"><i
                    class=" fa-duotone fa-square-caret-left"></i> Back</span>
        </div>
    </div>
    <div class="row">

        <div class="col-12 col-md-6">

            <h3 class="lead ms-3 "><span class="text-muted">Previewing report</span>
                <strong>#<?= $_REQUEST['repnumber'] ?></strong>
            </h3>
        </div>
        <div class="col-12 col-md-6 text-end mt-0 mt-md-n3 pe-5">
            <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 id="previewContainer" class="ms-3" style="height:94%; width:98%"></div>
</div>
<div class="d-block" style="height:50px"></div>
<div class="col-12">
    <div id="loadingObject" style="display: none">
        <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) {
    PDFObject.embed(
        "output/reports/<?= $d[0] ?>/<?= $_REQUEST['repnumber'] ?>_<?= $cd['itemnumber'] ?>.pdf?<?= rand() ?>",
        "#previewContainer");

} 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%; ' 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']) ?>',
    mode: 'preview',
    tenant: TENANT_URL
}, function(data) {
    //debug(data)
    if (PDFObject.supportsPDFs) {
        PDFObject.embed("output/reports/<?= $d[0] ?>/preview.pdf", "#previewContainer");
    } else {
        $('#previewContainer').html(
            "<iframe src='https://docs.google.com/viewer?url=https://<?= getCompanyDetails('web') ?>/output/reports/<?= $d[0] ?>/preview.pdf&embedded=true' style='width:100%; ' frameborder='0'></iframe>"
        )
    }


})

<?php } ?>
</script>