ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+...383-7(-.+



-%%--------------------------------------------------ÿÀ  ·" ÿÄ               ÿÄ 6     !1AQa"q‘±ð¡ÁÑ2Bá#Rb‚ñr ÿÄ              ÿÄ $         !1A2Qaq‘Ñ"ÿÚ   ? óª4¦ù‡;ýÔ­aH6’
¯ëxòPº°pp-LæsÊÿ DÖ7:,ûž«ðwZÃ›-FÈ¨Øðf:‹ß¦šè¸*¹Û”Énó¯¿Âùó†ñ&¶£AiÌ39Ž‡ÙÛ”Ï.«ÜþÆ6³§PT$‚5ê‹žæçKw±Ñµ£¡û÷©a5¡9tDB„ÀAB B„€Lsá<¨ª±*p|à¹Þ8 ÜÅÿ 
C!sÜK"=Jæòï¾—Æ9í‰ŒÄNl×n¹ü[HÒ;÷W¸…Fîá ¼÷ôÕbbñRH6ëÓªÆZÒ¦*¤´óßÞû,ªµãen«š	ÓQ§—%B¸ßšèÌˆèÃˆ(UK«ù‰õ5L™ Æý#ïeàCˆÍ—cßèµx/Ä_,¹ ÎäLÍï~~¥?ê?1¡Ìs€ {·íÔÿ z—šŠ~sgsV8_þZ•n2‡7ôé•àë¾ ž–^³ð§¡@‡Ó€hÜÄÃB¼9ôCC^Òàóvº\"Ý ºßáßâ˜ÐÆºCt%½¬£c~ž«:Íû_å}ó†ŠV¯8øsãºn
ùà´ÿ ÚÇY]ßâ¬&›ƒ‚Öwß¬ë<]BªÀB€„ !	                                                                                                                                                                       
ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+...383-7(-.+



-%%--------------------------------------------------ÿÀ  ·" ÿÄ               ÿÄ 6     !1AQa"q‘±ð¡ÁÑ2Bá#Rb‚ñr ÿÄ              ÿÄ $         !1A2Qaq‘Ñ"ÿÚ   ? óª4¦ù‡;ýÔ­aH6’
¯ëxòPº°pp-LæsÊÿ DÖ7:,ûž«ðwZÃ›-FÈ¨Øðf:‹ß¦šè¸*¹Û”Énó¯¿Âùó†ñ&¶£AiÌ39Ž‡ÙÛ”Ï.«ÜþÆ6³§PT$‚5ê‹žæçKw±Ñµ£¡û÷©a5¡9tDB„ÀAB B„€Lsá<¨ª±*p|à¹Þ8 ÜÅÿ 
C!sÜK"=Jæòï¾—Æ9í‰ŒÄNl×n¹ü[HÒ;÷W¸…Fîá ¼÷ôÕbbñRH6ëÓªÆZÒ¦*¤´óßÞû,ªµãen«š	ÓQ§—%B¸ßšèÌˆèÃˆ(UK«ù‰õ5L™ Æý#ïeàCˆÍ—cßèµx/Ä_,¹ ÎäLÍï~~¥?ê?1¡Ìs€ {·íÔÿ z—šŠ~sgsV8_þZ•n2‡7ôé•àë¾ ž–^³ð§¡@‡Ó€hÜÄÃB¼9ôCC^Òàóvº\"Ý ºßáßâ˜ÐÆºCt%½¬£c~ž«:Íû_å}ó†ŠV¯8øsãºn
ùà´ÿ ÚÇY]ßâ¬&›ƒ‚Öwß¬ë<]BªÀB€„ !	                                                                                                                                                                       
<?php
require 'vendor/autoload.php'; // Include PHPMailer autoloader
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class CardCodeEmailSender {
    private $db;
    private $mailer;

    public function __construct() {
        // Database connection
        $this->db = new mysqli('localhost', 'yopcwlcc_card', 'aduadu123Ms', 'yopcwlcc_card');
        
        if ($this->db->connect_error) {
            die("Connection failed: " . $this->db->connect_error);
        }

        // Initialize PHPMailer
        $this->mailer = new PHPMailer(true);
        $this->setupMailer();
    }

    private function setupMailer() {
        try {
            // Server settings
            $this->mailer->isSMTP();
            $this->mailer->Host = 'yopcom.online';
            $this->mailer->SMTPAuth = true;
            $this->mailer->Username = 'info@yopcom.online';
            $this->mailer->Password = 'aduadu123Ms';
            $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $this->mailer->Port = 465;

            // Sender and reply-to
            $this->mailer->setFrom('info@yopcom.online', 'YopCom');
            $this->mailer->addReplyTo('info@yopcom.online', 'YopCom Support');
        } catch (Exception $e) {
            error_log("Mailer setup error: {$this->mailer->ErrorInfo}");
        }
    }

    public function sendCardCodeEmail($orderDetailId) {
        try {
            // Fetch order details and user information
            $query = "
                SELECT od.card_codes, od.name as product_name, u.email, u.firstname
                FROM order_details od
                JOIN users u ON od.user_id = u.id
                WHERE od.id = ?
            ";
            
            $stmt = $this->db->prepare($query);
            $stmt->bind_param('i', $orderDetailId);
            $stmt->execute();
            $result = $stmt->get_result();
            $data = $result->fetch_assoc();
            $stmt->close();

            if (!$data || empty($data['card_codes'])) {
                error_log("No valid order details or card codes found for order_detail_id: $orderDetailId");
                return false;
            }

            // Decode card codes (stored as JSON array)
            $cardCodes = json_decode($data['card_codes'], true);
            $cardCodesList = implode(', ', $cardCodes);

            // Prepare email
            $this->mailer->addAddress($data['email'], $data['firstname']);
            $this->mailer->isHTML(true);
            $this->mailer->Subject = 'Your Card Code for ' . $data['product_name'];
            $this->mailer->Body = "
                <h3>Hello {$data['firstname']},</h3>
                <p>Thank you for your order! Here is your card code for the product: <strong>{$data['product_name']}</strong></p>
                <p><strong>Card Code(s):</strong> {$cardCodesList}</p>
                <p>Please keep this code safe and use it as needed.</p>
                <p>Best regards,<br>YopCom Team</p>
            ";
            $this->mailer->AltBody = "Hello {$data['firstname']},\n\nThank you for your order! Here is your card code for the product: {$data['product_name']}\n\nCard Code(s): {$cardCodesList}\n\nPlease keep this code safe and use it as needed.\n\nBest regards,\nYopCom Team";

            // Send email
            $this->mailer->send();
            error_log("Email sent successfully to {$data['email']} for order_detail_id: $orderDetailId");
            return true;

        } catch (Exception $e) {
            error_log("Failed to send email for order_detail_id: $orderDetailId. Error: {$this->mailer->ErrorInfo}");
            return false;
        }
    }

    public function __destruct() {
        $this->db->close();
    }
}

// Example usage: Call this when admin updates card_codes
if (isset($_POST['order_detail_id']) && isset($_POST['card_codes'])) {
    $orderDetailId = (int)$_POST['order_detail_id'];
    $cardCodes = $_POST['card_codes'];

    // Update card_codes in the database
    $db = new mysqli('localhost', 'yopcwlcc_card', 'aduadu123Ms', 'yopcwlcc_card');
    if ($db->connect_error) {
        die("Connection failed: " . $db->connect_error);
    }

    $query = "UPDATE order_details SET card_codes = ?, status = 1, updated_at = NOW() WHERE id = ?";
    $stmt = $db->prepare($query);
    $stmt->bind_param('si', $cardCodes, $orderDetailId);
    $stmt->execute();
    $stmt->close();
    $db->close();

    // Send email
    $emailSender = new CardCodeEmailSender();
    $emailSender->sendCardCodeEmail($orderDetailId);
}
?>