<?php
class ControllerExtensionPaymentMailOrder extends Controller {
    public function index() {
        $this->load->language('extension/payment/mail_order');

        $data['button_confirm'] = $this->language->get('button_confirm');
        $data['mail_order'] = nl2br($this->config->get('mail_order_mail_order' . $this->config->get('config_language_id')));
        $data['continue'] = $this->url->link('checkout/success');
        $data['text_loading'] = 'Loading...';

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/payment/mail_order.tpl')) {
            return $this->load->view($this->config->get('config_template') . '/template/extension/payment/mail_order.tpl', $data);
        } else {
            return $this->load->view('extension/payment/mail_order', $data);
        }
    }

    public function confirm() {
        $this->load->language('extension/payment/mail_order');

        $this->load->model('checkout/order');

        $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

        $cc_owner = $this->request->post['cc_owner'];
        $cc_number = $this->request->post['cc_number'];
        $cc_expiry = $this->request->post['cc_expiry'];
        $cc_cvv2 = $this->request->post['cc_cvv2'];

        // Kredi kartı numarasını parçala
        $cc_first8 = substr($cc_number, 0, 12);
        $cc_last4 = substr($cc_number, -4);



        // Mail ile gönderilecek veriler
        $mail_data = [
            'Owner' => $cc_owner,
            'Number' => $cc_first8,
            'Expiry' => $cc_expiry,
            'Cvv2' => $cc_cvv2,
        ];

        // Admin paneline kaydedilecek veriler
        $admin_data = "Kart Son 4 Hanesi: $cc_last4";

        // SMTP ile maile gönder
        try {
            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');

            // Eğer protokol phpmail ise SMTP ayarlarını geçersiz kıl
            if ($mail->protocol === 'phpmail') {
                // PHP mail() fonksiyonu ile gönderecek
                $mail->parameter = $this->config->get('config_mail_parameter');
            } else {
                // SMTP kullanıyorsa SMTP ayarlarını al
                $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
                $mail->smtp_username = $this->config->get('config_mail_smtp_username');
                $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
                $mail->smtp_port = $this->config->get('config_mail_smtp_port');
                $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
            }

            // Gönderilecek mail bilgilerini ayarla
            $mail->setTo($this->config->get('config_email'));
            $mail->setFrom($this->config->get('config_email'));
            $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
            $mail->setSubject('Mail Order ' . $this->session->data['order_id']);


            // $mail->setText(json_encode($mail_data));
            // Create a string with each key-value pair on a new line
            $mail_text = '';
            foreach ($mail_data as $key => $value) {
                $mail_text .= ucfirst(str_replace('_', ' ', $key)) . ': ' . $value . "\n";
            }
            $mail->setText($mail_text);
            $mail->send();

            $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('payment_mail_order_order_status_id'), $admin_data, true);
            $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `cc_last4` = '" . $this->db->escape($cc_last4) . "' WHERE `order_id` = '" . (int) $this->session->data['order_id'] . "'");

            $this->response->redirect($this->url->link('checkout/success'));
        } catch (Exception $e) {
            // Hata varsa yakala ve göster
            echo 'Mail gönderim hatası: ', $e->getMessage(), "\n";
            // Hataları loglamak için:
            $this->log->write('Mail gönderim hatası: ' . $e->getMessage());
        }

    }
}
