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

// Autoload PHPMailer via Composer
require '/home/wiselie/public_html/vendor/autoload.php';

// Konfigurasi SMTP untuk email cPanel
$smtpHost = 'mail.wiselie.co.id'; // Host SMTP untuk cPanel
$smtpUsername = 'admin@wiselie.co.id'; // Username SMTP (email cPanel)
$smtpPassword = 'Rnanda123.'; // Password email cPanel
$smtpPort = 465; // Port SMTP untuk SSL
$smtpEncryption = PHPMailer::ENCRYPTION_SMTPS; // Enkripsi SSL

// Set default scan status date to current date
$current_date = date('Y-m-d');

// Konfigurasi database
$host = 'localhost';
$user = 'wiselie_nanda';
$password = 'Clemira123';
$database = 'wiselie_inventory';

// Koneksi ke database
$db = new mysqli($host, $user, $password, $database);
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

// Query untuk mendapatkan ringkasan data oleh packer untuk tanggal saat ini, hanya untuk Surabaya
$sql_summary_by_packer = "
    SELECT 
        packer_no, 
        COUNT(DISTINCT resi) as total
    FROM awb
    WHERE DATE(scan_status) = '{$current_date}' AND (loc IS NULL OR loc = 'surabaya')  -- Mengambil hanya data untuk Surabaya
    GROUP BY packer_no
    ORDER BY packer_no ASC";

$result_summary_by_packer = $db->query($sql_summary_by_packer);
$summary_by_packer = [];
while ($row_summary_by_packer = $result_summary_by_packer->fetch_assoc()) {
    $summary_by_packer[] = $row_summary_by_packer;
}

// Set recipient emails
$to = [
    'r.nanda.industrial@gmail.com',
    'fajarpab@gmail.com',
    'Joswiselie@gmail.com',
    'hrwiselieindonesia@gmail.com'
];

$subject = "Laporan Packer Harian untuk Surabaya - {$current_date}";
$message = "
<html>
<head>
    <title>Laporan Packer Harian</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        h1 {
            color: #333;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 12px;
            text-align: center;
        }
        th {
            background-color: #f4f4f4;
        }
    </style>
</head>
<body>
    <div class='container'>
        <h1>Laporan Packer Harian untuk Surabaya - {$current_date}</h1>
        <table>
            <thead>
                <tr>
                    <th>Packer Number</th>
                    <th>Total Resi Terpacking</th>
                </tr>
            </thead>
            <tbody>
";

foreach ($summary_by_packer as $packer) {
    $message .= "
                <tr>
                    <td>{$packer['packer_no']}</td>
                    <td>{$packer['total']}</td>
                </tr>
    ";
}

$message .= "
            </tbody>
        </table>
    </div>
</body>
</html>
";

// Initialize send status
$sendStatus = '';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = $smtpHost;
    $mail->SMTPAuth   = true;
    $mail->Username   = $smtpUsername;
    $mail->Password   = $smtpPassword;
    $mail->SMTPSecure = $smtpEncryption;
    $mail->Port       = $smtpPort;

    // Recipients
    $mail->setFrom($smtpUsername, 'Admin Packer'); // Nama pengirim menjadi 'Admin Packer'

    // Add recipient emails
    foreach ($to as $email) {
        $mail->addAddress($email);
    }

    // Content
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body    = $message;

    $mail->send();
    $sendStatus = 'Pesan telah berhasil dikirim';
} catch (Exception $e) {
    $sendStatus = "Pesan gagal dikirim. Mailer Error: {$mail->ErrorInfo}";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Status Pengiriman Email</title>
</head>
<body>

<h1>Status Pengiriman Email</h1>
<p><?php echo $sendStatus; ?></p>

</body>
</html>
