<?php
include 'koneksi.php';
$nofile = $_GET['nofile'];
$sqlea = mysqli_query($konek, "SELECT r.idbismanps,r.keterangan_status,p.nama_pic,p.telp_pic,u.nama_unit,c.telp,c.email,c.bu,c.alamat,r.nofile_bismanps,r.checker,c.nama_customer,r.tgl_bismanps,r.status_checkbismanps,r.checker,r.tgl_checkbismanps as tgl_checker FROM bisman_ps r INNER JOIN customer c ON r.idcustomer = c.idcustomer INNER JOIN pic p ON p.idcustomer=c.idcustomer INNER JOIN detail_bismanps d ON d.idbismanps = r.idbismanps INNER JOIN unit u ON d.idunit = u.idunit WHERE nofile_bismanps = '$nofile'");
$rowsa = mysqli_fetch_assoc($sqlea);
$nama_customer = $rowsa['bu'].' '.$rowsa['nama_customer'];
?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo  $nofile." ".$nama_customer ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <style>
        body {
            font-size: 12px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-image: url("asset/gambar/kopbisman.jpg");
        }

        .kopsur {
            position: fixed;
            top: 0mm;
            width: 100%;
            /* border-bottom: 1px solid black; */
            /* for demo */
            /* background: yellow; */
            /* for demo */
        }

        .kopsur,
        .page-header-space {
            height: 150px;
            margin-bottom: 0px;
        }

        .footer,
        .page-footer-space {
            height: 170px;
            /* background-color: aqua; */
        }

        .page-footer-space {
            position: relative;
            bottom: 0;
            width: 100%;
        }

        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            /* border-top: 1px solid black; */
            /* for demo */
            /* background: yellow; */
            /* for demo */
        }

        .desc-atas {
            margin-top: -50px;
        }

        .catatan {
            display: flex;
        }

        .print-area {
            width: 210mm;
            min-height: 297mm;
            /* background-image: url("asset/gambar/kopbisman.jpg"); */
            /* background-color: pink; */
            /* background-repeat: no-repeat; */
            background-repeat: repeat-y;
            background-position: top center;
            background-size: 210mm 297mm;
            padding: 30mm 20mm 20mm 20mm;
        }

        @media print {
            @page {
                margin: 0;
                size: A4;
            }

            .print-area {
                background: transparent;
                min-height: 297mm;
            }

            body {
                margin: 0;
                background-image: url("asset/gambar/kopbisman.jpg");
                background-repeat: repeat-y;
                background-position: top center;
                background-size: 210mm 297mm;

                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }

            thead {
                display: table-header-group;
            }

            tfoot {
                display: table-footer-group;
            }

            button {
                display: none;
            }

            .print-area:last-child {
                page-break-after: auto;
            }

            /* .isine table {
                page-break-inside: avoid;
            } */

            .isine th,
            .isine td {
                border: 1px solid #000;
            }
        }
    </style>
</head>

<body onload="window.print()">
    <div class="print-area">
        <?php
        include "koneksi.php";
        $nofile = $_GET['nofile'];
        $sqle = mysqli_query($konek, "SELECT r.idbismanps,r.keterangan_status,p.nama_pic,p.telp_pic,u.nama_unit,c.telp,c.email,c.bu,c.alamat,r.nofile_bismanps,r.checker,c.nama_customer,r.tgl_bismanps,r.status_checkbismanps,r.checker,r.tgl_checkbismanps as tgl_checker FROM bisman_ps r INNER JOIN customer c ON r.idcustomer = c.idcustomer INNER JOIN pic p ON p.idcustomer=c.idcustomer INNER JOIN detail_bismanps d ON d.idbismanps = r.idbismanps INNER JOIN unit u ON d.idunit = u.idunit WHERE nofile_bismanps = '$nofile'");
        $rows = mysqli_fetch_assoc($sqle);

        if (str_contains($nofile, "T")) {
            $cabang = "Tangerang";
        } elseif (str_contains($nofile, "S")) {
            $cabang = "Surabaya";
        }
        $tanggal = $rows['tgl_bismanps'];
        $bu = $rows['bu'];
        $customer = $rows['nama_customer'];
        $alamat = $rows['alamat'];
        $telp = $rows['telp'];
        $email = $rows['email'];

        $bulan = [
            1 => 'Januari',
            'Februari',
            'Maret',
            'April',
            'Mei',
            'Juni',
            'Juli',
            'Agustus',
            'September',
            'Oktober',
            'November',
            'Desember'
        ];

        $t = strtotime($tanggal);
        $tanggal = date('j', $t) . ' ' . $bulan[(int)date('n', $t)] . ' ' . date('Y', $t);
        ?>
        <div class="kopsur">
            <div class="row">
                <div class="col"></div>
            </div>
        </div>
        <div class="footer"></div>
        <table style="width: 100%;margin-top: -50px;">
            <thead>
                <tr>
                    <td>
                        <!--place holder for the fixed-position header-->
                        <div class="page-header-space"></div>
                    </td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <div class="desc-atas">
                            <table style="width: 100%;">
                                <tr>
                                    <td>No File</td>
                                    <td>:</td>
                                    <td><?= $nofile ?></td>
                                </tr>
                                <tr>
                                    <td>Tanggal</td>
                                    <td>:</td>
                                    <td><?= $tanggal ?></td>
                                </tr>
                                <tr>
                                    <td>Hal</td>
                                    <td>:</td>
                                    <td>Penawaran Harga</td>
                                </tr>
                                <tr>
                                    <td><br></td>
                                </tr>
                                <tr>
                                    <td>Kepada</td>
                                    <td>:</td>
                                    <td><?= $bu . $customer ?></td>
                                </tr>
                                <tr>
                                    <td>Alamat</td>
                                    <td>:</td>
                                    <td><?= $alamat ?></td>
                                </tr>
                                <tr>
                                    <td>Telp</td>
                                    <td>:</td>
                                    <td><?= $telp ?></td>
                                </tr>
                                <tr>
                                    <td>Email</td>
                                    <td>:</td>
                                    <td><?= $email ?></td>
                                </tr>
                            </table>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="ur1" style="margin-bottom: -15px;">
                            <br>
                            <p>Dengan Hormat,
                                <br>Bersama ini kami <span style="font-weight: 700;">PT. BISMAN BINTANG BUANA</span> memberikan penawaran harga sebagai berikut :
                            </p>
                            <!-- <p>Bersama ini kami PT. bisman INDONESIA bergerak di bidang Penjualan, Sewa, serta Perbaikan
                            Material
                            Handling
                            di Indonesia bermaksud menawarkan harga sebagai berikut:</p> -->
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="isine">
                            <table style="width: 100%;border: 1px solid #000;">
                                <thead>
                                    <th scope="col">No</th>
                                    <th scope="col">Uraian</th>
                                    <th scope="col" style="width: 5px;">QTY</th>
                                    <th scope="col" style="width: 250px;">Keterangan</th>
                                    <th scope="col">Harga Satuan</th>
                                    <th scope="col">Harga</th>
                                </thead>
                                <tbody>
                                    <?php
                                    $cridrps = mysqli_query($konek, "select idbismanps from bisman_ps where nofile_bismanps='$nofile'");
                                    $cridrps = mysqli_fetch_array($cridrps);
                                    $idbismanps = $cridrps["idbismanps"] ?? "";

                                    $no = 1;
                                    $sql = mysqli_query($konek, "SELECT d.iddetail_bismanps,u.nama_unit,d.no_unit,d.wo_unit FROM detail_bismanps d INNER JOIN unit u ON d.idunit=u.idunit WHERE idbismanps='$idbismanps'");
                                    $subTotalKeseluruhan = 0;
                                    $subTotal = 0;
                                    while ($row = mysqli_fetch_array($sql)) {
                                        $iddbismanps = $row['iddetail_bismanps'];
                                        $querygabung = mysqli_query(
                                            $konek,
                                            "SELECT s.jenis_service AS keterangan,j.harga_servicebisman AS harga,j.qty_service AS kuota,j.totalservice_bisman AS total,j.keterangan AS kete FROM uraianbismanps_service j INNER JOIN jenis_service s ON j.idjenis_service=s.idjenis_service WHERE j.iddetail_bismanps = '$iddbismanps'
                                        UNION ALL
                                        SELECT b.nama_barang AS keterangan,ub.harga_barangbisman AS harga,ub.qty AS kuota,ub.totalbarang_bisman AS total,ub.keterangan AS kete FROM uraianbismanps_barang ub INNER JOIN barang b ON ub.idbarang=b.idbarang WHERE ub.iddetail_bismanps = '$iddbismanps'"
                                        );
                                        $data = mysqli_num_rows($querygabung);
                                        $noe = 0;
                                        while ($d = mysqli_fetch_array($querygabung)) {
                                            $subTotalKeseluruhan += $d['total'];
                                    ?>
                                            <tr style="text-align: center;">
                                                <?php
                                                if ($noe == 0) {
                                                ?>
                                                    <td rowspan="<?php echo $data ?>"><?= $no ?></td>
                                                    <td rowspan="<?php echo $data ?>"><?php
                                                                                        echo $row['nama_unit'];
                                                                                        echo "<br>";
                                                                                        echo !empty($row['no_unit']) ? " ({$row['no_unit']})" : "";
                                                                                        echo "<br>";
                                                                                        echo !empty($row['wo_unit']) ? " ({$row['wo_unit']})" : "";
                                                                                        ?>
                                                    </td>
                                                <?php
                                                }
                                                ?>
                                                <td><?php echo $d['kuota'] ?></td>
                                                <td style="text-align: left;">
                                                    <div style="font-weight:600;">
                                                        <?= htmlspecialchars($d['keterangan']) ?>
                                                    </div>
                                                    <?php if (!empty($d['kete'])): ?>
                                                        <div style="text-align:left; margin-top:6px; line-height:1;">
                                                            <?= nl2br(htmlspecialchars($d['kete'])) ?>
                                                        </div>
                                                    <?php endif; ?>
                                                </td>
                                                <td style="text-align: center;">
                                                    <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;">
                                                        <span style="text-align: left;">Rp</span>
                                                        <span>
                                                            <?= number_format($d['harga'], 0, ',', '.') ?>
                                                        </span>
                                                    </div>
                                                </td>
                                                <td style="text-align: center;">
                                                    <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;">
                                                        <span style="text-align: left;">Rp</span>
                                                        <span>
                                                            <?= number_format($d['total'], 0, ',', '.') ?>
                                                        </span>
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php
                                            $noe++;
                                            }
                                            ?>
                                        <tr>
                                            <td colspan="5" style="text-align: center;font-weight: 700;">TOTAL <?= $no ?></td>
                                            <td>
                                                <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;font-weight: 700;">
                                                    <span style="text-align: left;">Rp</span>
                                                    <?= number_format($subTotalKeseluruhan, 0, ',', '.') ?>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php
                                        $subTotal += $subTotalKeseluruhan;
                                        $no++;
                                    }
                                    ?>
                                    <tr style="font-weight:700;">
                                        <td colspan="5" style="text-align:center;">
                                            SUB TOTAL
                                        </td>
                                        <td style="text-align:center;">
                                            <div style="display:flex; justify-content:space-between; margin:0 5px;">
                                                <span>Rp</span>
                                                <span><?= number_format($subTotal, 0, ',', '.') ?></span>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr style="font-weight:700;">
                                        <td colspan="5" style="text-align:center;">
                                            PPN
                                        </td>
                                        <td style="text-align:center;">
                                            <div style="display:flex; justify-content:space-between; margin:0 5px;">
                                                <span>Rp</span>
                                                <?php
                                                    $ppn = $subTotal * 0.11;
                                                ?>
                                                <span><?= number_format($ppn, 0, ',', '.') ?></span>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr style="font-weight:700;">
                                        <td colspan="5" style="text-align:center;">
                                            TOTAL AMOUNT
                                        </td>
                                        <td style="text-align:center;">
                                            <div style="display:flex; justify-content:space-between; margin:0 5px;">
                                                <span>Rp</span>
                                                <?php
                                                    $tmount = $subTotal + $ppn;
                                                ?>
                                                <span><?= number_format($tmount, 0, ',', '.') ?></span>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="catatan">
                            <p style="font-weight: 700;">Note : </p>
                            <?php
                            if ($rows['status_checkbismanps'] == "Belum") {
                            ?>
                                <p style="margin-left: 5px;">Unit <?= $rows['status_checkbismanps'] ?>
                                    <?php
                                    if ($rows['keterangan_status'] == "Belum di ambil") {
                                        echo "dilakukan";
                                    } else {
                                        echo "diambil &";
                                    }
                                    ?>
                                    pengecekan oleh teknisi PT bisman Indonesia</p>
                            <?php
                            } else {
                                $tglcek = $rows['tgl_checker'];
                                $t = strtotime($tglcek);
                                $tglcek = date('j', $t) . ' ' . $bulan[(int)date('n', $t)] . ' ' . date('Y', $t);
                            ?>
                                <p style="margin-left: 5px;">Unit <?= $rows['status_checkbismanps'] ?>
                                    <?php
                                    if ($rows['keterangan_status'] == "Belum di ambil") {
                                        echo "dilakukan";
                                    } else {
                                        echo "diambil &";
                                    }
                                    ?>
                                    pengecekan oleh teknisi PT Bisman Bintang Buana <?= $tglcek ?>
                                    (<?php echo substr($rows['checker'] ?? '-', 0, 2) ?>)</p>
                            <?php
                            }
                            ?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="keterangan">
                            <p style="font-weight: 700;margin-bottom: -2px;">Keterangan :</p>
                            <?php
                            if (!empty($_POST['catatan'])) {
                                echo "<ul>";
                                foreach ($_POST['catatan'] as $item) {
                                    echo "<li>" . htmlspecialchars($item) . "</li>";
                                }
                                echo "</ul>";
                            }
                            ?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Demikian surat penawaran ini kami ajukan, untuk informasi lebih lanjut ataupun pertanyaan dapat menghubungi kami kembali</td>
                </tr>
                <tr>
                    <td>Atas perhatian dan kerja sama yang diberikan, kami ucapkan terima kasih.</td>
                </tr>
                <tr style="page-break-inside: avoid;">
                    <td>
                        <div class="ttd">
                            <p>Hormat Kami,</p>
                            <p style="font-weight: 700;margin-top: -20px;">Marketing Division PT. RAXI INDONESIA</p>
                            <?php
                            if ($cabang == 'Tangerang') {
                            ?>
                                <img src="asset/gambar/ttd arleta maharani (bisman sv ckr).png" alt="" width="200"
                                    style="margin-bottom: -10px;margin-top: -10px;">
                                <p style="font-weight: 700;text-decoration: underline;margin-left: 70px;">Arleta Maharani</p>
                                <p style="margin-top: -20px;margin-left: 60px;">0812 5298 1592</p>
                            <?php
                            } else {
                            ?>
                                <img src="asset/gambar/ttd dita putri (bisman sv sby).png" alt="" width="200"
                                    style="margin-top: -15px;margin-bottom: -10px;">
                                <p style="font-weight: 700;text-decoration: underline;">Dita Putri</p>
                                <p style="margin-top: -20px;">0813 7022 4880</p>
                            <?php
                            }
                            ?>
                        </div>
                        <br>
                    </td>
                </tr>
                <tr style="text-align: center;page-break-inside: avoid;">
                    <td>
                        <div class="topg" style="width: 100%;">
                            <img src="asset/gambar/svb 1.png" style="width: 100%;">
                        </div>
                        <br>
                        <div class="gambare">
                            <img src="asset/gambar/svb 2.png" alt="" style="width: 45%;">
                            <img src="asset/gambar/svb 3.png" alt="" style="width: 45%;">
                        </div>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <br>
                        <!--place holder for the fixed-position footer-->
                        <div class="page-footer-space"></div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous">
    </script>
</body>

</html>