<?php
/**
 * Plan create/edit form.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 *
 * @var LUCRSURE_Settings $settings Settings.
 */

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_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'] ) );
}

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

$categories      = ( new LUCRSURE_Plan_Category_Repository() )->options();
$branch_repo     = new LUCRSURE_Branch_Repository();
$branches        = $branch_repo->active();
$name            = isset( $plan['name'] ) ? $plan['name'] : '';
$slug            = isset( $plan['slug'] ) ? $plan['slug'] : '';
$description     = isset( $plan['description'] ) ? $plan['description'] : '';
$price           = isset( $plan['price'] ) ? $plan['price'] : '0.00';
$special_price   = isset( $plan['special_price'] ) ? $plan['special_price'] : '0.00';
$special_enabled = $id ? ! empty( $plan['special_price_enabled'] ) : false;
$currency        = isset( $plan['currency'] ) ? $plan['currency'] : $settings->get( 'default_currency', 'USD' );
$interval        = isset( $plan['billing_interval'] ) ? $plan['billing_interval'] : 'month';
$icount          = isset( $plan['interval_count'] ) ? (int) $plan['interval_count'] : 1;
$trial_days      = isset( $plan['trial_days'] ) ? (int) $plan['trial_days'] : 0;
$category_id     = isset( $plan['category_id'] ) ? (int) $plan['category_id'] : 0;
$branch_id       = isset( $plan['branch_id'] ) ? (int) $plan['branch_id'] : 0;
$thumbnail_id    = isset( $plan['thumbnail_id'] ) ? (int) $plan['thumbnail_id'] : 0;
$thumbnail_url   = $plan ? LUCRSURE_Plan_Repository::thumbnail_url( $plan ) : '';
$is_active       = $id ? ! empty( $plan['is_active'] ) : true;
$is_popular      = $id ? ! empty( $plan['is_popular'] ) : false;
$features        = $plan ? implode( "\n", LUCRSURE_Plan_Repository::features( $plan ) ) : '';

if ( $branch_id > 0 ) {
	$assigned_ids = array_map( 'intval', wp_list_pluck( $branches, 'id' ) );
	if ( ! in_array( $branch_id, $assigned_ids, true ) ) {
		$assigned_branch = $branch_repo->find( $branch_id );
		if ( $assigned_branch ) {
			$branches[] = $assigned_branch;
		}
	}
}
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php echo $id ? esc_html__( 'Edit Plan', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'New Plan', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Define pricing, billing interval and included features.', '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-plan-thumbnail-form">
		<?php wp_nonce_field( 'lucrsure_save_plan' ); ?>
		<input type="hidden" name="action" value="lucrsure_save_plan" />
		<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( 'Plan 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="basic" />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Price', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="number" step="0.01" min="0" name="price" value="<?php echo esc_attr( $price ); ?>" required />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Currency', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="currency" value="<?php echo esc_attr( $currency ); ?>" />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Special Price', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="number" step="0.01" min="0" name="special_price" value="<?php echo esc_attr( $special_price ); ?>" />
				<p class="lucrsure-field-hint"><?php esc_html_e( 'Optional promotional price. When enabled, customers are charged this amount instead of the regular price.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
			</div>
			<div class="lucrsure-field lucrsure-field-check">
				<label class="lucrsure-check"><input type="checkbox" name="special_price_enabled" value="1" <?php checked( $special_enabled ); ?> /> <?php esc_html_e( 'Enable special price', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Billing Interval', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="billing_interval">
					<?php foreach ( lucrsure_billing_intervals() as $key => $label ) : ?>
						<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $interval, $key ); ?>><?php echo esc_html( $label ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Interval Count', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="number" min="1" name="interval_count" value="<?php echo esc_attr( $icount ); ?>" />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Category', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="category_id">
					<option value="0"><?php esc_html_e( '— No category —', 'lucrativeit-subscription-recurring-billing' ); ?></option>
					<?php foreach ( $categories as $cat_id => $cat_name ) : ?>
						<option value="<?php echo esc_attr( $cat_id ); ?>" <?php selected( $category_id, $cat_id ); ?>><?php echo esc_html( $cat_name ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Branch', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="branch_id">
					<option value="0"><?php esc_html_e( '— All branches —', 'lucrativeit-subscription-recurring-billing' ); ?></option>
					<?php foreach ( $branches as $branch ) : ?>
						<option value="<?php echo esc_attr( $branch['id'] ); ?>" <?php selected( $branch_id, (int) $branch['id'] ); ?>><?php echo esc_html( $branch['name'] ); ?></option>
					<?php endforeach; ?>
				</select>
				<p class="lucrsure-field-hint"><?php esc_html_e( 'Assign this plan to a location, or leave as all branches so it appears in every location filter.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
			</div>
			<div class="lucrsure-field lucrsure-field-check">
				<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-field lucrsure-field-check">
				<label class="lucrsure-check"><input type="checkbox" name="is_popular" value="1" <?php checked( $is_popular ); ?> /> <?php esc_html_e( 'Most popular', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<p class="lucrsure-field-hint"><?php esc_html_e( 'Shows a “Most popular” badge on the frontend pricing page for this category and billing interval.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
			</div>
		</div>

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

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Trial Days', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="number" min="0" name="trial_days" value="<?php echo esc_attr( $trial_days ); ?>" />
			</div>
		</div>

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

		<div class="lucrsure-field">
			<label><?php esc_html_e( 'Features (one per line)', 'lucrativeit-subscription-recurring-billing' ); ?></label>
			<textarea name="features" rows="4" placeholder="Unlimited projects&#10;Priority support"><?php echo esc_textarea( $features ); ?></textarea>
		</div>

		<div class="lucrsure-form-actions">
			<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_url( 'plans' ) ); ?>"><?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 Plan', 'lucrativeit-subscription-recurring-billing' ) : esc_html__( 'Create Plan', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</div>
	</form>
</div>
