<?php
/** Administrative screens and privileged actions. */
namespace PlugozaEventTicketing\Admin;

use PlugozaEventTicketing\Payment_Gateways;
use PlugozaEventTicketing\Repository;

defined( 'ABSPATH' ) || exit;

final class Admin {
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
		add_action( 'admin_post_plugoza_ett_save_event', array( $this, 'save_event' ) );
		add_action( 'admin_post_plugoza_ett_save_ticket', array( $this, 'save_ticket' ) );
		add_action( 'admin_post_plugoza_ett_update_ticket', array( $this, 'update_ticket' ) );
		add_action( 'admin_post_plugoza_ett_delete_ticket', array( $this, 'delete_ticket' ) );
		add_action( 'admin_post_plugoza_ett_toggle_booking', array( $this, 'toggle_booking' ) );
		add_action( 'admin_post_plugoza_ett_settings', array( $this, 'settings_save' ) );
		add_action( 'admin_post_plugoza_ett_booking_page', array( $this, 'booking_page_save' ) );
		add_action( 'admin_post_plugoza_ett_mark_paid', array( $this, 'mark_paid' ) );
	}

	public function menu() {
		$cap = 'manage_options';
		add_menu_page( __( 'Event Booking', 'plugoza-event-ticketing' ), __( 'Event Booking', 'plugoza-event-ticketing' ), $cap, 'plugoza-ett-dashboard', array( $this, 'dashboard' ), 'dashicons-tickets-alt', 26 );
		$pages = array(
			'events' => __( 'Events', 'plugoza-event-ticketing' ),
			'orders' => __( 'Orders', 'plugoza-event-ticketing' ),
			'manual-pay' => __( 'Payment Gateways', 'plugoza-event-ticketing' ),
			'shortcode-reference' => __( 'Shortcodes', 'plugoza-event-ticketing' ),
			'booking-page' => __( 'Booking Page', 'plugoza-event-ticketing' ),
			'settings' => __( 'Settings', 'plugoza-event-ticketing' ),
			'ticket-manager' => __( 'Tickets', 'plugoza-event-ticketing' ),
			'customer-list' => __( 'Customers', 'plugoza-event-ticketing' ),
		);
		foreach ( $pages as $slug => $title ) {
			add_submenu_page( 'plugoza-ett-dashboard', $title, $title, $cap, 'plugoza-ett-' . $slug, array( $this, str_replace( '-', '_', $slug ) ) );
		}
	}

	public function assets( $hook ) {
		if ( false === strpos( $hook, 'plugoza-ett-' ) ) {
			return;
		}

		wp_enqueue_style(
			'plugoza-ett-admin',
			PLUGOZA_ETT_URL . 'assets/css/admin.css',
			array(),
			PLUGOZA_ETT_VERSION
		);
		wp_enqueue_script(
			'plugoza-ett-admin',
			PLUGOZA_ETT_URL . 'assets/js/admin.js',
			array(),
			PLUGOZA_ETT_VERSION,
			true
		);
		wp_localize_script(
			'plugoza-ett-admin',
			'plugozaEttAdmin',
			array(
				'copied' => __( 'Copied!', 'plugoza-event-ticketing' ),
			)
		);
	}

	private function guard( $action ) {
		if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'plugoza-event-ticketing' ) ); }
		check_admin_referer( $action );
	}
	private function page_start( $title ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You are not allowed to access this page.', 'plugoza-event-ticketing' ) );
		}

		echo '<div class="wrap"><h1>' . esc_html( $title ) . '</h1>';
	}
	private function shortcode_field( $event_id, $context = 'event' ) {
		$id = 'plugoza-ett-shortcode-' . absint( $event_id ) . '-' . sanitize_key( $context );
		$code = '[plugoza_ett_event_ticket id="' . absint( $event_id ) . '"]';
		echo '<div class="plugoza-ett-shortcode"><input id="' . esc_attr( $id ) . '" class="regular-text" type="text" readonly value="' . esc_attr( $code ) . '"><button type="button" class="button plugoza-ett-copy-shortcode" data-copy-target="' . esc_attr( $id ) . '">' . esc_html__( 'Copy', 'plugoza-event-ticketing' ) . '</button></div>';
	}

	public function dashboard() {
		global $wpdb;
		$this->page_start( __( 'Event Booking Dashboard', 'plugoza-event-ticketing' ) );
		$event_table   = Repository::table( 'events' );
		$booking_table = Repository::table( 'bookings' );
		$stats = array(
			'events'  => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status = %s', $event_table, 'published' ) ),
			'orders'  => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $booking_table ) ),
			'active'  => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE booking_status = %s', $booking_table, 'confirmed' ) ),
			'entries' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COALESCE(SUM(quantity),0) FROM %i WHERE booking_status = %s', $booking_table, 'confirmed' ) ),
		);
		$latest = $wpdb->get_results(
			$wpdb->prepare(
				'SELECT b.booking_number, b.booking_date, b.quantity, b.amount, b.booking_status, e.name AS event_name, c.first_name, c.last_name FROM %i b LEFT JOIN %i e ON e.id = b.event_id LEFT JOIN %i c ON c.id = b.customer_id ORDER BY b.booking_date DESC LIMIT 5',
				Repository::table( 'bookings' ),
				Repository::table( 'events' ),
				Repository::table( 'customers' )
			)
		);
		?>
		<div class="plugoza-ett-grid">
			<?php foreach ( array( 'events' => __( 'Published events', 'plugoza-event-ticketing' ), 'orders' => __( 'Total orders', 'plugoza-event-ticketing' ), 'active' => __( 'Active tickets', 'plugoza-event-ticketing' ), 'entries' => __( 'Confirmed entries', 'plugoza-event-ticketing' ) ) as $key => $label ) : ?>
				<section class="plugoza-ett-card plugoza-ett-stat"><span class="plugoza-ett-description"><?php echo esc_html( $label ); ?></span><div class="plugoza-ett-stat__number"><?php echo esc_html( number_format_i18n( $stats[ $key ] ) ); ?></div></section>
			<?php endforeach; ?>
		</div>
		<section class="plugoza-ett-card" style="max-width:1200px;margin-top:20px"><h2><?php esc_html_e( 'Latest bookings', 'plugoza-event-ticketing' ); ?></h2><table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Order ID', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Booking date', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Guest', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Entries', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Status', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody><?php if ( $latest ) : foreach ( $latest as $booking ) : ?><tr><td><?php echo esc_html( $booking->booking_number ); ?></td><td><?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $booking->booking_date ) ) ); ?></td><td><?php echo esc_html( trim( $booking->first_name . ' ' . $booking->last_name ) ); ?></td><td><?php echo esc_html( $booking->event_name ); ?></td><td><?php echo esc_html( number_format_i18n( $booking->quantity ) ); ?></td><td><span class="plugoza-ett-status"><?php echo esc_html( $booking->booking_status ); ?></span></td></tr><?php endforeach; else : ?><tr><td colspan="6"><?php esc_html_e( 'No bookings yet.', 'plugoza-event-ticketing' ); ?></td></tr><?php endif; ?></tbody></table></section>
		</div>
		<?php
	}
	public function events() {
		$this->page_start( __( 'Events', 'plugoza-event-ticketing' ) );
		$events = Repository::events();
		echo '<p><a class="button button-primary" href="#plugoza-ett-event-form">' . esc_html__( 'Add event', 'plugoza-event-ticketing' ) . '</a></p><table class="widefat striped"><thead><tr><th>' . esc_html__( 'Name', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Date', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Status', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Shortcode', 'plugoza-event-ticketing' ) . '</th></tr></thead><tbody>';
		foreach ( $events as $event ) { echo '<tr><td><a href="' . esc_url( add_query_arg( array( 'page' => 'plugoza-ett-events', 'event_id' => $event->id, 'plugoza_ett_event_nonce' => wp_create_nonce( 'plugoza_ett_edit_event_' . $event->id ) ), admin_url( 'admin.php' ) ) ) . '">' . esc_html( $event->name ) . '</a></td><td>' . esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $event->start_date ) ) ) . '</td><td>' . esc_html( $event->status ) . '</td><td>'; $this->shortcode_field( $event->id, 'list' ); echo '</td></tr>'; }
		echo '</tbody></table>';
		$event_id    = absint( filter_input( INPUT_GET, 'event_id', FILTER_SANITIZE_NUMBER_INT ) );
		$event_nonce = sanitize_text_field( wp_unslash( $_GET['plugoza_ett_event_nonce'] ?? '' ) );
		$event       = $event_id && wp_verify_nonce( $event_nonce, 'plugoza_ett_edit_event_' . $event_id ) ? Repository::event( $event_id ) : null;
		$this->event_form( $event );
		echo '</div>';
	}
	private function event_form( $event = null ) {
		$is_edit = ! empty( $event );
		$methods = $event ? Payment_Gateways::event_methods( $event ) : array();
		$settings = Payment_Gateways::settings();
		$legal_links = $event ? Payment_Gateways::event_legal_links( $event->id ) : array( 'terms' => '', 'privacy' => '', 'refund' => '' );
		$gateway_labels = array(
			'bank_transfer'     => __( 'Manual Bank Transfer', 'plugoza-event-ticketing' ),
			'free_registration' => __( 'Manual Pay', 'plugoza-event-ticketing' ),
		);
		$enabled_gateways = array_filter( $gateway_labels, static function ( $key ) use ( $settings ) { return ! empty( $settings[ $key ]['enabled'] ); }, ARRAY_FILTER_USE_KEY ); ?>
<div class="plugoza-ett-card" id="plugoza-ett-event-form"><h2><?php echo esc_html( $is_edit ? __( 'Edit event', 'plugoza-event-ticketing' ) : __( 'New event', 'plugoza-event-ticketing' ) ); ?></h2>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_save_event"><input type="hidden" name="event_id" value="<?php echo esc_attr( $event->id ?? 0 ); ?>"><?php wp_nonce_field( 'plugoza_ett_save_event' ); ?><p><label for="plugoza-ett-description"><strong><?php esc_html_e( 'Event description', 'plugoza-event-ticketing' ); ?></strong></label><br><textarea class="large-text" rows="5" id="plugoza-ett-description" name="description"><?php echo esc_textarea( $event->description ?? '' ); ?></textarea></p>
<table class="form-table" role="presentation"><tr><th><label for="plugoza_ett_name"><?php esc_html_e( 'Event name', 'plugoza-event-ticketing' ); ?></label></th><td><input required class="regular-text" id="plugoza_ett_name" name="name" value="<?php echo esc_attr( $event->name ?? '' ); ?>"></td></tr><tr><th><label for="plugoza_ett_start"><?php esc_html_e( 'Start date', 'plugoza-event-ticketing' ); ?></label></th><td><input required type="datetime-local" id="plugoza_ett_start" name="start_date" value="<?php echo esc_attr( $event ? gmdate( 'Y-m-d\TH:i', strtotime( $event->start_date ) ) : '' ); ?>"></td></tr><tr><th><label for="plugoza_ett_end"><?php esc_html_e( 'End date', 'plugoza-event-ticketing' ); ?></label></th><td><input required type="datetime-local" id="plugoza_ett_end" name="end_date" value="<?php echo esc_attr( $event ? gmdate( 'Y-m-d\TH:i', strtotime( $event->end_date ) ) : '' ); ?>"></td></tr><tr><th><label for="plugoza_ett_type"><?php esc_html_e( 'Event type', 'plugoza-event-ticketing' ); ?></label></th><td><select id="plugoza_ett_type" name="event_type"><?php foreach ( array( 'physical' => __( 'Physical', 'plugoza-event-ticketing' ), 'online' => __( 'Online', 'plugoza-event-ticketing' ), 'hybrid' => __( 'Hybrid', 'plugoza-event-ticketing' ) ) as $type => $label ) { echo '<option value="' . esc_attr( $type ) . '" ' . selected( $event->event_type ?? 'physical', $type, false ) . '>' . esc_html( $label ) . '</option>'; } ?></select></td></tr><tr><th><label for="plugoza_ett_venue"><?php esc_html_e( 'Venue', 'plugoza-event-ticketing' ); ?></label></th><td><input class="regular-text" id="plugoza_ett_venue" name="venue" value="<?php echo esc_attr( $event->venue ?? '' ); ?>"></td></tr><tr><th><label for="plugoza_ett_status"><?php esc_html_e( 'Status', 'plugoza-event-ticketing' ); ?></label></th><td><select id="plugoza_ett_status" name="status"><option value="draft" <?php selected( $event->status ?? 'draft', 'draft' ); ?>><?php esc_html_e( 'Draft', 'plugoza-event-ticketing' ); ?></option><option value="published" <?php selected( $event->status ?? '', 'published' ); ?>><?php esc_html_e( 'Published', 'plugoza-event-ticketing' ); ?></option></select></td></tr><tr><th><?php esc_html_e( 'Payment methods', 'plugoza-event-ticketing' ); ?></th><td class="plugoza-ett-payment-methods"><?php if ( $enabled_gateways ) : ?><?php foreach ( $enabled_gateways as $gateway => $label ) : ?><label><input type="checkbox" name="payment_methods[]" value="<?php echo esc_attr( $gateway ); ?>" <?php checked( in_array( $gateway, $methods, true ) ); ?>> <?php echo esc_html( $label ); ?></label><?php endforeach; ?><?php else : ?><p class="description"><?php esc_html_e( 'Enable a payment gateway first under Event Booking > Payment Gateways.', 'plugoza-event-ticketing' ); ?></p><?php endif; ?></td></tr><tr><th><?php esc_html_e( 'Event policy links', 'plugoza-event-ticketing' ); ?></th><td><p><label for="plugoza-ett-terms-url"><?php esc_html_e( 'Terms & Conditions URL', 'plugoza-event-ticketing' ); ?></label><br><input type="url" class="regular-text" id="plugoza-ett-terms-url" name="legal_links[terms]" value="<?php echo esc_attr( $legal_links['terms'] ); ?>"></p><p><label for="plugoza-ett-privacy-url"><?php esc_html_e( 'Privacy Policy URL', 'plugoza-event-ticketing' ); ?></label><br><input type="url" class="regular-text" id="plugoza-ett-privacy-url" name="legal_links[privacy]" value="<?php echo esc_attr( $legal_links['privacy'] ); ?>"></p><p><label for="plugoza-ett-refund-url"><?php esc_html_e( 'Refund Policy URL', 'plugoza-event-ticketing' ); ?></label><br><input type="url" class="regular-text" id="plugoza-ett-refund-url" name="legal_links[refund]" value="<?php echo esc_attr( $legal_links['refund'] ); ?>"></p></td></tr></table><?php submit_button( __( 'Save event', 'plugoza-event-ticketing' ) ); ?></form></div>
<?php if ( $is_edit ) : ?><div class="plugoza-ett-card"><h2><?php esc_html_e( 'Event Shortcode', 'plugoza-event-ticketing' ); ?></h2><?php $this->shortcode_field( $event->id, 'edit' ); ?><p class="plugoza-ett-description"><?php esc_html_e( 'Use this shortcode on any page or post to display this event booking form.', 'plugoza-event-ticketing' ); ?></p></div><?php endif; }
	public function save_event() {
		$this->guard( 'plugoza_ett_save_event' ); global $wpdb;
		$id = absint( $_POST['event_id'] ?? 0 ); $existing = $id ? Repository::event( $id ) : null;
		$name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ); $start = sanitize_text_field( wp_unslash( $_POST['start_date'] ?? '' ) ); $end = sanitize_text_field( wp_unslash( $_POST['end_date'] ?? '' ) );
		if ( ! $name || ! strtotime( $start ) || ! strtotime( $end ) || strtotime( $end ) <= strtotime( $start ) ) { wp_safe_redirect( add_query_arg( 'plugoza_ett_error', 'event', admin_url( 'admin.php?page=plugoza-ett-events' ) ) ); exit; }
		$requested = isset( $_POST['payment_methods'] ) && is_array( $_POST['payment_methods'] ) ? array_map( 'sanitize_key', wp_unslash( $_POST['payment_methods'] ) ) : array();
		if ( empty( $requested ) && ! empty( Payment_Gateways::settings()['free_registration']['enabled'] ) ) {
			$requested = array( 'free_registration' );
		}
		$event_type = sanitize_key( wp_unslash( $_POST['event_type'] ?? 'physical' ) );
		$status = sanitize_key( wp_unslash( $_POST['status'] ?? 'draft' ) );
		$data = array( 'name' => $name, 'event_type' => in_array( $event_type, array( 'physical', 'online', 'hybrid' ), true ) ? $event_type : 'physical', 'venue' => sanitize_text_field( wp_unslash( $_POST['venue'] ?? '' ) ), 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( $start ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( $end ) ), 'timezone' => wp_timezone_string(), 'status' => in_array( $status, array( 'draft', 'published' ), true ) ? $status : 'draft', 'updated_at' => current_time( 'mysql', true ) );
		$data['description'] = wp_kses_post( wp_unslash( $_POST['description'] ?? '' ) );
		if ( $existing ) {
			$result = $wpdb->update( Repository::table( 'events' ), $data, array( 'id' => $existing->id ) );
		} else {
			$data['slug']       = $this->unique_event_slug( $name );
			$data['banner_url'] = '';
			$data['address']    = '';
			$data['city']       = '';
			$data['country']    = '';
			$data['meeting_url'] = '';
			$data['created_at'] = current_time( 'mysql', true );
			$result = $wpdb->insert( Repository::table( 'events' ), $data );
		}
		if ( false === $result ) {
			wp_die( esc_html__( 'The event could not be saved. Please try again.', 'plugoza-event-ticketing' ) );
		}
		$event_id    = $existing ? $existing->id : $wpdb->insert_id;
		$legal_input = isset( $_POST['legal_links'] ) && is_array( $_POST['legal_links'] ) ? wp_unslash( $_POST['legal_links'] ) : array();
		$legal_links = array(
			'terms'   => esc_url_raw( $legal_input['terms'] ?? '' ),
			'privacy' => esc_url_raw( $legal_input['privacy'] ?? '' ),
			'refund'  => esc_url_raw( $legal_input['refund'] ?? '' ),
		);
		Payment_Gateways::save_event_methods( $event_id, $requested );
		Payment_Gateways::save_event_legal_links( $event_id, $legal_links );
		wp_safe_redirect( admin_url( 'admin.php?page=plugoza-ett-events' ) ); exit;
	}
	private function unique_event_slug( $name ) {
		global $wpdb;
		$base = sanitize_title( $name );
		$base = $base ? $base : 'event';
		$slug = $base;
		$index = 2;
		while ( $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE slug = %s LIMIT 1', Repository::table( 'events' ), $slug ) ) ) {
			$slug = $base . '-' . $index;
			++$index;
		}
		return $slug;
	}
	public function payment_gateways() { $this->page_start( __( 'Payment Gateways', 'plugoza-event-ticketing' ) ); $settings = Payment_Gateways::settings(); ?>
<form method="post" action="options.php"><?php settings_fields( 'plugoza_ett_payment_gateways' ); ?><div class="plugoza-ett-grid"><section class="plugoza-ett-card"><h2><span class="dashicons dashicons-bank"></span> <?php esc_html_e( 'Manual Bank Transfer', 'plugoza-event-ticketing' ); ?></h2><p><label><input type="checkbox" name="plugoza_ett_payment_gateways[bank_transfer][enabled]" value="1" <?php checked( ! empty( $settings['bank_transfer']['enabled'] ) ); ?>> <?php esc_html_e( 'Enable', 'plugoza-event-ticketing' ); ?></label></p><?php foreach ( array( 'bank_name' => __( 'Bank Name', 'plugoza-event-ticketing' ), 'account_name' => __( 'Account Name', 'plugoza-event-ticketing' ), 'account_number' => __( 'Account Number', 'plugoza-event-ticketing' ), 'ifsc_swift' => __( 'IFSC / SWIFT', 'plugoza-event-ticketing' ), 'branch' => __( 'Branch', 'plugoza-event-ticketing' ), 'upi_id' => __( 'UPI ID', 'plugoza-event-ticketing' ) ) as $key => $label ) : ?><p><label><?php echo esc_html( $label ); ?><br><input class="regular-text" name="plugoza_ett_payment_gateways[bank_transfer][<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( $settings['bank_transfer'][ $key ] ?? '' ); ?>"></label></p><?php endforeach; ?><p><label><?php esc_html_e( 'Instructions', 'plugoza-event-ticketing' ); ?><br><textarea class="large-text" rows="4" name="plugoza_ett_payment_gateways[bank_transfer][instructions]"><?php echo esc_textarea( $settings['bank_transfer']['instructions'] ?? '' ); ?></textarea></label></p></section><section class="plugoza-ett-card"><h2><span class="dashicons dashicons-tickets-alt"></span> <?php esc_html_e( 'Free Registration', 'plugoza-event-ticketing' ); ?></h2><p><label><input type="checkbox" name="plugoza_ett_payment_gateways[free_registration][enabled]" value="1" <?php checked( ! empty( $settings['free_registration']['enabled'] ) ); ?>> <?php esc_html_e( 'Enable', 'plugoza-event-ticketing' ); ?></label></p><p class="plugoza-ett-description"><?php esc_html_e( 'When enabled, users can register without making any payment.', 'plugoza-event-ticketing' ); ?></p></section></div><?php submit_button( __( 'Save Settings', 'plugoza-event-ticketing' ) ); ?></form></div><?php }
	public function tickets() {
		$this->page_start( __( 'Tickets', 'plugoza-event-ticketing' ) );
		$events = Repository::events();
		$filter_nonce = sanitize_text_field( wp_unslash( $_GET['plugoza_ett_filter_nonce'] ?? '' ) );
		$valid_filter = wp_verify_nonce( $filter_nonce, 'plugoza_ett_ticket_filter' );
		if ( $valid_filter && 'ticket_saved' === sanitize_key( wp_unslash( $_GET['plugoza_ett_notice'] ?? '' ) ) ) {
			echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Ticket added successfully.', 'plugoza-event-ticketing' ) . '</p></div>';
		}
		if ( ! $events ) {
			echo '<div class="notice notice-warning"><p>' . esc_html__( 'Create an event before adding tickets.', 'plugoza-event-ticketing' ) . '</p></div></div>';
			return;
		}
		global $wpdb;
		$selected_event_id = $valid_filter ? absint( $_GET['event_id'] ?? 0 ) : 0;
		if ( $selected_event_id ) { $tickets = $wpdb->get_results( $wpdb->prepare( 'SELECT t.id, t.name, t.price, t.quantity, t.available_quantity, t.status, e.name AS event_name FROM %i t INNER JOIN %i e ON e.id = t.event_id WHERE t.event_id = %d ORDER BY t.created_at DESC', Repository::table( 'tickets' ), Repository::table( 'events' ), $selected_event_id ) ); } else { $tickets = $wpdb->get_results( $wpdb->prepare( 'SELECT t.id, t.name, t.price, t.quantity, t.available_quantity, t.status, e.name AS event_name FROM %i t INNER JOIN %i e ON e.id = t.event_id ORDER BY t.created_at DESC', Repository::table( 'tickets' ), Repository::table( 'events' ) ) ); }
		$currency = sanitize_text_field( get_option( 'plugoza_ett_settings', array() )['currency'] ?? 'USD' );
		?>
<div class="plugoza-ett-card"><h2><?php esc_html_e( 'Add ticket', 'plugoza-event-ticketing' ); ?></h2><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_save_ticket"><?php wp_nonce_field( 'plugoza_ett_save_ticket' ); ?><table class="form-table" role="presentation"><tr><th><label for="plugoza-ett-ticket-event"><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?></label></th><td><select required id="plugoza-ett-ticket-event" name="event_id"><?php foreach ( $events as $event ) { echo '<option value="' . esc_attr( $event->id ) . '">' . esc_html( $event->name ) . '</option>'; } ?></select></td></tr><tr><th><label for="plugoza-ett-ticket-name"><?php esc_html_e( 'Ticket name', 'plugoza-event-ticketing' ); ?></label></th><td><input required id="plugoza-ett-ticket-name" class="regular-text" name="name"></td></tr><tr><th><label for="plugoza-ett-ticket-price"><?php esc_html_e( 'Price', 'plugoza-event-ticketing' ); ?></label></th><td><input required id="plugoza-ett-ticket-price" min="0" step="0.01" type="number" name="price"> <span><?php echo esc_html( $currency ); ?></span></td></tr><tr><th><label for="plugoza-ett-ticket-quantity"><?php esc_html_e( 'Quantity', 'plugoza-event-ticketing' ); ?></label></th><td><input required id="plugoza-ett-ticket-quantity" min="1" type="number" name="quantity"></td></tr></table><?php submit_button( __( 'Add ticket', 'plugoza-event-ticketing' ) ); ?></form></div>
<h2><?php esc_html_e( 'Existing tickets', 'plugoza-event-ticketing' ); ?></h2><form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>"><input type="hidden" name="page" value="plugoza-ett-tickets"><?php wp_nonce_field( 'plugoza_ett_ticket_filter', 'plugoza_ett_filter_nonce' ); ?><label for="plugoza-ett-ticket-filter" class="screen-reader-text"><?php esc_html_e( 'Filter by event', 'plugoza-event-ticketing' ); ?></label><select id="plugoza-ett-ticket-filter" name="event_id"><option value="0"><?php esc_html_e( 'All events', 'plugoza-event-ticketing' ); ?></option><?php foreach ( $events as $event ) { echo '<option value="' . esc_attr( $event->id ) . '" ' . selected( $selected_event_id, $event->id, false ) . '>' . esc_html( $event->name ) . '</option>'; } ?></select><?php submit_button( __( 'Filter', 'plugoza-event-ticketing' ), 'secondary', 'filter_action', false ); ?></form><table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Ticket', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Price', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Available', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Status', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody><?php if ( $tickets ) : ?><?php foreach ( $tickets as $ticket ) : ?><tr><td><?php echo esc_html( $ticket->name ); ?></td><td><?php echo esc_html( $ticket->event_name ); ?></td><td><?php echo esc_html( $ticket->price . ' ' . $currency ); ?></td><td><?php echo esc_html( $ticket->available_quantity . ' / ' . $ticket->quantity ); ?></td><td><?php echo esc_html( $ticket->status ); ?></td></tr><?php endforeach; ?><?php else : ?><tr><td colspan="5"><?php esc_html_e( 'No tickets have been created yet.', 'plugoza-event-ticketing' ); ?></td></tr><?php endif; ?></tbody></table></div><?php
	}
	public function save_ticket() {
		$this->guard( 'plugoza_ett_save_ticket' ); $event = Repository::event( absint( $_POST['event_id'] ?? 0 ) ); $name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ); $quantity = absint( $_POST['quantity'] ?? 0 ); $price = number_format( max( 0, (float) sanitize_text_field( wp_unslash( $_POST['price'] ?? '' ) ) ), 2, '.', '' );
		if ( ! $event || ! $name || ! $quantity ) { wp_die( esc_html__( 'Invalid ticket data.', 'plugoza-event-ticketing' ) ); }
		global $wpdb;
		$result = $wpdb->insert( Repository::table( 'tickets' ), array( 'event_id' => $event->id, 'name' => $name, 'price' => $price, 'quantity' => $quantity, 'available_quantity' => $quantity, 'status' => 'active', 'created_at' => current_time( 'mysql', true ) ), array( '%d', '%s', '%f', '%d', '%d', '%s', '%s' ) );
		if ( false === $result ) {
			wp_die( esc_html__( 'The ticket could not be saved. Please try again.', 'plugoza-event-ticketing' ) );
		}
		wp_safe_redirect( add_query_arg( 'plugoza_ett_notice', 'ticket_saved', admin_url( 'admin.php?page=plugoza-ett-ticket-manager' ) ) ); exit;
	}
	public function customers() { $this->page_start( __( 'Customers', 'plugoza-event-ticketing' ) ); echo '<p>' . esc_html__( 'Customer records are created from bookings.', 'plugoza-event-ticketing' ) . '</p></div>'; }
	public function organizers() { $this->page_start( __( 'Organizers', 'plugoza-event-ticketing' ) ); echo '<p>' . esc_html__( 'Organizer records are ready for future management screens.', 'plugoza-event-ticketing' ) . '</p></div>'; }
	public function checkin() { $this->page_start( __( 'Check-in', 'plugoza-event-ticketing' ) ); ?><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_checkin"><?php wp_nonce_field( 'plugoza_ett_checkin' ); ?><label for="plugoza-ett-token"><?php esc_html_e( 'QR token', 'plugoza-event-ticketing' ); ?></label> <input required id="plugoza-ett-token" name="token" autocomplete="off"><?php submit_button( __( 'Check in', 'plugoza-event-ticketing' ), 'primary', 'submit', false ); ?></form></div><?php }
	public function checkin_submit() { $this->guard( 'plugoza_ett_checkin' ); global $wpdb; $token = sanitize_text_field( wp_unslash( $_POST['token'] ?? '' ) ); $booking = $wpdb->get_row( $wpdb->prepare( 'SELECT id FROM %i WHERE qr_code = %s', Repository::table( 'bookings' ), $token ) ); if ( $booking ) { $wpdb->replace( Repository::table( 'checkins' ), array( 'booking_id' => $booking->id, 'checked_in' => 1, 'checked_in_at' => current_time( 'mysql', true ), 'user_id' => get_current_user_id() ) ); } wp_safe_redirect( admin_url( 'admin.php?page=plugoza-ett-checkin' ) ); exit; }
	public function bookings() { global $wpdb; $this->page_start( __( 'Orders', 'plugoza-event-ticketing' ) ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT b.id, b.booking_number, b.booking_status, b.payment_status, b.payment_method, e.name event_name, c.email FROM %i b LEFT JOIN %i e ON b.event_id = e.id LEFT JOIN %i c ON b.customer_id = c.id ORDER BY b.booking_date DESC LIMIT 100', Repository::table( 'bookings' ), Repository::table( 'events' ), Repository::table( 'customers' ) ) ); echo '<table class="widefat striped"><thead><tr><th>' . esc_html__( 'Booking', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Event', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Customer', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Status', 'plugoza-event-ticketing' ) . '</th></tr></thead><tbody>'; foreach ( $rows as $row ) { echo '<tr><td>' . esc_html( $row->booking_number ) . '</td><td>' . esc_html( $row->event_name ) . '</td><td>' . esc_html( $row->email ) . '</td><td><span class="plugoza-ett-status">' . esc_html( $row->payment_status ) . '</span>'; if ( 'bank_transfer' === $row->payment_method && 'paid' !== $row->payment_status ) { echo ' <form style="display:inline" method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '"><input type="hidden" name="action" value="plugoza_ett_mark_paid"><input type="hidden" name="booking_id" value="' . esc_attr( $row->id ) . '">'; wp_nonce_field( 'plugoza_ett_mark_paid_' . $row->id ); submit_button( __( 'Mark paid', 'plugoza-event-ticketing' ), 'secondary small', 'submit', false ); echo '</form>'; } echo '</td></tr>'; } echo '</tbody></table></div>'; }
	public function mark_paid() { $id = absint( $_POST['booking_id'] ?? 0 ); $this->guard( 'plugoza_ett_mark_paid_' . $id ); global $wpdb; $wpdb->update( Repository::table( 'bookings' ), array( 'payment_status' => 'paid', 'booking_status' => 'confirmed' ), array( 'id' => $id ), array( '%s', '%s' ), array( '%d' ) ); wp_safe_redirect( wp_get_referer() ?: admin_url( 'admin.php?page=plugoza-ett-orders' ) ); exit; }
	public function settings() { $this->page_start( __( 'Settings', 'plugoza-event-ticketing' ) ); $settings = get_option( 'plugoza_ett_settings', array() ); ?><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_settings"><?php wp_nonce_field( 'plugoza_ett_settings' ); ?><p><label for="plugoza-ett-currency"><?php esc_html_e( 'Currency code', 'plugoza-event-ticketing' ); ?></label> <input id="plugoza-ett-currency" maxlength="3" name="currency" value="<?php echo esc_attr( $settings['currency'] ?? 'USD' ); ?>"></p><p><label><input type="checkbox" name="remove_data" value="1" <?php checked( get_option( 'plugoza_ett_remove_data_on_uninstall' ) ); ?>> <?php esc_html_e( 'Remove all data when the plugin is deleted.', 'plugoza-event-ticketing' ); ?></label></p><?php submit_button(); ?></form></div><?php }
	public function shortcodes() { $this->page_start( __( 'Shortcodes', 'plugoza-event-ticketing' ) ); echo '<table class="widefat striped"><thead><tr><th>' . esc_html__( 'Shortcode', 'plugoza-event-ticketing' ) . '</th><th>' . esc_html__( 'Purpose', 'plugoza-event-ticketing' ) . '</th></tr></thead><tbody><tr><td><code>[plugoza_ett_event_list]</code></td><td>' . esc_html__( 'Displays published events as cards with Book now buttons.', 'plugoza-event-ticketing' ) . '</td></tr><tr><td><code>[plugoza_ett_event_ticket id="1"]</code></td><td>' . esc_html__( 'Displays the booking form for one event.', 'plugoza-event-ticketing' ) . '</td></tr><tr><td><code>[plugoza_ett_event_booking event_id="1"]</code></td><td>' . esc_html__( 'Alternative booking form shortcode for one event.', 'plugoza-event-ticketing' ) . '</td></tr><tr><td><code>[plugoza_ett_event_details id="1"]</code></td><td>' . esc_html__( 'Displays event information and description.', 'plugoza-event-ticketing' ) . '</td></tr><tr><td><code>[plugoza_ett_my_tickets]</code></td><td>' . esc_html__( 'Displays tickets for the current customer.', 'plugoza-event-ticketing' ) . '</td></tr></tbody></table></div>'; }
	public function booking_page() { $this->page_start( __( 'Booking Page', 'plugoza-event-ticketing' ) ); ?><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_booking_page"><?php wp_nonce_field( 'plugoza_ett_booking_page' ); ?><p><label for="plugoza-ett-booking-page"><strong><?php esc_html_e( 'Booking page', 'plugoza-event-ticketing' ); ?></strong></label><br><?php wp_dropdown_pages( array( 'name' => 'booking_page_id', 'id' => 'plugoza-ett-booking-page', 'show_option_none' => esc_html__( 'Use the event list page', 'plugoza-event-ticketing' ), 'option_none_value' => 0, 'selected' => absint( get_option( 'plugoza_ett_booking_page_id', 0 ) ) ) ); ?></p><p class="description"><?php esc_html_e( 'Create a page containing [plugoza_ett_event_booking]. Event card buttons will open this page.', 'plugoza-event-ticketing' ); ?></p><?php submit_button( esc_html__( 'Save Booking Page', 'plugoza-event-ticketing' ) ); ?></form></div><?php }
	public function booking_page_save() { $this->guard( 'plugoza_ett_booking_page' ); update_option( 'plugoza_ett_booking_page_id', absint( $_POST['booking_page_id'] ?? 0 ) ); wp_safe_redirect( admin_url( 'admin.php?page=plugoza-ett-booking-page' ) ); exit; }
	public function settings_save() { $this->guard( 'plugoza_ett_settings' ); update_option( 'plugoza_ett_settings', array( 'currency' => strtoupper( sanitize_text_field( wp_unslash( $_POST['currency'] ?? 'USD' ) ) ) ) ); update_option( 'plugoza_ett_remove_data_on_uninstall', ! empty( $_POST['remove_data'] ) ); wp_safe_redirect( admin_url( 'admin.php?page=plugoza-ett-settings' ) ); exit; }

	/** Render the single Manual Pay gateway configuration. */
	public function manual_pay() { $this->payment_gateways(); }

	/** Modern orders screen with event filtering and protected ticket activation. */
	public function orders() {
		global $wpdb;
		$this->page_start( __( 'Orders', 'plugoza-event-ticketing' ) );
		$events = $wpdb->get_results( $wpdb->prepare( 'SELECT id, name FROM %i ORDER BY start_date DESC', Repository::table( 'events' ) ) );
		$filter_nonce = sanitize_text_field( wp_unslash( $_GET['plugoza_ett_orders_nonce'] ?? '' ) );
		$valid_filter = wp_verify_nonce( $filter_nonce, 'plugoza_ett_orders_filter' );
		$event_id     = $valid_filter ? absint( $_GET['event_id'] ?? 0 ) : 0;
		if ( ! $event_id && $events ) { $event_id = absint( $events[0]->id ); }
		$search = $valid_filter ? sanitize_text_field( wp_unslash( $_GET['s'] ?? '' ) ) : '';
		$bookings_table  = Repository::table( 'bookings' );
		$customers_table = Repository::table( 'customers' );
		if ( $event_id && strlen( $search ) >= 3 ) {
			$like = '%' . $wpdb->esc_like( $search ) . '%';
			$rows = $wpdb->get_results( $wpdb->prepare( "SELECT b.id, b.booking_number, b.booking_date, b.quantity, b.amount, CASE WHEN b.payment_method = 'free_registration' THEN 'Manual Pay' ELSE b.payment_method END AS payment_method, b.payment_status, b.booking_status, c.first_name, c.last_name, c.mobile, c.email FROM %i b LEFT JOIN %i c ON c.id = b.customer_id WHERE b.event_id = %d AND (b.booking_number LIKE %s OR c.first_name LIKE %s OR c.last_name LIKE %s OR c.mobile LIKE %s OR c.email LIKE %s) ORDER BY b.booking_date DESC LIMIT 100", $bookings_table, $customers_table, $event_id, $like, $like, $like, $like, $like ) );
		} elseif ( $event_id ) {
			$rows = $wpdb->get_results( $wpdb->prepare( "SELECT b.id, b.booking_number, b.booking_date, b.quantity, b.amount, CASE WHEN b.payment_method = 'free_registration' THEN 'Manual Pay' ELSE b.payment_method END AS payment_method, b.payment_status, b.booking_status, c.first_name, c.last_name, c.mobile, c.email FROM %i b LEFT JOIN %i c ON c.id = b.customer_id WHERE b.event_id = %d ORDER BY b.booking_date DESC LIMIT 100", $bookings_table, $customers_table, $event_id ) );
		} elseif ( strlen( $search ) >= 3 ) {
			$like = '%' . $wpdb->esc_like( $search ) . '%';
			$rows = $wpdb->get_results( $wpdb->prepare( "SELECT b.id, b.booking_number, b.booking_date, b.quantity, b.amount, CASE WHEN b.payment_method = 'free_registration' THEN 'Manual Pay' ELSE b.payment_method END AS payment_method, b.payment_status, b.booking_status, c.first_name, c.last_name, c.mobile, c.email FROM %i b LEFT JOIN %i c ON c.id = b.customer_id WHERE b.booking_number LIKE %s OR c.first_name LIKE %s OR c.last_name LIKE %s OR c.mobile LIKE %s OR c.email LIKE %s ORDER BY b.booking_date DESC LIMIT 100", $bookings_table, $customers_table, $like, $like, $like, $like, $like ) );
		} else {
			$rows = $wpdb->get_results( $wpdb->prepare( "SELECT b.id, b.booking_number, b.booking_date, b.quantity, b.amount, CASE WHEN b.payment_method = 'free_registration' THEN 'Manual Pay' ELSE b.payment_method END AS payment_method, b.payment_status, b.booking_status, c.first_name, c.last_name, c.mobile, c.email FROM %i b LEFT JOIN %i c ON c.id = b.customer_id ORDER BY b.booking_date DESC LIMIT 100", $bookings_table, $customers_table ) );
		}
		$currency = sanitize_text_field( get_option( 'plugoza_ett_settings', array() )['currency'] ?? 'USD' );
		?>
		<div class="plugoza-ett-toolbar"><form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>"><input type="hidden" name="page" value="plugoza-ett-orders"><?php wp_nonce_field( 'plugoza_ett_orders_filter', 'plugoza_ett_orders_nonce' ); ?><label class="screen-reader-text" for="plugoza-ett-order-event"><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?></label><select class="plugoza-ett-event-filter" id="plugoza-ett-order-event" name="event_id"><?php foreach ( $events as $event ) : ?><option value="<?php echo esc_attr( $event->id ); ?>" <?php selected( $event_id, $event->id ); ?>><?php echo esc_html( $event->name ); ?></option><?php endforeach; ?></select><label class="screen-reader-text" for="plugoza-ett-order-search"><?php esc_html_e( 'Search orders', 'plugoza-event-ticketing' ); ?></label><input class="plugoza-ett-order-search plugoza-ett-search" id="plugoza-ett-order-search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search 3+ characters', 'plugoza-event-ticketing' ); ?>"></form></div>
		<table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Order ID', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Booking date', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Guest name', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Phone', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Total entry', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Amount', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Payment method', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Status', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody>
		<?php if ( $rows ) : foreach ( $rows as $row ) : $active = 'confirmed' === $row->booking_status; ?><tr><td><?php echo esc_html( $row->booking_number ); ?></td><td><?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $row->booking_date ) ) ); ?></td><td><?php echo esc_html( trim( $row->first_name . ' ' . $row->last_name ) ); ?><br><small><?php echo esc_html( $row->email ); ?></small></td><td><?php echo esc_html( $row->mobile ); ?></td><td><?php echo esc_html( number_format_i18n( $row->quantity ) ); ?></td><td><?php echo esc_html( number_format_i18n( (float) $row->amount, 2 ) . ' ' . $currency ); ?></td><td><?php echo esc_html( str_replace( '_', ' ', $row->payment_method ) ); ?></td><td><form class="plugoza-ett-toggle" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_toggle_booking"><input type="hidden" name="booking_id" value="<?php echo esc_attr( $row->id ); ?>"><?php wp_nonce_field( 'plugoza_ett_toggle_booking_' . $row->id ); ?><label><input class="plugoza-ett-booking-toggle" type="checkbox" name="active" value="1" <?php checked( $active ); ?> <?php disabled( ! $active && 'paid' !== $row->payment_status ); ?>> <?php echo esc_html( $active ? __( 'Active', 'plugoza-event-ticketing' ) : $row->booking_status ); ?></label></form><?php if ( 'bank_transfer' === $row->payment_method && 'paid' !== $row->payment_status ) : ?><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_mark_paid"><input type="hidden" name="booking_id" value="<?php echo esc_attr( $row->id ); ?>"><?php wp_nonce_field( 'plugoza_ett_mark_paid_' . $row->id ); submit_button( __( 'Mark paid', 'plugoza-event-ticketing' ), 'secondary small', 'submit', false ); ?></form><?php endif; ?></td></tr><?php endforeach; else : ?><tr><td colspan="8"><?php esc_html_e( 'No orders found.', 'plugoza-event-ticketing' ); ?></td></tr><?php endif; ?></tbody></table></div><?php
	}

	public function toggle_booking() { $id = absint( $_POST['booking_id'] ?? 0 ); $this->guard( 'plugoza_ett_toggle_booking_' . $id ); global $wpdb; $booking = $wpdb->get_row( $wpdb->prepare( 'SELECT payment_status FROM %i WHERE id = %d', Repository::table( 'bookings' ), $id ) ); if ( $booking ) { $status = ! empty( $_POST['active'] ) && 'paid' === $booking->payment_status ? 'confirmed' : 'cancelled'; $wpdb->update( Repository::table( 'bookings' ), array( 'booking_status' => $status ), array( 'id' => $id ), array( '%s' ), array( '%d' ) ); } wp_safe_redirect( wp_get_referer() ?: admin_url( 'admin.php?page=plugoza-ett-orders' ) ); exit; }

	/** Ticket manager that protects booked inventory from unsafe reductions. */
	public function ticket_manager() {
		global $wpdb; $this->page_start( __( 'Tickets', 'plugoza-event-ticketing' ) ); $events = Repository::events(); $event_id = absint( $_GET['event_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filter.
		if ( $event_id ) { $tickets = $wpdb->get_results( $wpdb->prepare( 'SELECT t.*, e.name AS event_name, (t.quantity - t.available_quantity) AS booked FROM %i t INNER JOIN %i e ON e.id = t.event_id WHERE t.event_id = %d ORDER BY t.created_at DESC', Repository::table( 'tickets' ), Repository::table( 'events' ), $event_id ) ); } else { $tickets = $wpdb->get_results( $wpdb->prepare( 'SELECT t.*, e.name AS event_name, (t.quantity - t.available_quantity) AS booked FROM %i t INNER JOIN %i e ON e.id = t.event_id ORDER BY t.created_at DESC', Repository::table( 'tickets' ), Repository::table( 'events' ) ) ); }
		$currency = sanitize_text_field( get_option( 'plugoza_ett_settings', array() )['currency'] ?? 'USD' ); ?>
		<div class="plugoza-ett-card"><h2><?php esc_html_e( 'Add ticket', 'plugoza-event-ticketing' ); ?></h2><form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_save_ticket"><?php wp_nonce_field( 'plugoza_ett_save_ticket' ); ?><p><label><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?> <select name="event_id"><?php foreach ( $events as $event ) : ?><option value="<?php echo esc_attr( $event->id ); ?>"><?php echo esc_html( $event->name ); ?></option><?php endforeach; ?></select></label></p><p><label><?php esc_html_e( 'Ticket name', 'plugoza-event-ticketing' ); ?> <input required name="name"></label></p><p><label><?php esc_html_e( 'Price', 'plugoza-event-ticketing' ); ?> <input required min="0" step="0.01" type="number" name="price"> <?php echo esc_html( $currency ); ?></label></p><p><label><?php esc_html_e( 'Quantity', 'plugoza-event-ticketing' ); ?> <input required min="1" type="number" name="quantity"></label></p><?php submit_button( __( 'Add ticket', 'plugoza-event-ticketing' ) ); ?></form></div>
		<div class="plugoza-ett-toolbar"><form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>"><input type="hidden" name="page" value="plugoza-ett-ticket-manager"><select class="plugoza-ett-event-filter" name="event_id"><option value="0"><?php esc_html_e( 'All events', 'plugoza-event-ticketing' ); ?></option><?php foreach ( $events as $event ) : ?><option value="<?php echo esc_attr( $event->id ); ?>" <?php selected( $event_id, $event->id ); ?>><?php echo esc_html( $event->name ); ?></option><?php endforeach; ?></select></form></div><table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Ticket', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Event', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Price', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Available', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Actions', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody><?php foreach ( $tickets as $ticket ) : ?><tr><td><?php echo esc_html( $ticket->name ); ?></td><td><?php echo esc_html( $ticket->event_name ); ?></td><td><?php echo esc_html( $ticket->price . ' ' . $currency ); ?></td><td><?php echo esc_html( $ticket->available_quantity . ' / ' . $ticket->quantity ); ?></td><td class="plugoza-ett-ticket-actions"><a href="#edit-ticket-<?php echo esc_attr( $ticket->id ); ?>"><?php esc_html_e( 'Edit quantity', 'plugoza-event-ticketing' ); ?></a><?php if ( ! $ticket->booked ) : ?><a class="submitdelete" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'plugoza_ett_delete_ticket', 'ticket_id' => $ticket->id ), admin_url( 'admin-post.php' ) ), 'plugoza_ett_delete_ticket_' . $ticket->id ) ); ?>"><?php esc_html_e( 'Delete', 'plugoza-event-ticketing' ); ?></a><?php endif; ?><form id="edit-ticket-<?php echo esc_attr( $ticket->id ); ?>" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"><input type="hidden" name="action" value="plugoza_ett_update_ticket"><input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>"><?php wp_nonce_field( 'plugoza_ett_update_ticket_' . $ticket->id ); ?><label><?php esc_html_e( 'Total quantity', 'plugoza-event-ticketing' ); ?> <input required min="<?php echo esc_attr( $ticket->booked ); ?>" type="number" name="quantity" value="<?php echo esc_attr( $ticket->quantity ); ?>"></label> <small><?php esc_html_e( 'Booked:', 'plugoza-event-ticketing' ); ?> <?php echo esc_html( absint( $ticket->booked ) ); ?></small> <?php submit_button( __( 'Save', 'plugoza-event-ticketing' ), 'secondary small', 'submit', false ); ?></form></td></tr><?php endforeach; ?></tbody></table></div><?php
	}

	public function update_ticket() { $id = absint( $_POST['ticket_id'] ?? 0 ); $this->guard( 'plugoza_ett_update_ticket_' . $id ); global $wpdb; $ticket = $wpdb->get_row( $wpdb->prepare( 'SELECT quantity, available_quantity FROM %i WHERE id = %d', Repository::table( 'tickets' ), $id ) ); $quantity = absint( $_POST['quantity'] ?? 0 ); if ( ! $ticket ) { wp_die( esc_html__( 'Ticket not found.', 'plugoza-event-ticketing' ) ); } $booked = absint( $ticket->quantity ) - absint( $ticket->available_quantity ); if ( $quantity < $booked ) { wp_die( esc_html__( 'Quantity cannot be lower than booked entries.', 'plugoza-event-ticketing' ) ); } $available = $quantity - $booked; $wpdb->update( Repository::table( 'tickets' ), array( 'quantity' => $quantity, 'available_quantity' => $available, 'status' => $available ? 'active' : 'sold_out' ), array( 'id' => $id ), array( '%d', '%d', '%s' ), array( '%d' ) ); wp_safe_redirect( wp_get_referer() ?: admin_url( 'admin.php?page=plugoza-ett-ticket-manager' ) ); exit; }
	public function delete_ticket() { $id = absint( $_GET['ticket_id'] ?? 0 ); $this->guard( 'plugoza_ett_delete_ticket_' . $id ); global $wpdb; $bookings = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE ticket_id = %d', Repository::table( 'bookings' ), $id ) ); if ( ! $bookings ) { $wpdb->delete( Repository::table( 'tickets' ), array( 'id' => $id ), array( '%d' ) ); } wp_safe_redirect( admin_url( 'admin.php?page=plugoza-ett-ticket-manager' ) ); exit; }

	public function customer_list() { global $wpdb; $this->page_start( __( 'Customers', 'plugoza-event-ticketing' ) ); $search = sanitize_text_field( wp_unslash( $_GET['s'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only search.
		if ( $search ) { $like = '%' . $wpdb->esc_like( $search ) . '%'; $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT first_name, last_name, email, mobile FROM %i WHERE first_name LIKE %s OR last_name LIKE %s OR email LIKE %s OR mobile LIKE %s ORDER BY created_at DESC LIMIT 100', Repository::table( 'customers' ), $like, $like, $like, $like ) ); } else { $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT first_name, last_name, email, mobile FROM %i ORDER BY created_at DESC LIMIT 100', Repository::table( 'customers' ) ) ); } ?><form method="get"><input type="hidden" name="page" value="plugoza-ett-customer-list"><p class="search-box"><label class="screen-reader-text" for="customer-search-input"><?php esc_html_e( 'Search customers', 'plugoza-event-ticketing' ); ?></label><input id="customer-search-input" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search name, email or phone', 'plugoza-event-ticketing' ); ?>"> <?php submit_button( __( 'Search', 'plugoza-event-ticketing' ), 'secondary', '', false ); ?></p></form><table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Name', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Email', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Phone', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody><?php foreach ( $rows as $customer ) : ?><tr><td><?php echo esc_html( trim( $customer->first_name . ' ' . $customer->last_name ) ); ?></td><td><?php echo esc_html( $customer->email ); ?></td><td><?php echo esc_html( $customer->mobile ); ?></td></tr><?php endforeach; ?></tbody></table></div><?php }

	public function shortcode_reference() { $this->page_start( __( 'Shortcodes', 'plugoza-event-ticketing' ) ); ?><div class="plugoza-ett-card"><h2><?php esc_html_e( 'Required pages', 'plugoza-event-ticketing' ); ?></h2><p><?php esc_html_e( 'Create an Events page with [event_list], a Booking page with [event_booking], and a Booking Success page with [event_booking_success]. Select the Booking page in Event Booking > Booking Page.', 'plugoza-event-ticketing' ); ?></p></div><table class="widefat striped"><thead><tr><th><?php esc_html_e( 'Shortcode', 'plugoza-event-ticketing' ); ?></th><th><?php esc_html_e( 'Purpose', 'plugoza-event-ticketing' ); ?></th></tr></thead><tbody><?php foreach ( array( '[event_list]' => __( 'Event cards and Book now buttons.', 'plugoza-event-ticketing' ), '[event_booking]' => __( 'Booking form for the event selected from an Event List button.', 'plugoza-event-ticketing' ), '[event_booking event_id="1"]' => __( 'Booking form for a specific event.', 'plugoza-event-ticketing' ), '[event_ticket id="1"]' => __( 'Booking form for one specific event.', 'plugoza-event-ticketing' ), '[event_details id="1"]' => __( 'Event name, description, date, and venue.', 'plugoza-event-ticketing' ), '[event_booking_success]' => __( 'Required on the Booking Success page; displays PDF download after booking.', 'plugoza-event-ticketing' ), '[plugoza_ett_my_tickets]' => __( 'Tickets for the currently logged-in customer.', 'plugoza-event-ticketing' ) ) as $shortcode => $purpose ) : ?><tr><td><code><?php echo esc_html( $shortcode ); ?></code></td><td><?php echo esc_html( $purpose ); ?></td></tr><?php endforeach; ?></tbody></table></div><?php }

}
