<?php
include_once "global.php";
$invId = $functions->decode($_GET["id"]);

$sql = "SELECT * FROM `invoices` WHERE id = ?";
$invoice = $dbF->getRow($sql, [$invId]);
$amnt_words = (int) $invoice["total_billed_amount"];
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);

$userId = $invoice["client_id"];
$userData = $productClass->getUserById($userId);
$userDetail = $productClass->getUserDetailsById($userId);
$address = $webClass->webUserInfoArray($userDetail, 'address');

$phones = $productClass->getUserPhonesById($userId);

$invoiceDetail = $productClass->getInvoiceDetailsById($invId);
$domain = $productClass->invoiceDetailInfoArray($invoiceDetail, 'vendor_domain');
?>

<!doctype html>
<html lang="en">

<head>
    <!-- Meta Tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description"
        content="Login to access your dashboard at <?php echo $functions->ibms_setting('Site Name'); ?>">
    <meta name="author" content="Interactive Media Pakistan">

    <!-- Page Title -->
    <title>Commercial Invoice - <?php echo $functions->ibms_setting('Site Name'); ?></title>

    <!-- Favicon -->
    <link rel="icon" href="webImages/favicon.ico" type="image/x-icon">

    <!-- Preconnect for Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Material+Icons+Outlined" rel="stylesheet">

    <!-- Loader -->
    <link href="css/pace.min.css" rel="stylesheet">
    <script src="js/pace.min.js" defer></script>

    <!-- Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-extended.css" rel="stylesheet">

    <!-- Plugins (if actually used on login page) -->
    <link href="plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="plugins/metismenu/metisMenu.min.css">
    <link rel="stylesheet" type="text/css" href="plugins/metismenu/mm-vertical.css">

    <!-- Custom Styles with Cache Busting -->
    <link href="sass/main.css?ver=<?php echo filemtime(__DIR__ . '/sass/main.css'); ?>" rel="stylesheet">
    <link href="sass/dark-theme.css?ver=<?php echo filemtime(__DIR__ . '/sass/dark-theme.css'); ?>" rel="stylesheet">
</head>

<body>
    <style>
        body {
            max-width: 812px !important;
            margin: 0 auto !important;
            width: 100% !important;
            background-color: #ffffff !important;
        }

        table {
            max-width: 700px !important;
            width: 100% !important;
            margin: 0 auto !important;
        }

        table td {
            background: none !important;
        }

        .card {
            padding-top: 100px !important;
            background-image: url('webImages/imedia-new2.jpg');
            background-repeat: no-repeat;
            background-size: cover;
            margin-bottom: 0 !important;
            box-shadow: none !important;
            /*padding-bottom: 240px;*/
            /* height: 1150px;*/
            /*width: 810px;*/
            height: 1140px;
            width: 100%;
        }

        .card .table-responsive {
            overflow-x: unset;
        }

        .card-header {
            border: none !important;
        }

        @page {
            margin: 0 !important;
            padding: 0 !important;
        }
    </style>
    <div class="card rounded-0">
        <div class="card-header text-center mt-5 mb-2">
            <h3 class="mb-0 text-dark text-uppercase">Commercial Invoice</h3>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered mb-3">
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Customer Name</th>
                    <td class="text-dark">
                        <?php echo !empty($invoice["client_company"]) ? $invoice["client_company"] : $domain; ?>
                    </td>
                    <th style="background-color: #8cc63f; color: #ffffff;">Invoice Number</th>
                    <td class="text-dark"><?php echo $invoice["invoice_number"]; ?></td>
                </tr>
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Contact Person</th>
                    <td class="text-dark"><?php echo $userData["acc_name"]; ?></td>
                    <th style="background-color: #8cc63f; color: #ffffff;">Date</th>
                    <td class="text-dark"><?php echo date('M-d-Y', strtotime($invoice["invoice_date"])); ?></td>
                </tr>
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Phone Number</th>
                    <td class="text-dark"><?php echo $userData["acc_phone"]; ?></td>
                    <th style="background-color: #8cc63f; color: #ffffff;">Prepared By</th>
                    <td class="text-dark">Billing Department</td>
                </tr>
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Cell Number</th>
                    <td class="text-dark"><?php echo $phones[0] ?? ''; ?></td>
                    <th style="background-color: #8cc63f; color: #ffffff;">Email</th>
                    <td class="text-dark">billing@imedia.com.pk</td>
                </tr>
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Email</th>
                    <td class="text-dark"><?php echo $userData["acc_email"]; ?></td>
                    <th style="background-color: #8cc63f; color: #ffffff;">Phone Number</th>
                    <td class="text-dark">+92-21-34325656-57-59</td>
                </tr>
                <tr>
                    <th class="text-dark" style="background-color: #a2a2a245;">Address</th>
                    <td class="text-dark" colspan="3"><?php echo $address ?? ''; ?></td>
                </tr>
            </table>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered border-dark mb-3">
                <tr class="text-center">
                    <th class="text-dark">Description</th>
                    <th class="text-dark">Amount (PKR)</th>
                </tr>
                <tr>
                    <td class="text-dark"><?php echo $invoice["invoice_for"]; ?> Invoice - <?php echo $domain; ?></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="text-dark">Duration:
                        <?php echo date('M-Y', strtotime($invoice["due_date"])) . ' - ' . date('M-Y', strtotime($invoice["expiry_date"])); ?>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class="text-dark"><?php echo $invoice["description"]; ?><br><br></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="text-end text-dark">Total Payment</td>
                    <td class="text-end text-dark"><?php echo $invoice["billed_amount"]; ?></td>
                </tr>
                <?php
                if ($invoice["total_amount"] > 0) {
                ?>
                    <tr>
                        <td class="text-end text-dark">Total Amount</td>
                        <td class="text-end text-dark"><?php echo $invoice["total_amount"]; ?></td>
                    </tr>
                <?php
                }
                ?>
                <?php
                if ($invoice["invoice_type"] == "sales_tax") {
                ?>
                    <tr>
                        <td class="text-end text-dark">Tax 15%</td>
                        <td class="text-end text-dark"><?php echo $invoice["tax_amount"]; ?></td>
                    </tr>
                <?php
                }
                ?>
                <tr>
                    <td class="text-end text-dark">Total Invoice Amount</td>
                    <td class="text-end text-dark"><strong><?php echo $invoice["total_billed_amount"]; ?></strong></td>
                </tr>
                <tr>
                    <td class="text-dark" colspan="3"><strong>Amount in Words:
                        </strong><?php echo ucwords($f->format($amnt_words)) . ' Only'; ?></td>
                </tr>
            </table>
        </div>
        <div class="row px-5">
            <div class="col-9">
                <h6 class="text-dark text-decoration-underline">Terms and Conditions</h6>
                <ol>
                    <li class="text-dark">No! Any deductions are acceptable on the Total Invoice Amount.</li>
                    <li class="text-dark">All Prices will be in PKR</li>
                    <li class="text-dark">Bank Details:<br>Name: United Bank Limited (UBL) <br>
                        Account Title: Interactive Media <br>
                        Account Number: 0149-211738181 <br>
                        IBAN: PK77UNIL0109000211738181<br>
                        SWIFT Code: UNILPKKA</li>
                </ol>
                <strong class="text-dark">For
                    <?php echo !empty($invoice["client_company"]) ? $invoice["client_company"] : $domain; ?></strong>
            </div>
            <div class="col-3 d-flex justify-content-end" style="padding-top: 2rem;  margin-left: -1rem;">
                <div class="text-center">
                    <img src="webImages/stamp.png" alt="stamp" style="height: 90px;">
                    <strong class="d-block pt-2 text-dark">For Interactive Media</strong>
                </div>
            </div>
        </div>
        <span class="text-dark" style="font-size: 12px; position: absolute; bottom: 50px; left: 45px;">Office #6, 2nd
            Floor, Aman
            Chamber KCHUS,
            Shaheed-e-Millat Road, Karachi 75350, Pk</span>
    </div>
</body>

</html>