<?php
/**
 * Provide an admin view for editing an individual Rule
 *
 * @since      1.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Admin/Partials
 */

namespace PersonalizeWP\Admin\Partials;

use PersonalizeWP\Admin\PageRules;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

?>
<main class="section flow">
	<a class="back" href="<?php echo esc_url( admin_url( PageRules::$index_url ) ); ?>">&larr; <?php esc_html_e( 'Back', 'personalizewp' ); ?></a>
	<?php
	if ( ! $rule->is_usable ) :
		?>
		<div class="alert alert-danger" role="alert">
			<p><?php esc_html_e( 'This rule is not currently usable for the following reasons:', 'personalizewp' ); ?></p>
			<ul>
				<li>
				<?php
				echo implode( '</li><li>', array_map( 'esc_html', $rule->getConditionDependencyIssues() ) );
				?>
				</li>
			</ul>
		</div>
		<?php
	endif;
	?>

	<div class="repel">
		<h2 class="section-title"><?php esc_html_e( 'View / edit rule', 'personalizewp' ); ?></h2>
		<ul class="context-links">
			<?php if ( $rule->can_duplicate ) : ?>
				<li><a class="contextual-link" href="<?php echo esc_url( wp_nonce_url( admin_url( PageRules::$duplicate_url . $rule->id ), 'rule-duplicate-' . $rule->id ) ); ?>"><?php esc_html_e( 'Duplicate', 'personalizewp' ); ?></a></li>
			<?php else : ?>
				<li><span class="contextual-link" inert><?php esc_html_e( 'Duplicate', 'personalizewp' ); ?></span></li>
			<?php endif; ?>

			<?php if ( $rule->can_delete ) : ?>
				<li><a class="contextual-link delete-rule" href="<?php echo esc_url( wp_nonce_url( admin_url( PageRules::$delete_url . $rule->id ), 'rule-delete-' . $rule->id ) ); ?>"
					data-show-modal="#deleteRuleModal"><?php esc_html_e( 'Delete', 'personalizewp' ); ?></a></li>
			<?php else : ?>
				<li><span class="contextual-link" inert><?php esc_html_e( 'Delete', 'personalizewp' ); ?></span></li>
			<?php endif; ?>
		</ul>
	</div>

	<div class="pwp-panel">
		<div class="section-description">
			<p><?php esc_html_e( 'Edit an existing rule using the options below. Once the rule has been saved, it will become active or inactive, depending on the conditions chosen. All conditions within a single rule are matched using AND logic.', 'personalizewp' ); ?></p>
			<p>
			<?php
			echo wp_kses(
				sprintf(
					/* translators: 1: %s HTML of asterisk indicating required fields */
					__( '%s = required fields', 'personalizewp' ),
					'<span class="required">*</span>'
				),
				array(
					'span' => array(
						'class' => array(),
					),
				)
			);
			?>
		</div>

		<form action="<?php echo esc_url( $rule->edit_url ); ?>" method="post" accept-charset="UTF-8" id="pwp-form">
			<?php
				wp_nonce_field( 'rule-update-' . $rule->id );
				require_once __DIR__ . '/_form.php';
			?>
		</form>
	</div>
</main>

<?php
require_once __DIR__ . '/_modal-delete.php';
