<?php
require '../../config.php';
session_start();
load('middleware', 'user');
$invoice = false;

$order_id = '';

if (isset($_GET['no'])) {
    $order_id = $_GET['no'];
    
    $query_pembelian_guest = mysqli_query($db, "SELECT * FROM deposit WHERE order_id = '$order_id'");
    
    if (mysqli_num_rows($query_pembelian_guest) === 1) {
        
        $fetch_pembelian = mysqli_fetch_assoc($query_pembelian_guest);
        
        $invoice = true;
    } else {
        header('location: '.base_url('id'));
    }
    
} else {
    header('location: '.base_url('id'));
}
require '../../layouts/header.php';
?>
<style>
        @import  url("https://fonts.googleapis.com/css?family=Open+Sans:400,700");
        @import  url("https://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css");

        .cus-accordion {
            transform: translateZ(0);
            box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
            /* background: #fff; */
        }

        .cus-accordion>.accordion-toggle {
            position: absolute;
            opacity: 0;
            display: none;
        }

        .cus-accordion>label {
            position: relative;
            display: block;
            height: 50px;
            line-height: 50px;
            padding: 0 20px;
            font-size: 14px;
            font-weight: 700;
            border-top: 1px solid #ddd;
            /* background: #fff; */
            cursor: pointer;
        }

        .cus-accordion>label:after {
            content: '\f078';
            position: absolute;
            top: 0px;
            right: 20px;
            font-family: fontawesome;
            transform: rotate(90deg);
            transition: .3s transform;
        }

        .cus-accordion>section {
            height: 0;
            transition: .3s all;
            overflow: hidden;
        }

        .cus-accordion>.accordion-toggle:checked~label:after {
            transform: rotate(0deg);
        }

        .cus-accordion>.accordion-toggle:checked~section {
            height: 200px;
        }

        .cus-accordion>section p {
            margin: 15px 0;
            padding: 0 20px;
            font-size: 12px;
            line-height: 1.5;
        }
    </style>
        <div class="row justify-content-center mb-5">
            <div class="col-lg-9">
                <button class="btn btn-warning btn-sm mb-3" onclick="print_invoice()"><i class="fas fa-print"></i>Print</button>
                <div class="col-lg-12">
                    <div class="alert p-2 alert-danger"><b>Note:</b> Jika sudah melakukan transfer namun status <b>CANCELED</b> harap langsung menghubungi kontak admin.</div>
                </div>
                <?php if ($invoice === true): ?>
                <div class="card bg-dark" id="invoice">
                    <div class="card-header">
                        <span class="card-title" style="font-size:14px;">Invoice Number <strong>#<?= $fetch_pembelian['order_id'];?></strong></span>
                        <?php if ($fetch_pembelian['status'] == 'Pending') {
                                $badge_bg = 'warning';
                                $stts = "PENDING";
                        }else if ($fetch_pembelian['status'] == 'Success'){
                                $badge_bg = 'success';
                                $stts = "SUCCESS";
                                $icon = "far fa-check-circle";
                        }else if ($fetch_pembelian['status'] == 'Canceled'){
                                $badge_bg = 'danger';
                                $stts = "CANCELED";
                                $icon = "fa-times-circle";
                        }else if($fetch_pembelian['status'] == 'Waiting'){
                                $badge_bg = 'secondary';
                                $icon = "fas fa-circle-notch fa-spin";
                                $stts = "WAITING";
                        }else if($fetch_pembelian['status'] == 'Processing'){
                                $badge_bg = 'primary';
                                $icon = "fas fa-circle-notch fa-spin";
                                $stts = "PROCESSING";
                        }
                        ?>
                        <span class="float-right badge p-2 bg-<?= $badge_bg;?> badge-pill"><i class="<?= $icon;?>"></i> <?=$stts;?></span>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <span class="h6"><b>Tanggal order</b>: <?= $fetch_pembelian['date_create'];?></span>
                            </div>
                        </div>
                        <div class="row mt-2">
                            <?php if ($fetch_pembelian['status'] == 'Pending'): ?>
                            <div class="col-lg-12">
                                <div class="alert p-2 alert-danger"><b>Informasi:</b> Segera lunasi tagihan kamu agar pesananmu segera kami proses. Terimakasih</div>
                            </div>
                            <br>
                            <?php endif;?>
                        </div>
                        <div class="row mt-3">
                            <div class="col-12">
                                <div class="table-responsive">
                                    <table class="table table-clear table-dark" style="font-size:14px;">
                                        <thead>
                                            <tr>
                                                <td style="text-align: center; vertical-align: middle;">Nama Layanan</td>
                                                <td style="text-align: center; vertical-align: middle;">Get Saldo</td>
                                                <td style="text-align: center; vertical-align: middle;">Metode Pembayaran</td>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td style="text-align: center; vertical-align: middle;"><?= $fetch_pembelian['layanan'];?></td>
                                                <td style="text-align: center; vertical-align: middle;">Rp. <?= number_format($fetch_pembelian['get_saldo'],0,',','.');?></td>
                                                <td style="text-align: center; vertical-align: middle;"><?= $fetch_pembelian['metode'];?></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-2">
                            <div class="col-lg-12">
                                <table class="table table-clear table-dark" style="font-size:14px;">
                                    <tbody>
                                        <tr>
                                            <td class="left">Harga</td>
                                            <td class="right text-right">Rp. <?= number_format($fetch_pembelian['amount'],0,',','.');?></td>
                                        </tr>
                                        <tr>
                                            <td class="left">Biaya Admin</td>
                                            <td class="right text-right">Rp. <?= number_format($fetch_pembelian['fee'],0,',','.');?></td>
                                        </tr>
                                        <tr>
                                            <td class="left"><strong>Total Yang Harus Di Bayar</strong></td>
                                            <td class="right text-right"><strong style="color:lime;">Rp. <span type="button" title="Click to copy" id="totPriceBayar" onclick="copyToClipboard2(this)"><?= number_format($fetch_pembelian['amount']+$fetch_pembelian['fee'],0,',','.');?></span> <i class="fas fa-copy" type="button" onclick="copyToClipboard2($('#totPriceBayar')[0])"></i></strong></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-lg-12">
                                <div class="card bg-dark shadow">
                                    <div class="card-body">
                                        <?php if ($fetch_pembelian['metode'] == "E-QRIS" || $fetch_pembelian['metode'] == "QRISC" || $fetch_pembelian['metode'] == "QRIS"){ ?>
                                            <img src="<?= base_url('assets/qris/'.$fetch_pembelian['tujuan']);?>" alt="<?= $fetch_pembelian['metode'];?>" height="200px">
                                        <?php }else if ($fetch_pembelian['metode'] == "OVO" || $fetch_pembelian['metode'] == "DANA" || $fetch_pembelian['metode'] == "LINKAJA" || $fetch_pembelian['metode'] == "SHOPEEPAY"){?>
                                        <?php $fetchh = $db->query("SELECT * FROM metode_guest WHERE metode='".$fetch_pembelian['metode']."'")->fetch_assoc();?>
                                        <div class="col-12 mb-4">
                                            <img src="<?= base_url('assets/metode/'.$fetchh['img']); ?>" alt="<?= $fetch_pembelian['metode'];?>" height="60px">
                                        </div>
                                        <?php }else{?>
                                        <?php $fetchh = $db->query("SELECT * FROM metode_guest WHERE metode='".$fetch_pembelian['metode']."'")->fetch_assoc();?>
                                        <div class="col-12 mb-4">
                                            <img src="<?= base_url('assets/metode/'.$fetchh['img']); ?>" alt="<?= $fetch_pembelian['metode'];?>" height="60px">
                                        </div>
                                        <div class="col-12 mb-2">
                                            <br>Silahkan Transfer ke rekening berikut : <b><?= $fetch_pembelian['tujuan']; ?></b><br>Segera lakukan pembayaran sesuai "Total Yang Harus Dibayar" tekan tombol Copy Nominal yg berwarna hijau<br>
                                        </div>
                                        <?php }; ?>
                                        <?= $fetch_pembelian['intruksi'];?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif;?>
            </div>
        </div>
<?php require '../../layouts/footer-admin.php';?>
<script>
        $(document).ready(function() {
            $("#paycode").tooltip();
            $("#paycode").click(function() {
                copyToClipboard($(this).text().trim().replace(".", ""), $(this));
            })
            $("#paycode").css('cursor', 'pointer');
        })

        function print_invoice() {
            var printContents = document.getElementById('invoice').innerHTML;
            var originalContents = document.body.innerHTML;
            document.body.innerHTML = printContents;
            window.print();
            document.body.innerHTML = originalContents;
            window.onafterprint = function() {
                location.reload()
            }
        }

        function copyrekening(element) {
            var $temp = $("<input>");
            $("body").append($temp);
                $temp.val($(element).text().trim().replace(".", "").replace(".", "")).select();
            document.execCommand("copy");
            $temp.remove();
            toastr.info("Copied to clipboard");
        }
        function copyToClipboard2(element) {
            var $temp = $("<input>");
            $("body").append($temp);
                $temp.val($(element).text().trim().replace(".", "").replace(".", "")).select();
            document.execCommand("copy");
            $temp.remove();
            toastr.info("Copied to clipboard");
        }
        function copyVirtualAccount(element) {
            var $temp = $("<input>");
            $("body").append($temp);
            $temp.val($(element).text().trim().replace(".", "")).select();
            document.execCommand("copy");
            $temp.remove();
            toastr.info("Copied to clipboard");
        }


        function copyToClipboard(text, el) {
            var copyTest = document.queryCommandSupported('copy');
            var elOriginalText = el.attr('data-bs-original-title');

            if (copyTest === true) {
                var copyTextArea = document.createElement("textarea");
                copyTextArea.value = text;
                document.body.appendChild(copyTextArea);
                copyTextArea.select();
                try {
                    var successful = document.execCommand('copy');
                    var msg = successful ? 'Copied!' : 'Whoops, not copied!';
                    el.attr('data-bs-original-title', msg).tooltip('show');
                } catch (err) {
                    console.log('Oops, unable to copy');
                }
                document.body.removeChild(copyTextArea);
                el.attr('data-bs-original-title', elOriginalText);
                console.log(elOriginalText);
            } else {
                window.prompt("Copy to clipboard: Ctrl+C or Command+C, Enter", text);
            }
        }
</script>