<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


if (!class_exists('B2bking_Quote_Received_Email')){
    class B2bking_Quote_Received_Email extends WC_Email {

        public $message;

        public function __construct() {

            $this->message = '';

            $this->id = 'b2bking_quote_received_email';

            $this->title = esc_html__('Quote request received', 'b2bking');

            $this->customer_email = true;

            $this->description = esc_html__('Confirmation email sent to the customer after they submit a quote request.', 'b2bking');
            $this->description .= '. <a href="https://woocommerce-b2b-plugin.com/docs/how-to-customize-b2bking-emails/" target="_blank">'.esc_html__('Learn how to customize B2BKing emails.', 'b2bking').'</a>';

            $this->heading = esc_html__('We have received your quote request', 'b2bking');
            $this->subject = esc_html__('We have received your quote request', 'b2bking');

            $this->template_base  = B2BKING_DIR . 'includes/emails/templates/';
            $this->template_html  = 'quote-received-email-template.php';
            $this->template_plain = 'plain-quote-received-email-template.php';

            parent::__construct();

            add_action( 'b2bking_quote_received_notification', array($this, 'trigger'), 10, 2 );
        }

        public function trigger($email_address, $message) {

            $this->recipient = $email_address;
            $this->message = $message;

            if ( ! $this->is_enabled() || ! $this->get_recipient() ){
               return;
            }

            b2bking()->switch_to_user_locale($email_address);

            do_action('wpml_switch_language_for_email', $email_address);

            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );

            do_action('wpml_restore_language_from_email');

            b2bking()->restore_locale();
        }

        public function get_content_html() {
            ob_start();
            $additional_content_checked = method_exists($this, 'get_additional_content') ? $this->get_additional_content() : false;
            wc_get_template( $this->template_html, array(
                'email_heading'      => $this->get_heading(),
                'additional_content' => $additional_content_checked,
                'message'            => $this->message,
                'email'              => $this,
            ), $this->template_base, $this->template_base );
            return ob_get_clean();
        }

        public function get_content_plain() {
            ob_start();
            $additional_content_checked = method_exists($this, 'get_additional_content') ? $this->get_additional_content() : false;
            wc_get_template( $this->template_plain, array(
                'email_heading'      => $this->get_heading(),
                'additional_content' => $additional_content_checked,
                'message'            => $this->message,
                'email'              => $this,
            ), $this->template_base, $this->template_base );
            return ob_get_clean();
        }

        public function init_form_fields() {

            $this->form_fields = array(
                'enabled'    => array(
                    'title'   => esc_html__( 'Enable/Disable', 'b2bking' ),
                    'type'    => 'checkbox',
                    'label'   => esc_html__( 'Enable this email notification', 'b2bking' ),
                    'default' => 'yes',
                ),
                'subject'    => array(
                    'title'       => 'Subject',
                    'type'        => 'text',
                    'description' => esc_html__('This controls the email subject line. Leave blank to use the default subject: ','b2bking').sprintf( '<code>%s</code>.', $this->subject ),
                    'placeholder' => '',
                    'default'     => ''
                ),
                'heading'    => array(
                    'title'       => esc_html__('Email Heading','b2bking'),
                    'type'        => 'text',
                    'description' => esc_html__('This controls the main heading contained within the email notification. Leave blank to use the default heading: ','b2bking').sprintf( '<code>%s</code>.', $this->heading ),
                    'placeholder' => '',
                    'default'     => ''
                ),
                'email_type' => array(
                    'title'       => esc_html__('Email type','b2bking'),
                    'type'        => 'select',
                    'description' => esc_html__('Choose which format of email to send.','b2bking'),
                    'default'     => 'html',
                    'class'       => 'email_type',
                    'options'     => array(
                        'plain'     => 'Plain text',
                        'html'      => 'HTML', 'woocommerce',
                        'multipart' => 'Multipart', 'woocommerce',
                    )
                ),
                'additional_content' => array(
                    'title'       => __( 'Additional content', 'woocommerce' ),
                    'description' => __( 'Text to appear below the main email content.', 'woocommerce' ),
                    'css'         => 'width:400px; height: 75px;',
                    'placeholder' => __( 'N/A', 'woocommerce' ),
                    'type'        => 'textarea',
                    'default'     => '',
                    'desc_tip'    => true,
                ),
            );
        }

    }
}

return new B2bking_Quote_Received_Email();
