<?php
/**
 * Edición de usuario JB (standalone).
 *
 * @package Logistica
 */

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

require_once LOGISTICA_PLUGIN_DIR . 'admin/views/partials.php';

$user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0;
$user    = $user_id > 0 ? get_userdata( $user_id ) : false;

if ( ! is_user_logged_in() || ! Logistica_Roles::user_can_manage_config() ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'No tienes permisos para editar usuarios.', 'logistica' ) . '</p></div>';
	return;
}

if ( ! $user || ! $user->exists() ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'Usuario no encontrado.', 'logistica' ) . '</p></div>';
	return;
}

$config_tab_url  = logistica_config_section_url( logistica_config_return_section() );
$status          = isset( $_GET['logistica_status'] ) ? sanitize_key( wp_unslash( (string) $_GET['logistica_status'] ) ) : '';
$user_edit_url     = add_query_arg(
	array(
		'user_id'        => $user_id,
		'return_section' => logistica_config_return_section(),
	),
	home_url( 'user-edit.php' )
);
$role_label      = Logistica_Roles::get_user_role_label( $user_id );
$current_user_id = (int) get_current_user_id();
$can_delete_user = ( defined( 'LOGISTICA_STANDALONE' ) && LOGISTICA_STANDALONE )
	&& $current_user_id !== $user_id
	&& ! ( class_exists( 'Logistica_User_Lifecycle', false ) && Logistica_User_Lifecycle::is_deleted( $user_id ) );
$user_label      = trim( $user->display_name . ' (' . $user->user_login . ')' );
$initials        = preg_replace( '/[^A-ZÁÉÍÓÚÑ]/u', '', mb_strtoupper( mb_substr( $user->display_name, 0, 2 ) ) );
if ( '' === $initials ) {
	$initials = strtoupper( mb_substr( $user->user_login, 0, 2 ) );
}
?>
<div class="logistica-user-form-page logistica-user-form-page--edit">
	<div class="logistica-user-form">
		<header class="logistica-user-form__hero">
			<a class="logistica-user-form__back" href="<?php echo esc_url( $config_tab_url ); ?>">
				<span class="logistica-user-form__back-icon" aria-hidden="true">
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 18l-6-6 6-6"/></svg>
				</span>
				<span class="logistica-user-form__back-label"><?php esc_html_e( 'Volver a configuración', 'logistica' ); ?></span>
			</a>
			<p class="logistica-user-form__brand"><?php esc_html_e( 'JB Planeación', 'logistica' ); ?></p>
			<h1 class="logistica-user-form__title"><?php esc_html_e( 'Editar usuario', 'logistica' ); ?></h1>
			<p class="logistica-user-form__intro"><?php esc_html_e( 'Actualiza el rol operativo y las pestañas personalizadas del usuario.', 'logistica' ); ?></p>
		</header>

		<?php if ( 'user_saved' === $status ) : ?>
			<div class="logistica-user-form__notice logistica-user-form__notice--success" role="status">
				<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
				<?php esc_html_e( 'Usuario actualizado correctamente.', 'logistica' ); ?>
			</div>
		<?php endif; ?>
		<?php if ( 'user_created' === $status ) : ?>
			<div class="logistica-user-form__notice logistica-user-form__notice--success" role="status">
				<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
				<?php esc_html_e( 'Usuario creado correctamente.', 'logistica' ); ?>
			</div>
		<?php endif; ?>

		<form class="logistica-user-form__body" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" data-logistica-user-form>
			<input type="hidden" name="action" value="logistica_save_user_jb_profile">
			<input type="hidden" name="user_id" value="<?php echo esc_attr( (string) $user_id ); ?>">
			<input type="hidden" name="logistica_redirect_to" value="<?php echo esc_url( $user_edit_url ); ?>">
			<input type="hidden" name="return_section" value="<?php echo esc_attr( logistica_config_return_section() ); ?>">
			<?php wp_nonce_field( 'logistica_save_user_jb_profile' ); ?>

			<section class="logistica-user-form__section logistica-user-form__section--profile">
				<div class="logistica-user-form__profile-card">
					<div class="logistica-user-form__avatar" aria-hidden="true"><?php echo esc_html( $initials ); ?></div>
					<div class="logistica-user-form__profile-copy">
						<strong><?php echo esc_html( $user->display_name ); ?></strong>
						<span class="logistica-user-form__profile-login">@<?php echo esc_html( $user->user_login ); ?></span>
						<span class="logistica-user-form__profile-email"><?php echo esc_html( $user->user_email ); ?></span>
					</div>
					<?php if ( $role_label ) : ?>
						<span class="logistica-user-form__profile-role"><?php echo esc_html( $role_label ); ?></span>
					<?php endif; ?>
				</div>
			</section>

			<?php Logistica_Roles::render_user_profile_fields( $user, array( 'layout' => 'cards' ) ); ?>

			<footer class="logistica-user-form__actions">
				<?php if ( $can_delete_user ) : ?>
					<div class="logistica-user-form__actions-start">
						<form
							method="post"
							action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"
							class="logistica-user-delete-form"
							data-user-label="<?php echo esc_attr( $user_label ); ?>"
						>
							<input type="hidden" name="action" value="logistica_delete_user_jb">
							<input type="hidden" name="user_id" value="<?php echo esc_attr( (string) $user_id ); ?>">
							<input type="hidden" name="logistica_redirect_to" value="<?php echo esc_url( $config_tab_url ); ?>">
							<?php wp_nonce_field( 'logistica_delete_user_jb_' . $user_id ); ?>
							<button
								type="submit"
								class="logistica-user-form__btn logistica-user-form__btn--delete"
								data-logistica-delete-user
							>
								<span class="logistica-user-form__btn-icon" aria-hidden="true">
									<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M8 6V4h8v2"/><path d="M19 6l-1 14H6L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/></svg>
								</span>
								<?php esc_html_e( 'Eliminar', 'logistica' ); ?>
							</button>
						</form>
					</div>
				<?php endif; ?>
				<div class="logistica-user-form__actions-end">
					<a class="logistica-user-form__btn logistica-user-form__btn--cancel" href="<?php echo esc_url( $config_tab_url ); ?>">
						<?php esc_html_e( 'Cancelar', 'logistica' ); ?>
					</a>
					<button class="logistica-user-form__btn logistica-user-form__btn--submit" type="submit">
						<span class="logistica-user-form__btn-icon" aria-hidden="true">
							<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>
						</span>
						<?php esc_html_e( 'Guardar cambios', 'logistica' ); ?>
					</button>
				</div>
			</footer>
		</form>
	</div>
</div>
