<?php
/**
 * Created by PhpStorm.
 * User: Villatheme-Thanh
 * Date: 08-06-19
 * Time: 12:01 PM
 */

namespace WACVP\Inc\Settings;

use WACVP\Inc\Functions;
use WACVP\Inc\Email\ActiveCampaign_WACV;
use WACVP\Inc\Email\Sendinblue_WACV;
use WACVP\Inc\Email\GetResponse_WACV;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Email_Settings extends Admin_Settings {

	protected static $instance = null;

	public function __construct() {
	}

	public static function get_instance() {

		if ( null == self::$instance ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	public function setting_page() {
		?>
        <div class="vi-ui bottom attached tab segment tab-admin" data-tab="email">

            <h4><?php esc_html_e( 'Email for Abandoned Cart', 'woo-abandoned-cart-recovery' ) ?></h4>
            <table class="wacv-table">
				<?php
				$this->checkbox_option( 'send_email_to_member', esc_html__( "Send mail reminder to members", 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'send_email_to_guest', esc_html__( "Send mail reminder to guest", 'woo-abandoned-cart-recovery' ) );
				$this->text_option( 'email_reply_address', esc_html__( "Reply Emails to", 'woo-abandoned-cart-recovery' ) );
				$this->send_email_rules_settings( 'email_rules', true );
				$this->text_option( 'email_abandoned_cc', esc_html__( 'Add Cc email when send abandoned email', 'woo-abandoned-cart-recovery' ), '', '', false );
				$this->text_option( 'email_abandoned_bcc', esc_html__( 'Add Bcc email when send abandoned email', 'woo-abandoned-cart-recovery' ), '', '', false );
				$this->checkbox_option( 'email_to_admin_when_cart_recover', esc_html__( "Notification to Admin", 'woo-abandoned-cart-recovery' ), esc_html__( 'Send a notification email to admin whenever a cart is recovered', 'woo-abandoned-cart-recovery' ) );
				$this->text_option( 'email_custom_when_cart_recover', esc_html__( 'Notification to custom email', 'woo-abandoned-cart-recovery' ), '', '', true );
				$this->checkbox_option( 'send_email_unique_address', esc_html__( "One-time abandoned cart email", 'woo-abandoned-cart-recovery' ), esc_html__( 'Enable this option to prevent multiple abandoned cart emails from being sent to the same visitor. Only one email will be sent per user, even if multiple carts are abandoned.', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'price_incl_tax', esc_html__( "Price include tax", 'woo-abandoned-cart-recovery' ), esc_html__( 'Display price include tax if tax is enabled & prices entered exclusive of tax', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'email_item_link', esc_html__( "Product link", 'woo-abandoned-cart-recovery' ), esc_html__( 'Enable product link in the product detail in the abandoned emails', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'email_item_price', esc_html__( "Product price", 'woo-abandoned-cart-recovery' ), esc_html__( 'Enable product price in the product detail in the abandoned emails', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'email_item_price_calc', esc_html__( "Display current price", 'woo-abandoned-cart-recovery' ), esc_html__( 'Enable to display current product price in the product detail in Abandoned email, disable to show price saved in abandoned cart', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'email_new_cart_block', esc_html__( "Template use new cart block", 'woo-abandoned-cart-recovery' ), esc_html__( 'Use new cart block to edit more text, if you have old template, please edit and replace by new cart block to use', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'enable_active_campaign', esc_html__( "Active Campaign", 'woo-abandoned-cart-recovery' ), esc_html__( 'Once abandoned email is sent out to customers, use Active Campaign to achieve customer email', 'woo-abandoned-cart-recovery' ) );
				$this->text_option( 'active_campaign_api', esc_html__( 'Active Campaign API Key', 'woo-abandoned-cart-recovery' ), '', '', true );
				$this->text_option( 'active_campaign_url', esc_html__( 'Active Campaign API URL', 'woo-abandoned-cart-recovery' ), '', '', true );
				$ac_mail_list  = array();
				$data_enable_ac = self::get_field( 'enable_active_campaign' );
				if ($data_enable_ac) {
					$activeCampain = new ActiveCampaign_WACV();
					$ac_mail_lists = $activeCampain->get_lists();
					if ( is_array( $ac_mail_lists ) ) {
						foreach ( $ac_mail_lists as $_list ) {
							$ac_mail_list[ $_list->id ] = $_list->name;
						}
					}
				}
                $this->select_option( 'active_campaign_list', $ac_mail_list, esc_html__( "Active Campaign contacts list", 'woo-abandoned-cart-recovery' ) );

                $this->checkbox_option( 'enable_sendinblue', esc_html__( "Sendinblue", 'woo-abandoned-cart-recovery' ), esc_html__( 'Once abandoned email is sent out to customers, use Sendinblue to achieve customer email', 'woo-abandoned-cart-recovery' ) );
				$this->text_option( 'sendinblue_api', esc_html__( 'Sendinblue API Key', 'woo-abandoned-cart-recovery' ), '', '', true );
				$sib_mail_list  = array();
				$data_enable_sendinblue = self::get_field( 'enable_sendinblue' );
				if ($data_enable_sendinblue) {
					$sendinblue = new Sendinblue_WACV();
					$sib_mail_lists = $sendinblue->get_lists();
					if ( is_array( $sib_mail_lists ) ) {
						foreach ( $sib_mail_lists as $_list ) {
							$sib_mail_list[ $_list->id ] = $_list->name;
						}
					}
                }
				$this->select_option( 'sendinblue_list', $sib_mail_list, esc_html__( "Sendinblue contacts list", 'woo-abandoned-cart-recovery' ), '', '', true );

				$this->checkbox_option( 'enable_getresponse', esc_html__( "GetResponse", 'woo-abandoned-cart-recovery' ), esc_html__( 'Once abandoned email is sent out to customers, use GetResponse to achieve customer email', 'woo-abandoned-cart-recovery' ) );
				$data_enable_gr = self::get_field( 'enable_getresponse' );
				$mail_list_gr = [];
				if ($data_enable_gr) {
					$getresponse      = new GetResponse_WACV();
					$mail_lists_gr    = $getresponse->get_lists();
					if ( is_array( $mail_lists_gr ) ) {
						foreach ( $mail_lists_gr as $_list ) {
							$mail_list_gr[ $_list->campaignId ] = $_list->name;
						}
					}
				}
				$this->text_option( 'getresponse_api', esc_html__( 'GetResponse API Key', 'woo-abandoned-cart-recovery' ), '', '', true );
				$this->select_option( 'getresponse_list', $mail_list_gr, esc_html__( "GetResponse campaign (list)", 'woo-abandoned-cart-recovery' ) );

				?>
            </table>
            <hr>

            <h4><?php esc_html_e( 'Email for Abandoned Order', 'woo-abandoned-cart-recovery' ) ?></h4>
            <table class="wacv-table">
				<?php
				$order_stt = array(
					'wc-failed'    => 'Failed',
					'wc-cancelled' => 'Cancelled',
					'wc-pending'   => 'Pending payment',
					'wc-on-hold'   => "On hold",
				);

				if ( defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) && WC_BLOCKS_IS_FEATURE_PLUGIN ) {
					$order_stt['wc-checkout-draft'] = 'Draft';
				}

				$this->checkbox_option( 'enable_reminder_order', esc_html__( "Enable", 'woo-abandoned-cart-recovery' ), esc_html__( 'Send abandoned emails to all specific orders. Please limit email sending to orders created within a specific time and matching certain order statuses. Choose the order statuses and set the time frame below.', 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'enable_order_coupon', esc_html__( "Use Coupon", 'woo-abandoned-cart-recovery' ), esc_html__( "Include a coupon when sending an abandoned order email. Coupon settings are based on the email template's coupon configuration.", 'woo-abandoned-cart-recovery' ) );
				$this->checkbox_option( 'order_coupon_usage_limit', esc_html__( "Coupon Usage limit", 'woo-abandoned-cart-recovery' ), esc_html__( "Based on the order item quantity, this option sets how many times the coupon can be used before it becomes invalid.", 'woo-abandoned-cart-recovery' ) );
				$this->date_time_option( 'reminder_order_time', esc_html__( 'Send to orders after date', 'woo-abandoned-cart-recovery' ),
                    esc_html__( 'Abandoned Order only send if it created after this time', 'woo-abandoned-cart-recovery' ), 'days' );
				$this->select_option( 'order_stt', $order_stt, esc_html__( "Order status", 'woo-abandoned-cart-recovery' ), '', '', true );
				$this->send_email_rules_settings( 'abd_orders', true );
				?>
            </table>
            <hr>
            <table class="wacv-table">
				<?php
				$size_options = wp_get_additional_image_sizes();
				$r            = [ '' => esc_html__( 'Original' ) ];
				foreach ( $size_options as $key => $args ) {
					$height    = $args['height'] ? $args['height'] : 'auto';
					$width     = $args['width'] ? $args['width'] : 'auto';
					$r[ $key ] = "{$key} ({$width}x{$height})";
				}
				$this->select_option( 'image_size', $r, esc_html__( "Image size", 'woo-abandoned-cart-recovery' ), '', '' );
				?>
            </table>
            <p class="vi-ui yellow message"><?php esc_html_e( 'Note: Recover link will not work with Admin role', 'woo-abandoned-cart-recovery' ); ?></p>
        </div>
		<?php
	}

	//Email Rules

	public function send_email_rules_settings( $slug, $multilingual = '' ) {
		$data          = self::get_field( $slug );
		$list_template = Functions::get_email_template();
		?>
        <tr>
            <td class="col-1">
                <label><?php esc_html_e( 'Send mail rules', 'woo-abandoned-cart-recovery' ) ?></label>
            </td>

            <td class="col-2">
                <table class="vi-ui celled table wacv-email-rules-table">
                    <thead>
                    <tr>
                        <th><?php esc_html_e( 'Send after', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th><?php esc_html_e( 'Unit', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th><?php esc_html_e( 'Email template', 'woo-abandoned-cart-recovery' ); ?></th>
                        <th><?php esc_html_e( 'Action', 'woo-abandoned-cart-recovery' ); ?></th>
                    </tr>
                    </thead>
                    <tbody class="wacv-<?php echo esc_attr( $slug ) ?>-row-target">
					<?php
					if ( isset( $data['time_to_send'] ) ) {
						$loop = count( $data['time_to_send'] );

						for ( $i = 0; $i < $loop; $i ++ ) { ?>
                            <tr class="wacv-<?php echo esc_attr( $slug ) ?>-row-target" data-index="<?php echo esc_attr( $i ) ?>">
                                <td class="vlt-padding-small cols-1">
                                    <input type="number" name="wacv_params[<?php echo esc_attr( $slug ) ?>][time_to_send][]"
                                           class="vlt-input vlt-border vlt-none-shadow vlt-round"
                                           value="<?php echo esc_attr( $data['time_to_send'][ $i ] ) ?>" min="1">
                                </td>
                                <td class="vlt-padding-small cols-2">
                                    <select name="wacv_params[<?php echo esc_attr( $slug ) ?>][unit][]"
                                            class="vlt-input vlt-border vlt-none-shadow vlt-round">
                                        <option value="minutes" <?php echo $data['unit'][ $i ] == 'minutes' ? 'selected' : ''; ?>><?php esc_html_e( 'minutes', 'woo-abandoned-cart-recovery' ); ?></option>
                                        <option value="hours" <?php echo $data['unit'][ $i ] == 'hours' ? 'selected' : ''; ?>><?php esc_html_e( 'hours', 'woo-abandoned-cart-recovery' ); ?></option>
                                    </select>
                                </td>
                                <td class="vlt-padding-small cols-3">
                                    <select name="wacv_params[<?php echo esc_attr( $slug ) ?>][template][]"
                                            class="wacv-select-email-template vlt-input vlt-border vlt-none-shadow vlt-round">
										<?php
										foreach ( $list_template as $template ) {
											$selected = '';
											if ( isset( $data['template'][ $i ] ) ) {
												$selected = $template['id'] == $data['template'][ $i ] ? 'selected' : '';
											}
											echo "<option value='" . esc_attr( $template['id'] ) . "' " . esc_attr( $selected ) . ">" . esc_html( $template['value'] ) . "</option>";
										}
										?>
                                    </select>
                                    <?php
                                    if ( $multilingual ) {
	                                    if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
		                                    global $sitepress;
		                                    $default_lang           = $sitepress->get_default_language();
		                                    $languages = $langs = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );
		                                    if ( count( $languages ) ) {
			                                    foreach ( $languages as $key => $language ) {
				                                    if ( $key == $default_lang ) {
					                                    continue;
				                                    }
				                                    ?>
                                                    <p class="wacv-mlg-label"><?php echo esc_html( $language['native_name'] . ':' ) ?></p>
                                                    <select name="wacv_params[<?php echo esc_attr( $slug ) ?>][template<?php echo esc_attr('_' . $key) ?>][]"
                                                            class="wacv-select-email-template vlt-input vlt-border vlt-none-shadow vlt-round">
					                                    <?php
					                                    foreach ( $list_template as $template ) {
						                                    $selected = '';
						                                    if ( isset( $data['template_' . $key][ $i ] ) ) {
							                                    $selected = $template['id'] == $data['template_' . $key][ $i ] ? 'selected' : '';
						                                    }
						                                    echo "<option value='" . esc_attr( $template['id'] ) . "' " . esc_attr( $selected ) . ">" . esc_html( $template['value'] ) . "</option>";
					                                    }
					                                    ?>
                                                    </select>
				                                    <?php
			                                    }
		                                    }
	                                    } elseif ( class_exists( 'Polylang' ) ) {
		                                    $languages = pll_languages_list();
		                                    foreach ( $languages as $language ) {
			                                    $default_lang = pll_default_language( 'slug' );

			                                    if ( $language == $default_lang ) {
				                                    continue;
			                                    }
//			                                    $field_name_mlg = $slug . '_' . $language;
//			                                    $class_mlg    = 'wacv-' . str_replace( '_', '-', $field_name_mlg );
//			                                    $set_name_mlg = $this->set_field( $field_name_mlg, $multi );
			                                    ?>
                                                <p class="wacv-mlg-label"><?php echo esc_html( $language . ':' ) ?></p>
                                                <select name="wacv_params[<?php echo esc_attr( $slug ) ?>][template<?php echo esc_attr('_' . $language) ?>][]"
                                                        class="wacv-select-email-template vlt-input vlt-border vlt-none-shadow vlt-round">
				                                    <?php
				                                    foreach ( $list_template as $template ) {
					                                    $selected = '';
					                                    if ( isset( $data['template_' . $language][ $i ] ) ) {
						                                    $selected = $template['id'] == $data['template_' . $language][ $i ] ? 'selected' : '';
					                                    }
					                                    echo "<option value='" . esc_attr( $template['id'] ) . "' " . esc_attr( $selected ) . ">" . esc_html( $template['value'] ) . "</option>";
				                                    }
				                                    ?>
                                                </select>
			                                    <?php
		                                    }
	                                    }
                                    }
                                    ?>
                                </td>
                                <td align="center" class="vlt-padding-small cols-4">
                                    <button class="wacv-delete-<?php echo esc_attr( $slug ) ?> vi-ui small icon red button"
                                            type="button">
                                        <i class="trash icon"> </i>
                                    </button>
                                </td>
                            </tr>
						<?php }
					} ?>
                    </tbody>
                </table>
                <button type="button" class="wacv-add-<?php echo esc_attr( $slug ) ?> vi-ui small icon green button">
					<?php esc_html_e( 'Add rule', 'woo-abandoned-cart-recovery' ); ?>
                </button>
            </td>
            <td class="col-3"></td>
        </tr>
		<?php
	}
}
