<?php
/**
 * Dummy Elite Notification Module
 *
 * @package Elite_Notification_Lite\Modules
 */

namespace Elite_Notification_Lite\Modules;

use Appsbd_Lite\V4\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V4\libs\Ajax_Data_Response;
use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Core\Elite_Notification_Module_Lite;
use Elite_Notification_Lite\Models\Database\Mapbd_Dummy_Notification;

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

/**
 * Class Elite_Dummy_Notification
 *
 * @package Elite_Notification_Lite\Modules
 */
class Elite_Dummy_Notification extends Elite_Notification_Module_Lite {

	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on table create is generated by appsbd
	 */
	public function on_table_create() {
		Mapbd_Dummy_Notification::create_db_table();
	}

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

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

	/**
	 * The add is generated by appsbd
	 */
	public function add() {
		$response = new Ajax_Confirm_Response();
		if ( APPSBD_IS_POST_BACK ) {
			$main_object = new Mapbd_Dummy_Notification();
			if ( $main_object->set_from_post_data( true ) ) {
				if ( $main_object->save() ) {
					$this->add_info( 'Successfully added' );
					$response->display_with_response( true );

					return;
				} else {
					$this->add_error( 'Failed to save' );
					$response->display_with_response( false );

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

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

			return;
		}
		$details = new Mapbd_Dummy_Notification();
		$details->id( $notification_id );
		if ( $details->select() ) {
			$details->img_url = wp_get_attachment_image_url( $details->img_id );
			if ( empty( $details->img_url ) ) {
				$details->img_url = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNzguNzUgMTc4Ljc1Ij48ZGVmcz48c3R5bGU+LmNscy0xe2ZpbGw6IzAwN2NmNTt9LmNscy0ye2ZpbGw6IzZhMGZhYTt9LmNscy0ze2ZpbGw6IzFiZTA3NDt9LmNscy00e2ZpbGw6bm9uZTt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPnNob3J0LWxvZ288L3RpdGxlPjxnIGlkPSJMYXllcl8yIiBkYXRhLW5hbWU9IkxheWVyIDIiPjxnIGlkPSJMYXllcl8xLTIiIGRhdGEtbmFtZT0iTGF5ZXIgMSI+PHBhdGggY2xhc3M9ImNscy0xIiBkPSJNODcuMTksOTMuMjUsNTkuNjUsMTQxYTMuMjcsMy4yNywwLDAsMS0yLjgyLDEuNjNIMzNhMy4yNiwzLjI2LDAsMCwxLTIuODItNC44OEw1Ny43Nyw5MGEzLjI1LDMuMjUsMCwwLDEsMi44MS0xLjYySDg0LjM3QTMuMjUsMy4yNSwwLDAsMSw4Ny4xOSw5My4yNVoiLz48cGF0aCBjbGFzcz0iY2xzLTIiIGQ9Ik0xMjAuMzgsOTBsLTExLjg5LDIwLjY0YTMuMjYsMy4yNiwwLDAsMS01LjY0LDBMNzUuMyw2Mi44OGEzLjIyLDMuMjIsMCwwLDEsMC0zLjI2TDg3LjIsMzlhMy4yNSwzLjI1LDAsMCwxLDUuNjMsMGwyNy41NSw0Ny43M0EzLjI1LDMuMjUsMCwwLDEsMTIwLjM4LDkwWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTE0NywxNDIuNjFIOTEuODZBMy4yNywzLjI3LDAsMCwxLDg5LDE0MWwtMTEuODktMjAuNkEzLjI2LDMuMjYsMCwwLDEsODAsMTE1LjUxaDU1LjFhMy4yNSwzLjI1LDAsMCwxLDIuODIsMS42M2wxMS44OSwyMC42QTMuMjUsMy4yNSwwLDAsMSwxNDcsMTQyLjYxWiIvPjxyZWN0IGNsYXNzPSJjbHMtNCIgd2lkdGg9IjE3OC43NSIgaGVpZ2h0PSIxNzguNzUiLz48L2c+PC9nPjwvc3ZnPg==';
			}
			$response->display_with_response( true, $details );
		} else {
			$this->add_error( 'No notification found with this id' );
			$response->display_with_response( false, null );
		}
	}

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

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$main_obj = new Mapbd_Dummy_Notification();
			if ( $main_obj->set_from_post_data( false ) ) {
				$props = 'title,img_id,img_url,body,type,status,view_count,click_count,nt_by,op_date,link_url,slnf_style';
				$main_obj->unset_all_excepts( $props );
				$main_obj->set_where_update( 'id', $notification_id );
				if ( $main_obj->update() ) {
					$this->add_info( 'Successfully updated' );
					$response->display_with_response( true );

					return;
				} else {
					$this->add_error( 'Failed to update' );
					$response->display_with_response( false );
				}
			}
		}
		$response->display_with_response( false );
	}

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

			return;
		}
		$main_object = new Mapbd_Dummy_Notification();
		$main_object->id( $id );
		if ( $main_object->select() ) {
			if ( $main_object->delete_by_id( $id ) ) {
				$this->add_info( 'Successfully deleted' );
				$response->display_with_response( true );
			} else {
				$this->add_error( 'Failed to delete' );
				$response->display_with_response( false );
			}
		}
	}

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

			return;
		}
		if ( APPSBD_IS_POST_BACK ) {
			$main_obj = new Mapbd_Dummy_Notification();
			if ( $main_obj->set_from_post_data( false ) ) {
				$props = 'status';
				$main_obj->unset_all_excepts( $props );
				$main_obj->set_where_update( 'id', $notification_id );
				if ( $main_obj->update() ) {
					$this->add_info( 'Successfully updated' );
					$response->display_with_response( true );

					return;
				} else {
					$this->add_error( 'Failed to update' );
					$response->display_with_response( false );
				}
			}
		}
		$response->display_with_response( false );
	}
}
