<?php
/**
 * The template to display modal.
 *
 * This template can be overridden by copying it to
 * your-child-theme/views/modal.php.
 *
 * @param array $args
 *
 * @package MadeByAura\Prime
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime;

use MadeByAura\Prime\Helpers\Assorted;
use MadeByAura\Prime\Helpers\Markup;

defined( 'ABSPATH' ) || die();

// Merge incoming `$args` with defaults.
$args = array_replace_recursive(
	[
		'id'                    => '',
		'type'                  => 'popup',
		'header'                => true,
		'title'                 => '',
		'content_template'      => '',
		'content_template_args' => [],
	],
	$args,
);

// Don't proceed if there is no id.
if ( ! $args['id'] ) {
	return;
}

// Set variables.
$content_template = $args['content_template'] ?: $args['id'];

// Don't proceed if there is no content template.
if ( ! $content_template ) {
	return;
}

// Set CSS classes.
$classes[] = 'aura-modal';
$classes[] = 'aura-modal--' . Assorted::dashify( $args['id'] );
$classes[] = $args['type'] ? 'aura-modal--' . Assorted::dashify( $args['type'] ) : '';

// Set HTML attributes.
$attrs['class']      = $classes;
$attrs['data-modal'] = $args['id'];
?>

<div <?php Markup::echo_attrs( $attrs ); ?>>
	<div class="aura-modal__background" data-close-modal="<?php echo esc_attr( $args['id'] ); ?>"></div>

	<div class="aura-modal__container">
		<div class="aura-modal__inner">
			<div class="aura-modal__close" data-close-modal="<?php echo esc_attr( $args['id'] ); ?>">
				<div class="aura-close"></div>
			</div>

			<?php if ( true === $args['header'] ) : ?>
				<div class="aura-modal__header">
					<?php if ( $args['title'] ) : ?>
						<h1 class="aura-modal__title"><?php echo esc_html( $args['title'] ); ?></h1>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<div class="aura-modal__body">
				<?php
				Base::view(
					"views/modal-content/{$content_template}",
					$args['content_template_args'],
				);
				?>
			</div>
		</div>
	</div>
</div>
