<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use mysqli;

require 'vendor/autoload.php'; // Pastikan Anda telah menginstal PHPMailer via Composer

function send_bom_email($parent_bom, $phantom_bom, $level, $child_boms, $qtys, $location, $buy_price) {
    $mail = new PHPMailer(true);

    try {
        // Konfigurasi SMTP untuk cPanel
        $mail->isSMTP();
        $mail->Host = 'mail.wiselie.co.id'; // Host SMTP untuk cPanel
        $mail->SMTPAuth = true;
        $mail->Username = 'admin@wiselie.co.id'; // Username SMTP (email cPanel)
        $mail->Password = 'Rnanda123.'; // Password email cPanel
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL/TLS
        $mail->Port = 465; // Port SMTP untuk SSL

        // Penerima
        $mail->setFrom('admin@wiselie.co.id', 'RND');
        $mail->addAddress('r.nanda.industrial@gmail.com', 'PPIC');
        $mail->addAddress('rawmatwiselie@gmail.com', 'PPIC'); // Email kedua
        $mail->addAddress('lazuviayu8719@gmail.com', 'Accounting'); // Email kedua

        // Konten
        $mail->isHTML(true);
        $mail->Subject = 'BOM NOTICE';
        
        // Koneksi ke database
        $mysqli = new mysqli("localhost", "wiselie_nanda", "Clemira123", "wiselie_inventory");

        if ($mysqli->connect_error) {
            throw new Exception('Database connection failed: ' . $mysqli->connect_error);
        }

        // Mendapatkan nama dari parent_bom
        $stmt_parent = $mysqli->prepare("SELECT name FROM products WHERE part_number = ?");
        $stmt_parent->bind_param("s", $parent_bom);
        $stmt_parent->execute();
        $stmt_parent->bind_result($parent_name);
        $stmt_parent->fetch();
        $stmt_parent->close();

        // Mendapatkan data lengkap dari parent_bom
        $stmt_full = $mysqli->prepare("SELECT part_number, name, new_code, quantity, buy_price FROM products WHERE part_number = ?");
        $stmt_full->bind_param("s", $parent_bom);
        $stmt_full->execute();
        $result_full = $stmt_full->get_result();
        $parent_bom_info = $result_full->fetch_assoc();
        $stmt_full->close();

        // Mendapatkan daftar BOM yang terkait dengan parent_bom
        $stmt_bom_level = $mysqli->prepare("
            SELECT child_bom, qty, location 
            FROM bom_level_rnd 
            WHERE parent_bom = ?
        ");
        $stmt_bom_level->bind_param("s", $parent_bom);
        $stmt_bom_level->execute();
        $result_bom_level = $stmt_bom_level->get_result();

        $bom_level_list = '';
        while ($bom = $result_bom_level->fetch_assoc()) {
            $child_bom = $bom['child_bom'];
            $qty = $bom['qty'];
            $location = $bom['location'];

            // Query untuk mendapatkan nama dari child_bom
            $stmt_child = $mysqli->prepare("SELECT name FROM products WHERE part_number = ?");
            $stmt_child->bind_param("s", $child_bom);
            $stmt_child->execute();
            $stmt_child->bind_result($child_name);
            $stmt_child->fetch();
            $stmt_child->close();

            $bom_level_list .= "<tr>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$child_bom</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$child_name</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$qty</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$location</td>
                                  </tr>";
        }
        $stmt_bom_level->close();

        $child_bom_list = '';
        foreach ($child_boms as $key => $child_bom) {
            $qty = isset($qtys[$key]) ? $qtys[$key] : '';

            // Query untuk mendapatkan new_code dan name dari child_bom
            $stmt_child = $mysqli->prepare("SELECT new_code, name FROM products WHERE part_number = ?");
            $stmt_child->bind_param("s", $child_bom);
            $stmt_child->execute();
            $stmt_child->bind_result($new_code, $name);
            $stmt_child->fetch();
            $stmt_child->close();

            $child_bom_list .= "<tr>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$child_bom</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$name</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$new_code</td>
                                    <td style='padding: 8px; border: 1px solid #ddd;'>$qty</td>
                                  </tr>";
        }
        
        $parent_bom_details = "
            <table>
                <thead>
                    <tr><th>Part Number</th><th>Name</th><th>New Code</th><th>Quantity</th><th>Buy Price</th></tr>
                </thead>
                <tbody>
                    <tr>
                        <td style='padding: 8px; border: 1px solid #ddd;'>{$parent_bom_info['part_number']}</td>
                        <td style='padding: 8px; border: 1px solid #ddd;'>{$parent_bom_info['name']}</td>
                        <td style='padding: 8px; border: 1px solid #ddd;'>{$parent_bom_info['new_code']}</td>
                        <td style='padding: 8px; border: 1px solid #ddd;'>{$parent_bom_info['quantity']}</td>
                        <td style='padding: 8px; border: 1px solid #ddd;'>{$parent_bom_info['buy_price']}</td>
                    </tr>
                </tbody>
            </table>
        ";

        $mail->Body = "
            <html>
            <head>
                <style>
                    body {
                        font-family: Arial, sans-serif;
                        color: #333;
                        line-height: 1.6;
                        margin: 0;
                        padding: 0;
                    }
                    .container {
                        width: 80%;
                        margin: 0 auto;
                        padding: 20px;
                        background: #f9f9f9;
                        border: 1px solid #ddd;
                        border-radius: 5px;
                    }
                    h1 {
                        color: #0056b3;
                        margin-bottom: 20px;
                    }
                    p {
                        margin: 10px 0;
                    }
                    table {
                        width: 100%;
                        border-collapse: collapse;
                        margin-top: 20px;
                    }
                    th, td {
                        text-align: left;
                        padding: 12px;
                        border-bottom: 1px solid #ddd;
                    }
                    th {
                        background-color: #f2f2f2;
                    }
                    tr:nth-child(even) {
                        background-color: #f9f9f9;
                    }
                    tr:hover {
                        background-color: #f1f1f1;
                    }
                </style>
            </head>
            <body>
                <div class='container'>
                    <h1>New BOM Created</h1>
                    <p><strong>Parent BOM:</strong> $parent_bom</p>
                    <p><strong>Parent Name:</strong> $parent_name</p>
                    <p><strong>Phantom BOM:</strong> " . ($phantom_bom ? 'Yes' : 'No') . "</p>
                    <p><strong>Level:</strong> $level</p>
                    <p><strong>Location:</strong> $location</p>
                    <p><strong>Buy Price:</strong> $buy_price</p>
                    <p><strong>Child BOMs:</strong></p>
                    <table>
                        <thead>
                            <tr><th>Child BOM</th><th>Name</th><th>New Code</th><th>Quantity</th></tr>
                        </thead>
                        <tbody>
                            $child_bom_list
                        </tbody>
                    </table>
                    <p><strong>Parent BOM Details in BOM Level:</strong></p>
                    <table>
                        <thead>
                            <tr><th>Child BOM</th><th>Name</th><th>Quantity</th><th>Location</th></tr>
                        </thead>
                        <tbody>
                            $bom_level_list
                        </tbody>
                    </table>
                </div>
            </body>
            </html>
        ";

        $mail->send();
        return true;
    } catch (Exception $e) {
        return false;
    } finally {
        $mysqli->close(); // Pastikan koneksi ditutup
    }
}
?>
