<script src="src/js/pdfobject.min.js"></script>
<?php
switch ($_REQUEST['type']) {
    default:
        break;
    case "outstanding":
        $label = 'Outstanding';
        $script = 'finance_unpaid';
        break;

    case "paid":
        $label = 'Paid';
        $script = 'finance_paid';
        break;

    case "archive":
        $label = 'Archived';
        $script = 'finance_archive';
        break;
}
//////////////////////////
/// PAYMENT STATUS     ///
/// 1 - Unpaid         ///
/// 2 - Reminder       ///
/// 3 - Paid           ///
/// 4 - Refund         ///
//////////////////////////

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE `invNo`='" . $_REQUEST['invNo'] . "' ") or die(mysqli_error($GLOBALS['con']));
$stmt->execute();
$i = $stmt->get_result();
$stmt->close();
$id = mysqli_fetch_array($i);
echo $id['target'];
if ($id['target'] == "client" || $id['target'] == 'blank') {
    $id['target'] = "invoice";
}
switch ($id['status']) {
    default:
        break;
    case "1":
        $status = '<small><i>Status</i></small> <span class="badge badge-danger">Unpaid</span>';
        break;

    case "2":
        $status = '<small><i>Status</i></small> <span class="badge badge-warning">Reminder Sent</span>';
        break;

    case "3":
        $status = '<small><i>Status</i></small> <span class="badge badge-success">Paid</span>';
        break;

    case "4":
        $status = '<small><i>Status</i></small> <span class="badge badge-dark">Refunded</span>';
        break;
}
?>
<div id="loadingObject" style="position: fixed; top:50%; width:100%;">
    <h1 class="text-center"><i class="fad fa-file-invoice fa-spin fa-3x fa-fw"
            style="--fa-primary-color: #0055A6; --fa-secondary-color: gray;"></i><br>Loading Invoice <br><small>Please
            wait</small></h1>
</div>

<script>
$('#navBox').html(
    '<li class="breadrumb-item"><a href="index.php?t=includes/finance&p=finance_invoices.php">Finance - Invoices</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadrumb-item"><a href="index.php?t=includes/finance&p=<?php echo $script ?>.php"><?php echo $label ?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadrumb-item active">Preview</li>'
    )
</script>
<style>
#wrapper {
    height: 70vh
}

#previewContainer {
    position: relative;
    display: block;
    height: 100% !important;
    width: 100%;
}

.pdfobject-container {
    height: 100%;
}
</style>
<div class="row mt-n5">
    <div class="col-12 col-md-6">
        <h3 class="lead"><span class="text-muted">Previewing Invoice</span>
            <strong>#<?php echo $_REQUEST['invNo'] ?></strong>: <?php echo $status ?></h3>
    </div>
    <div class="col-12 col-md-6">
        <div class="float-right">
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_broker.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('broker')"><i
                    class="fad fa-file-pdf"></i> Broker Invoice</a>
            <?php } ?>
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_agent.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('agent')"><i
                    class="fad fa-file-pdf"></i> Agent Invoice</a>
            <?php } ?>
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_invoice.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('invoice')"><i
                    class="fad fa-file-pdf"></i> Client Invoice</a>
            <?php } ?>
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_remind.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('remind')"><i
                    class="fad fa-file-pdf"></i> Reminder</a>
            <?php } ?>
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_proforma.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('proforma')"><i
                    class="fad fa-file-pdf"></i> Proforma</a>
            <?php } ?>
            <?php if (file_exists('invoices/' . $_REQUEST['invNo'] . '_remit.pdf')) { ?>
            <a href="javascript:void(0)" class="btn btn-secondary noprint" onClick="invLoader('remit')"><i
                    class="fad fa-file-pdf"></i> Remittance</a>
            <?php } ?>
        </div>
    </div>

</div>

<div id="wrapper" class="border">
    <div id="previewContainer"></div>
</div>


<script>
$(".formButton,.transferButton,.archiveButton, .emailButton").click(function() {
    $('#popupBox .modal-body').html('');
    $('#popupBox .modal-footer').html('');
    var target = $(this).data('href');
    var string = $(this).data('values');
    var dest = encodeURI(target + "?mode=popup&data=" + string)
    $('#popupBox').modal('show');
    $('#popupBox .modal-body').load(dest);
})

PDFObject.embed("invoices/<?php echo $_REQUEST['invNo'] ?>_<?php echo $id['target'] ?>.pdf?<?= rand() ?>",
    "#previewContainer");
$('#loadingObject').css('display', 'block');
$('#previewContainer').css('display', 'block')
$('#loadingObject').css('display', 'none');

function invLoader(t) {
    $('#loadingObject').css('display', 'block');
    PDFObject.embed("invoices/<?php echo $_REQUEST['invNo'] ?>_" + t + ".pdf?<?= rand() ?>", "#previewContainer");
    $('#loadingObject').css('display', 'none');

}
</script>