<?php
/**
 * Branch create/edit form.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

if ( ! current_user_can( lucrsure_admin_capability() ) ) {
	wp_die( esc_html__( 'You are not allowed to access this page.', 'lucrativeit-subscription-recurring-billing' ) );
}

$repo   = new LUCRSURE_Branch_Repository();
$id     = 0;

if ( isset( $_GET['id'], $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_admin_form' )
) {
	$id = absint( wp_unslash( $_GET['id'] ) );
}

$branch = $id ? $repo->find( $id ) : array();
if ( $id && empty( $branch ) ) {
	$id     = 0;
	$branch = array();
}

$name         = isset( $branch['name'] ) ? $branch['name'] : '';
$code         = isset( $branch['code'] ) ? $branch['code'] : '';
$region       = isset( $branch['region'] ) ? $branch['region'] : '';
$address      = isset( $branch['address'] ) ? $branch['address'] : '';
$phone        = isset( $branch['phone'] ) ? $branch['phone'] : '';
$opening_time = isset( $branch['opening_time'] ) ? $branch['opening_time'] : '';
$closing_time = isset( $branch['closing_time'] ) ? $branch['closing_time'] : '';
$image_id     = isset( $branch['image_id'] ) ? (int) $branch['image_id'] : 0;
$image_url    = $branch ? LUCRSURE_Branch_Repository::image_url( $branch ) : '';
$description  = isset( $branch['description'] ) ? $branch['description'] : '';
$amenities    = $branch ? implode( "\n", LUCRSURE_Branch_Repository::amenities( $branch ) ) : '';
$is_main      = ! empty( $branch['is_main'] );
$is_active    = $id ? ! empty( $branch['is_active'] ) : true;
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php echo $id ? esc_html__( 'Edit Branch', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'New Branch', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Add a regional office or satellite location.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
</div>

<div class="lucrsure-card lucrsure-form-card">
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-branch-image-form">
		<?php wp_nonce_field( 'lucrsure_save_branch' ); ?>
		<input type="hidden" name="action" value="lucrsure_save_branch" />
		<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Branch Name', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="name" value="<?php echo esc_attr( $name ); ?>" required />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Code', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="code" value="<?php echo esc_attr( $code ); ?>" placeholder="north-america" />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Region', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="region" value="<?php echo esc_attr( $region ); ?>" placeholder="North America" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Address', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="address" value="<?php echo esc_attr( $address ); ?>" />
			</div>
		</div>

		<div class="lucrsure-form-row lucrsure-branch-contact-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Phone Number', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="tel" name="phone" value="<?php echo esc_attr( $phone ); ?>" placeholder="+1 (555) 123-4567" autocomplete="tel" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Opening Time', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="time" name="opening_time" value="<?php echo esc_attr( $opening_time ); ?>" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Closing Time', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="time" name="closing_time" value="<?php echo esc_attr( $closing_time ); ?>" />
			</div>
		</div>

		<div class="lucrsure-branding-logo-field">
			<label><?php esc_html_e( 'Branch Image', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<div class="lucrsure-branding-logo-row">
				<div class="lucrsure-branding-logo-preview lucrsure-branch-image-preview">
					<?php if ( $image_url ) : ?>
						<img src="<?php echo esc_url( $image_url ); ?>" alt="" />
					<?php else : ?>
						<span class="lucrsure-muted"><?php esc_html_e( 'No image selected', 'lucrativeit-subscription-recurring-billing' ); ?></span>
					<?php endif; ?>
				</div>
				<div class="lucrsure-branding-logo-actions">
					<input type="hidden" name="image_id" id="lucrsure-branch-image-id" value="<?php echo esc_attr( (string) $image_id ); ?>" />
					<button type="button" class="lucrsure-btn lucrsure-btn-light lucrsure-branch-image-upload"><?php esc_html_e( 'Select Image', 'lucrativeit-subscription-recurring-billing' ); ?></button>
					<button type="button" class="lucrsure-btn lucrsure-btn-light lucrsure-branch-image-remove<?php echo $image_url ? '' : ' is-hidden'; ?>"><?php esc_html_e( 'Remove', 'lucrativeit-subscription-recurring-billing' ); ?></button>
				</div>
			</div>
		</div>

		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Description', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<textarea name="description" rows="3"><?php echo esc_textarea( $description ); ?></textarea>
		</div>

		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Amenities', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<textarea name="amenities" rows="4" placeholder="<?php esc_attr_e( "Cardio Zone\nPool\nSauna", 'lucrativeit-subscription-recurring-billing' ); ?>"><?php echo esc_textarea( $amenities ); ?></textarea>
			<p class="lucrsure-muted"><?php esc_html_e( 'One amenity per line. Shown as tags on the frontend locations section.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		</div>

		<div class="lucrsure-form-inline">
			<label class="lucrsure-check"><input type="checkbox" name="is_main" value="1" <?php checked( $is_main ); ?> /> <?php esc_html_e( 'Main headquarters branch', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<label class="lucrsure-check"><input type="checkbox" name="is_active" value="1" <?php checked( $is_active ); ?> /> <?php esc_html_e( 'Active', 'lucrativeit-subscription-recurring-billing' ); ?></label>
		</div>

		<div class="lucrsure-form-actions">
			<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_url( 'branches' ) ); ?>"><?php esc_html_e( 'Cancel', 'lucrativeit-subscription-recurring-billing' ); ?></a>
			<button type="submit" class="lucrsure-btn lucrsure-btn-primary"><?php echo $id ? esc_html__( 'Update Branch', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'Create Branch', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</div>
	</form>
</div>
