<?php
/**
 * Admin view: organizer profiles.
 *
 * @package BitForgeNet_Guest_Tickets
 */

defined( 'ABSPATH' ) || exit;

// This file is a template: it is included from a method -- never at file
// scope -- so everything declared here belongs to that method's variable
// scope and not to the global one. PHPCS reads each file on its own and has
// no way to see where the include happens, so it reads these as globals.
// Stated once, for the whole file, per this plugin's suppression rule.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$organizers = BFNET_Organizer::all( '' );

// phpcs:ignore WordPress.Security.NonceVerification
$edit_id   = isset( $_GET['organizer'] ) ? (int) $_GET['organizer'] : 0;
$editing   = $edit_id ? BFNET_Organizer::get( $edit_id ) : null;
$logo_url  = $editing && $editing->logo_id ? wp_get_attachment_image_url( (int) $editing->logo_id, 'thumbnail' ) : '';
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Organizers', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'The brand behind each event. Its name, logo and contact details appear on the booking page, the emails and the tickets.', 'bitforgenet-guest-tickets' );
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-cols">
		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'All organizers', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<?php if ( empty( $organizers ) ) : ?>
						<div class="bfnet-empty-state"><p><?php esc_html_e( 'No organizer yet.', 'bitforgenet-guest-tickets' ); ?></p></div>
					<?php else : ?>
						<table class="bfnet-table">
							<thead>
								<tr>
									<th></th>
									<th><?php esc_html_e( 'Name', 'bitforgenet-guest-tickets' ); ?></th>
									<th><?php esc_html_e( 'Contact', 'bitforgenet-guest-tickets' ); ?></th>
									<th><?php esc_html_e( 'Colours', 'bitforgenet-guest-tickets' ); ?></th>
									<th><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></th>
									<th></th>
								</tr>
							</thead>
							<tbody>
							<?php foreach ( $organizers as $organizer ) : ?>
								<tr>
									<td style="width:52px">
										<img src="<?php echo esc_url( BFNET_Organizer::logo_url( $organizer ) ); ?>" alt="" style="width:40px;height:40px;object-fit:contain;background:#12141a;border-radius:6px">
									</td>
									<td>
										<div class="bfnet-table__title"><?php echo esc_html( $organizer->name ); ?></div>
										<div class="bfnet-table__sub"><?php echo esc_html( $organizer->tagline ); ?></div>
									</td>
									<td>
										<?php echo esc_html( $organizer->email ); ?>
										<?php if ( $organizer->whatsapp ) : ?>
											<div class="bfnet-table__sub"><?php echo esc_html( $organizer->whatsapp ); ?></div>
										<?php endif; ?>
									</td>
									<td>
										<span style="display:inline-block;width:18px;height:18px;border-radius:4px;background:<?php echo esc_attr( $organizer->brand_primary ); ?>;vertical-align:middle"></span>
										<span style="display:inline-block;width:18px;height:18px;border-radius:4px;background:<?php echo esc_attr( $organizer->brand_secondary ); ?>;vertical-align:middle"></span>
									</td>
									<td>
										<span class="bfnet-badge" style="background:<?php echo 'active' === $organizer->status ? '#1e8e3e' : '#5f6368'; ?>"><?php echo esc_html( $organizer->status ); ?></span>
									</td>
									<td class="bfnet-num">
										<div class="bfnet-actions">
											<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-organizers', array( 'organizer' => (int) $organizer->id ) ) ); ?>"><?php esc_html_e( 'Edit', 'bitforgenet-guest-tickets' ); ?></a>
											<a class="button button-small" style="color:#c5221f" data-bfnet-confirm="<?php esc_attr_e( 'Delete this organizer?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-organizers', array( 'bfnet_do' => 'delete_organizer', 'organizer' => (int) $organizer->id ) ), 'bfnet_delete_organizer', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Delete', 'bitforgenet-guest-tickets' ); ?></a>
										</div>
									</td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					<?php endif; ?>
				</div>
			</div>
		</div>

		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php echo esc_html( $editing ? __( 'Edit organizer', 'bitforgenet-guest-tickets' ) : __( 'New organizer', 'bitforgenet-guest-tickets' ) ); ?></h2>
					<?php if ( $editing ) : ?>
						<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-organizers' ) ); ?>"><?php esc_html_e( 'New', 'bitforgenet-guest-tickets' ); ?></a>
					<?php endif; ?>
				</div>
				<div class="bfnet-panel__body">
					<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
						<?php wp_nonce_field( 'bfnet_save_organizer', '_bfnet_nonce' ); ?>
						<input type="hidden" name="page" value="bitforgenet-guest-tickets-organizers">
						<input type="hidden" name="bfnet_do" value="save_organizer">
						<input type="hidden" name="organizer_id" value="<?php echo $editing ? (int) $editing->id : 0; ?>">

						<div class="bfnet-field">
							<label for="bfnet-org-name"><?php esc_html_e( 'Name', 'bitforgenet-guest-tickets' ); ?> *</label>
							<input type="text" id="bfnet-org-name" name="name" required value="<?php echo esc_attr( $editing ? $editing->name : '' ); ?>">
						</div>

						<div class="bfnet-field">
							<label for="bfnet-org-tagline"><?php esc_html_e( 'Tagline', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="text" id="bfnet-org-tagline" name="tagline" value="<?php echo esc_attr( $editing ? $editing->tagline : '' ); ?>">
						</div>

						<div class="bfnet-field">
							<label class="bfnet-label"><?php esc_html_e( 'Logo', 'bitforgenet-guest-tickets' ); ?></label>
							<div class="bfnet-media">
								<div class="bfnet-media__preview">
									<?php if ( $logo_url ) : ?><img src="<?php echo esc_url( $logo_url ); ?>" alt=""><?php else : ?><span><?php esc_html_e( 'Default logo', 'bitforgenet-guest-tickets' ); ?></span><?php endif; ?>
								</div>
								<div>
									<input type="hidden" name="logo_id" value="<?php echo $editing ? (int) $editing->logo_id : 0; ?>">
									<button type="button" class="button" data-bfnet-media><?php esc_html_e( 'Choose', 'bitforgenet-guest-tickets' ); ?></button>
									<button type="button" class="button-link" data-bfnet-media-clear style="margin-left:8px"><?php esc_html_e( 'Reset', 'bitforgenet-guest-tickets' ); ?></button>
									<p class="description"><?php esc_html_e( 'PNG with transparency works best on tickets.', 'bitforgenet-guest-tickets' ); ?></p>
								</div>
							</div>
						</div>

						<div class="bfnet-row">
							<div class="bfnet-field">
								<label for="bfnet-org-email"><?php esc_html_e( 'Email', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="email" id="bfnet-org-email" name="email" value="<?php echo esc_attr( $editing ? $editing->email : '' ); ?>">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-org-phone"><?php esc_html_e( 'Phone', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="tel" id="bfnet-org-phone" name="phone" value="<?php echo esc_attr( $editing ? $editing->phone : '' ); ?>">
							</div>
						</div>

						<div class="bfnet-field">
							<label for="bfnet-org-whatsapp"><?php esc_html_e( 'WhatsApp', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="tel" id="bfnet-org-whatsapp" name="whatsapp" value="<?php echo esc_attr( $editing ? $editing->whatsapp : '' ); ?>">
						</div>

						<div class="bfnet-row">
							<div class="bfnet-field">
								<label for="bfnet-org-web"><?php esc_html_e( 'Website', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="url" id="bfnet-org-web" name="website" value="<?php echo esc_attr( $editing ? $editing->website : '' ); ?>">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-org-ig"><?php esc_html_e( 'Instagram', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="url" id="bfnet-org-ig" name="instagram" value="<?php echo esc_attr( $editing ? $editing->instagram : '' ); ?>">
							</div>
						</div>

						<div class="bfnet-row">
							<div class="bfnet-field">
								<label for="bfnet-org-c1"><?php esc_html_e( 'Primary colour', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-org-c1" name="brand_primary" value="<?php echo esc_attr( $editing ? $editing->brand_primary : '#C9A227' ); ?>">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-org-c2"><?php esc_html_e( 'Secondary colour', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-org-c2" name="brand_secondary" value="<?php echo esc_attr( $editing ? $editing->brand_secondary : '#0B0B0B' ); ?>">
							</div>
						</div>

						<div class="bfnet-field">
							<label for="bfnet-org-status"><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></label>
							<select id="bfnet-org-status" name="status">
								<option value="active" <?php selected( $editing ? $editing->status : 'active', 'active' ); ?>><?php esc_html_e( 'Active', 'bitforgenet-guest-tickets' ); ?></option>
								<option value="inactive" <?php selected( $editing ? $editing->status : '', 'inactive' ); ?>><?php esc_html_e( 'Inactive', 'bitforgenet-guest-tickets' ); ?></option>
							</select>
						</div>

						<button class="button button-primary" style="width:100%"><?php echo esc_html( $editing ? __( 'Save organizer', 'bitforgenet-guest-tickets' ) : __( 'Create organizer', 'bitforgenet-guest-tickets' ) ); ?></button>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
