<?php
// ob_clean();
// ob_start();

// header("Content-Type: application/msword");
// header("Content-Disposition: attachment; filename=Penawaran_RAXI.pdf");
// header("Pragma: no-cache");
// header("Expires: 0");
$nofile = $_GET['nofile'];
?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $nofile; ?></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;
        }

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

        .kopsur,
        .page-header-space {
            height: 120px;
            margin-bottom: 20px;
        }

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

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

        .kop-surat {
            font-family: Arial, Helvetica, sans-serif;
            color: #000;
            line-height: 1.3;
            margin-left: -10px;
            /* border-bottom: 1px solid black; */
        }

        .kop-surat h1 {
            font-size: 20px;
            font-weight: bold;
            margin: 0 0 4px 0;
        }

        .kop-surat p {
            margin: 0;
            font-size: 12px;
        }

        .kop-surat .tagline {
            font-weight: bold;
        }

        .kop-surat .alamat,
        .kop-surat .kontak,
        .kop-surat .email {
            font-size: 11px;
        }

        .catatan {
            display: flex;
        }

        @media print {
            @page {
                margin: 5mm;
            }

            thead {
                display: table-header-group;
            }

            tfoot {
                display: table-footer-group;
            }

            button {
                display: none;
            }

            body {
                margin: 0;
            }

            .isiane table {
                page-break-inside: avoid;
            }

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

<body onload="window.print()">

    <div class="kopsur" style="display: flex;gap: 20px;">
        <div class="gkiri">
            <img src="asset/gambar/logo-raxprint.png" alt="" width="200px">
        </div>
        <div class="gkanan">
            <div class="kop-surat">
                <h1>PT. RAXI INDONESIA</h1>

                <p class="tagline">
                    Material Handling Equipment | Sale | Rent | Service | www.materialhandling.co.id
                </p>

                <p class="alamat">
                    Surabaya : Gedung Graha Pena Jawa Pos Lantai 5, Jl. Ahmad Yani No. 88 RT 007 RW 003,
                    Ketintang, Gayungan, Kota Surabaya, Jawa Timur | 031-30008273
                </p>
                <p class="kontak">
                    Tangerang : Jl. Raya Cibar Baro No.9C, BSD Serpong, Tangerang Selatan | 021-75671297
                </p>

                <p class="email">
                    sales@materialhandling.co.id | marketing@raxi.co.id | sby@raxi.co.id | jkt@raxi.co.id
                </p>
            </div>
        </div>
    </div>
    <div class="footer">
        <img src="asset/gambar/foot-rax.png" alt="" style="width: 100%;">
    </div>
    <table style="width: 100%;">
        <thead>
            <tr>
                <td>
                    <!--place holder for the fixed-position header-->
                    <div class="page-header-space"></div>
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <div class="tglkanan" style="margin-top: -25px;">
                        <?php
                        include "koneksi.php";
                        $nofile = $_GET['nofile'];
                        $sqle = mysqli_query($konek, "SELECT r.idrevisi_raxips,r.keterangan_status,p.nama_pic,p.telp_pic,u.nama_unit,c.telp,c.email,c.bu,c.alamat,r.nofile_revisi_raxips,r.checker,c.nama_customer,r.tgl_raxips,r.status_checkraxips,r.checker,r.tgl_checkraxips as tgl_checker 
                                                                    FROM revisi_raxi_ps r INNER JOIN customer c ON r.idcustomer = c.idcustomer 
                                                                    INNER JOIN pic p ON p.idcustomer=c.idcustomer 
                                                                    INNER JOIN revisi_detail_raxips d ON d.idrevisi_raxips = r.idrevisi_raxips 
                                                                    INNER JOIN unit u ON d.idunit = u.idunit 
                                                                    WHERE nofile_revisi_raxips = '$nofile'");
                        $rows = mysqli_fetch_assoc($sqle);

                        if (str_contains($nofile, "SV-TG")) {
                            $cabang = "Tangerang";
                        } elseif (str_contains($nofile, "SV-SB")) {
                            $cabang = "Surabaya";
                        }
                        $tanggal = $rows['tgl_raxips'];

                        $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);
                        ?>
                        <p style="text-align: right;"><?= $cabang ?>, <?= $tanggal ?></p>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="desc-atas">
                        <table style="width: 100%;">
                            <tr>
                                <td>Hal</td>
                                <td>:</td>
                                <td>Penawaran Harga</td>
                            </tr>
                            <tr>
                                <th style="width: 7%;">No file</th>
                                <td style="width: 2%;">:</td>
                                <td><?= $rows['nofile_revisi_raxips'] ?></td>
                                <th style="width: 9%;">Up</th>
                                <td>:</td>
                                <td><?= $rows['nama_pic'] ?></td>
                            </tr>
                            <tr>
                                <th>Kepada</th>
                                <td>:</td>
                                <td><?= $rows['bu'] . $rows['nama_customer'] ?></td>
                                <th>Telp / Hp</th>
                                <td>:</td>
                                <td>
                                    <?php
                                    $telp = $rows['telp_pic'] ?? '-';
                                    $telpcust = $rows['telp'] ?? '-';
                                    echo $telp . ' / ' . $telpcust;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>Alamat</th>
                                <td>:</td>
                                <td style="width: 50%;"><?= $rows['alamat'] ?></td>
                                <th>Email</th>
                                <td>:</td>
                                <td><?= $rows['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. RAXI INDONESIA</span> bergerak di bidang <span style="font-style: italic;">Penjualan, Sewa, serta Perbaikan Material Handling di Indonesia</span> bermaksud menawarkan harga sebagai berikut:
                        </p>
                        <!-- <p>Bersama ini kami PT. RAXI 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="isiane">
                        <?php
                        $cridrps = mysqli_query($konek, "select idrevisi_raxips from revisi_raxi_ps where nofile_revisi_raxips='$nofile'");
                        $cridrps = mysqli_fetch_array($cridrps);
                        $idraxips = $cridrps["idrevisi_raxips"] ?? "";

                        // echo $iddraxips;


                        $no = 1;
                        $sql = mysqli_query($konek, "SELECT d.iddetail_revisi_raxips,u.nama_unit,d.no_unit,d.wo_unit FROM revisi_detail_raxips d INNER JOIN unit u ON d.idunit=u.idunit WHERE idrevisi_raxips='$idraxips'");
                        while ($row = mysqli_fetch_array($sql)) {
                        ?>
                            <table style="width: 100%;">
                                <thead>
                                    <tr style="text-align: center;">
                                        <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>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $iddraxips = $row['iddetail_revisi_raxips'];
                                    $querygabung = mysqli_query(
                                        $konek,
                                        "SELECT s.jenis_service AS keterangan,j.harga_serviceraxi AS harga,j.qty_service AS kuota,j.totalservice_raxi AS total,j.keterangan AS kete FROM revisi_uraianraxips_service j INNER JOIN jenis_service s ON j.idjenis_service=s.idjenis_service WHERE j.iddetail_revisi_raxips  = '$iddraxips'
                                        UNION ALL
                                        SELECT b.nama_barang AS keterangan,ub.harga_barangraxi AS harga,ub.qty AS kuota,ub.totalbarang_raxi AS total,ub.keterangan AS kete FROM revisi_uraianraxips_barang ub INNER JOIN barang b ON ub.idbarang=b.idbarang WHERE ub.iddetail_revisi_raxips  = '$iddraxips'"
                                    );
                                    $data = mysqli_num_rows($querygabung);
                                    $noe = 0;
                                    while ($d    = mysqli_fetch_array($querygabung)) {
                                    ?>
                                        <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++;
                                    }
                                    $query_total = mysqli_query($konek, "
                                        SELECT SUM(total) AS grand_total
                                        FROM (
                                            SELECT totalservice_raxi AS total
                                            FROM revisi_uraianraxips_service
                                            WHERE iddetail_revisi_raxips = '$iddraxips'

                                            UNION ALL

                                            SELECT totalbarang_raxi AS total
                                            FROM revisi_uraianraxips_barang
                                            WHERE iddetail_revisi_raxips = '$iddraxips'
                                        ) AS total_all
                                    ");

                                    $total = mysqli_fetch_assoc($query_total);
                                    ?>

                                    <tr style="text-align: center;">
                                        <td rowspan="3" colspan="2" style="border-left: none;border-bottom: none;"></td>
                                        <td colspan="3" style="font-weight: 700;">Subtotal</td>
                                        <th>
                                            <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;">
                                                <span style="text-align: left;">Rp</span>
                                                <span>
                                                    <?= number_format($tuotal = $total['grand_total'], 0, ',', '.') ?>
                                                </span>
                                            </div>
                                        </th>
                                    </tr>
                                    <tr style="text-align: center;">
                                        <td colspan="3" style="font-weight: 700;">PPN 11%</td>
                                        <th>
                                            <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;">
                                                <span style="text-align: left;">Rp</span>
                                                <span>
                                                    <?= number_format($ppn = $total['grand_total'] * 0.11, 0, ',', '.') ?>
                                                </span>
                                            </div>
                                        </th>
                                    </tr>
                                    <tr style="text-align: center;">
                                        <td colspan="3" style="font-weight: 700;">TOTAL</td>
                                        <th>
                                            <div style="display: flex; justify-content: space-between;margin-left: 5px;margin-right: 5px;">
                                                <span style="text-align: left;">Rp</span>
                                                <span>
                                                    <?= number_format($kabeh = $ppn + $tuotal, 0, ',', '.') ?>
                                                </span>
                                            </div>
                                        </th>
                                    </tr>
                                </tbody>
                            </table>
                            <br>
                        <?php
                            $no++;
                        }
                        ?>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="catatan" style="margin-top: -15px;">
                        <p style="font-weight: 700;">Note : </p>
                        <?php
                        if ($rows['status_checkraxips'] == "Belum") {
                        ?>
                            <p style="margin-left: 5px;">Unit <?= $rows['status_checkraxips'] ?>
                                <?php
                                if ($rows['keterangan_status'] == "Belum di ambil") {
                                    echo "dilakukan";
                                } else {
                                    echo "diambil &";
                                }
                                ?>
                                pengecekan oleh teknisi PT Raxi 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_checkraxips'] ?>
                                <?php
                                if ($rows['keterangan_status'] == "Belum di ambil") {
                                    echo "dilakukan";
                                } else {
                                    echo "diambil &";
                                }
                                ?>
                                pengecekan oleh teknisi PT Raxi Indonesia <?= $tglcek ?>
                                (<?php echo substr($rows['checker'] ?? '-', 0, 2) ?>)</p>
                        <?php
                        }
                        ?>
                    </div>
                </td>
            </tr>
            <tr style="page-break-inside: avoid;">
                <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>
                    <div class="demikian">
                        <p>Demikian surat penawaran ini, apabila kurang jelas dapat ditanyakan langsung kepada kami dan
                            kami
                            tunggu kepastian ordernya. Atas perhatian dan kerjasamanya, terima kasih.</p>
                    </div>
                </td>
            </tr>
            <tr style="page-break-inside: avoid;">
                <td>
                    <div class="ttd" style="width: 40%;">
                        <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 aidah (raxi sv tgr).jpg" alt="" width="200"
                                style="margin-bottom: -10px;margin-top: -10px;">
                            <p style="font-weight: 700;text-decoration: underline;margin-left: 10px;">Aidah Selvia</p>
                            <p style="margin-top: -20px;margin-left: 10px;">0812 8357 1576</p>
                        <?php
                        } else {
                        ?>
                            <img src="asset/gambar/ttd ditya kusuma (raxi sv sby).png" alt="" width="200"
                                style="margin-top: -35px;margin-bottom: -20px;">
                            <p style="font-weight: 700;text-decoration: underline;">Ditya Kusuma Putri</p>
                            <p style="margin-top: -20px;">0812 2430 2982</p>
                        <?php
                        }
                        ?>
                    </div>
                </td>
            </tr>
            <tr style="text-align: center;page-break-inside: avoid;">
                <td>
                    <p style="font-weight: 700;">Contoh Pengerjaan Service Hydraulic di PT. RAXI INDONESIA</p>
                    <div class="gambare">
                        <img src="asset/gambar/kiri.png" alt="" style="width: 40%;">
                        <img src="asset/gambar/kanan.png" alt="" style="width: 40%;">
                    </div>
                </td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td>
                    <!--place holder for the fixed-position footer-->
                    <div class="page-footer-space"></div>
                </td>
            </tr>
        </tfoot>
    </table>



    <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>