<?php
/**
 * Plan category 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_Plan_Category_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'] ) );
}

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

$name        = isset( $category['name'] ) ? $category['name'] : '';
$slug        = isset( $category['slug'] ) ? $category['slug'] : '';
$description = isset( $category['description'] ) ? $category['description'] : '';
$is_active   = $id ? ! empty( $category['is_active'] ) : true;
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php echo $id ? esc_html__( 'Edit Plan Category', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'New Plan Category', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Group related subscription plans under a category.', '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' ) ); ?>">
		<?php wp_nonce_field( 'lucrsure_save_plan_category' ); ?>
		<input type="hidden" name="action" value="lucrsure_save_plan_category" />
		<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( 'Category 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( 'Slug', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="slug" value="<?php echo esc_attr( $slug ); ?>" placeholder="enterprise" />
			</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-form-inline">
			<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( 'plan-categories' ) ); ?>"><?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 Category', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'Create Category', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</div>
	</form>
</div>
