<?php

namespace SugarCalendarBookings\Appointments\Admin;

use SugarCalendarBookings\Admin\Area;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Admin\AbstractPage;
use SugarCalendarBookings\Support\Helpers\UI;

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

/**
 * Appointments List page.
 *
 * @since 1.0.0
 */
class AppointmentsListPage extends AbstractPage {

	/**
	 * Appointments list table.
	 *
	 * @since 1.0.0
	 *
	 * @var AppointmentsListTable
	 */
	private $list_table;

	/**
	 * Page title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_title() {

		return esc_html__( 'Appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Page slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_slug() {

		return 'appointments';
	}

	/**
	 * Page ID.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_id() {

		return 'appointments-list';
	}

	/**
	 * Hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() {

		add_action( 'admin_init', [ $this, 'init_table' ] );
		add_filter( 'screen_options_show_screen', '__return_false' );
		add_action(
			'sugar_calendar_bookings_admin_area_enqueue_assets_before',
			[ $this, 'enqueue_lib_assets' ]
		);
		add_action(
			'sugar_calendar_bookings_admin_page_subheader_actions_left',
			[ $this, 'display_subheader_actions_left' ]
		);
	}

	/**
	 * Initialize the list table.
	 *
	 * @since 1.0.0
	 * @since 1.2.0 Updated with appointments list table extension.
	 *
	 * @return void
	 */
	public function init_table() {

		$this->list_table = Plugin::get( AppointmentsListTable::class );

		$this->list_table->process_bulk_actions();
	}

	/**
	 * Enqueue lib assets.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_lib_assets() {

		// Required for Service feature image (when creating service from appointment form).
		wp_enqueue_media();

		// Required for WYSIWYG editor (when creating service from appointment form).
		wp_enqueue_editor();
	}

	/**
	 * Display the admin subheader left actions.
	 *
	 * @since 1.0.0
	 */
	public function display_subheader_actions_left() {

		UI::button(
			[
				'text'  => esc_html__( 'Add New', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'size'  => 'sm',
				'class' => 'sugar-calendar-bookings-btn-new-item',
				'link'  => '#',
			]
		);
	}

	/**
	 * Display the page content.
	 *
	 * @since 1.0.0
	 */
	public function display_content() {

		$this->list_table->prepare_items();

		$this->list_table->search_result_notice();
		?>

		<?php $this->list_table->views(); ?>

		<form action="<?php echo esc_url( self::get_url() ); ?>" method="get">
			<input type="hidden" name="page" value="<?php echo esc_attr( Area::SLUG . '-' . self::get_slug() ); ?>" />
			<input type="hidden" name="status" value="<?php echo esc_attr( $this->list_table->get_selected_status() ); ?>" />

			<?php
			$this->list_table->search_box(
				esc_html__( 'Search', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'appointments'
			);
			$this->list_table->display();
			?>
		</form>

		<div id="sugar-calendar-bookings-sidebar"></div>

		<?php UI::confirmation_modal(); ?>

		<?php
	}
}
