<?php
/**
 * Reward Email Template Module
 *
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

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

use Appsbd_Lite\V5\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V5\libs\Ajax_Data_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Cassandra\Map;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Email_Template;


/**
 * Class Email_Template
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_Email_Template extends Vite_Reward_Module_Lite {


	/**
	 * The on initialize is generated by appsbd
	 */
	public function initialize() {
		add_filter(
			'appsbd/input/html/fields',
			function ( $html_field ) {
				$html_field[] = 'content';

				return $html_field;
			}
		);
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		$this->add_admin_ajax_action( 'get-template-list', array( $this, 'get_template_list' ) );
		$this->add_admin_ajax_action( 'update-template', array( $this, 'update_template' ) );
		$this->add_admin_ajax_action( 'get-template-details', array( $this, 'template_details' ) );
		$this->add_admin_ajax_action( 'change-status', array( $this, 'change_status' ) );
	}

	/**
	 * The on active is generated by appsbd.
	 */
	public function on_active() {
		parent::on_active();
		$this->add_default_templates();
	}

	/**
	 * The add default template is generated by appsbd
	 */
	public function add_default_templates() {

		Mapbd_Reward_Email_Template::add_new_template(
			'SUP',
			$this->__( 'To Customer' ),
			$this->__( 'On user signup' ),
			'A',
			'Signup Points Notification',
			'points=Added points,total_points=Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,msg=Message',
			'<p>You have received <strong>{{points}}</strong> points and your total points are <strong>{{total_points}}</strong> </p>'
		);

		Mapbd_Reward_Email_Template::add_new_template(
			'POP',
			$this->__( 'To Customer' ),
			$this->__( 'On order complete' ),
			'A',
			'Order Points Notification',
			'points=Added points,total_points=Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,order_no=Order Number,sub_total=Order Sub Total,order_total=Cart Total,msg=Message',
			'<p>Hello {{customer_name}},</p><p>From Order No : {{order_no}}</p><p>You have received <strong>{{points}}</strong> points and your total points are <strong>{{total_points}}</strong> .</p>'
		);
		Mapbd_Reward_Email_Template::add_new_template(
			'ORP',
			$this->__( 'To Customer' ),
			$this->__( 'On Redeem Reward Point' ),
			'A',
			'Reward Redeem Notification',
			'points=Redeemed points,total_points=Current Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,order_no=Order Number,redeemed_amount=Order Redeemed amount,sub_total=Order Sub Total,order_total=Cart Total,msg=Message',
			'<p>Hello {{customer_name}},</p><p>On Order : {{order_no}}</p><p>You have used <strong>{{points}}</strong> points and now your total current points are <strong>{{total_points}}</strong> .</p>'
		);
		Mapbd_Reward_Email_Template::add_new_template(
			'ROR',
			$this->__( 'To Customer' ),
			$this->__( 'On Redeem Order Refund' ),
			'A',
			'Order Refund Notification',
			'points=Redeemed points,total_points=Current Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,order_no=Order Number,refund_id=Refund Id,redeemed_amount=Order Redeemed amount,sub_total=Order Sub Total,order_total=Cart Total,msg=Message',
			'<p>Hello {{customer_name}},</p><p>We have refunded your order: <strong>{{refund_id}}</strong>.</p><p>As a result of this refund, <strong>{{points}}</strong> points have been restored to your rewards balance.</p><p>Your updated total rewards points are now <strong>{{total_points}}</strong>.</p>',
		);
		Mapbd_Reward_Email_Template::add_new_template(
			'ORR',
			$this->__( 'To Customer' ),
			$this->__( 'On Order Refund Reward' ),
			'A',
			'Order Refund Notification',
			'points=Redeemed points,total_points=Current Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,order_no=Order Number,refund_id=Refund Id,redeemed_amount=Order Redeemed amount,sub_total=Order Sub Total,order_total=Cart Total,msg=Message',
			'<p>Hello {{customer_name}},</p><p>We have refunded your order: <strong>{{refund_id}}</strong>.</p><p>As a result of this refund, <strong>{{points}}</strong> points have been deducted from your rewards balance.</p><p>Your updated total rewards points are now <strong>{{total_points}}</strong>.</p>',
		);
		Mapbd_Reward_Email_Template::add_new_template(
			'BUP',
			$this->__( 'To Customer' ),
			$this->__( 'On Badge Update Point' ),
			'A',
			'Badge Update Notification',
			'points=Redeemed points,total_points=Current Total points,profile_link=Profile URL,view_profile_anchor=Profile Link,customer_name=Customer Name,prev_badge=Previous Badge Title,new_badge=New Badge Title,msg=Message',
			'<p>Hello {{customer_name}},</p><p>Welcome to {{new_badge}}</p><p>You have got <strong>{{points}}</strong> points as your welcome points. Now your total current points are <strong>{{total_points}}</strong> .</p>'
		);
	}

	/**
	 * The data is generated by appsbd
	 */
	public function data() {
		$response = new Ajax_Data_Response();
		$mainobj  = new Mapbd_Reward_Email_Template();
		$records  = $mainobj->count_all();
		if ( $records > 0 ) {
			$data = $mainobj->select_all_grid_data();
			$response->set_grid_data( $data );
		}
		$response->display_grid_response();
	}

	/**
	 * The get template details is generated by appsbd
	 */
	public function template_details() {
		$response     = new Ajax_Confirm_Response();
		$template_key = AppInput::post_value( 'key' );
		if ( empty( $template_key ) ) {
			$this->add_error( 'Invalid request' );
			$response->display_with_response( false );

			return;
		}
		$details = new Mapbd_Reward_Email_Template();
		$details->k_word( $template_key );
		if ( $details->select() ) {
			$response->display_with_response( true, $details->get_details() );
		} else {
			$this->add_error( 'No Email Template found with this key' );
			$response->display_with_response( false, null );
		}
	}


	/**
	 * The get template is generated by appsbd
	 */
	public function update_template() {
		$response = new Ajax_Confirm_Response();
		$key      = AppInput::post_value( 'k_word' );
		if ( empty( $key ) ) {
			$this->add_error( 'Invalid update request' );
			$response->display_with_response( false );

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$uobject = new Mapbd_Reward_Email_Template();
			if ( $uobject->set_from_post_data( false ) ) {
				$props = 'subject,content';
				$uobject->unset_all_excepts( $props );
				$uobject->set_where_update( 'k_word', $key );
				if ( $uobject->update() ) {
					$this->add_info( 'Successfully updated' );
					$response->display_with_response( true );

					return;
				}
			}
		}
		$response->display_with_response( false );
	}

	/**
	 * The change status is generated by appsbd
	 */
	public function change_status() {
		$response = new Ajax_Confirm_Response();
		$key      = AppInput::post_value( 'k_word' );
		if ( empty( $key ) ) {
			$this->add_error( 'Invalid update request' );
			$response->display_with_response( false );

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$uobject = new Mapbd_Reward_Email_Template();
			if ( $uobject->set_from_post_data( false ) ) {
				$props = 'status';
				$uobject->unset_all_excepts( $props );
				$uobject->set_where_update( 'k_word', $key );
				if ( $uobject->update() ) {
					$this->add_info( 'Successfully status changed' );
					$response->display_with_response( true );

					return;
				}
			}
		}
		$response->display_with_response( false );
	}
}
