<?php
/**
 * Manual invoice create 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.

$customer_repo = new LUCRSURE_Customer_Repository();
$customers     = $customer_repo->query( array( 'limit' => 500 ) );
$gateways      = LUCRSURE_Gateway_Manager::instance()->all();
$default_gw    = $settings->get( 'default_gateway', 'manual' );
$currency      = $settings->get( 'default_currency', 'USD' );
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Create Invoice', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Generate a one-off invoice for a customer.', '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_invoice' ); ?>
		<input type="hidden" name="action" value="lucrsure_save_invoice" />

		<div class="lucrsure-form-row">
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Customer', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="customer_id" required>
					<option value=""><?php esc_html_e( 'Select customer', 'lucrativeit-subscription-recurring-billing' ); ?></option>
					<?php foreach ( $customers as $customer ) : ?>
						<option value="<?php echo esc_attr( $customer['id'] ); ?>"><?php echo esc_html( $customer['name'] . ' (' . $customer['email'] . ')' ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Amount', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="number" step="0.01" min="0" name="amount" value="0.00" required />
			</div>
		</div>

		<div class="lucrsure-form-row">
			<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 class="lucrsure-field">
				<label><?php esc_html_e( 'Payment Gateway', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<select name="gateway">
					<?php foreach ( $gateways as $gw ) : ?>
						<option value="<?php echo esc_attr( $gw->get_id() ); ?>" <?php selected( $default_gw, $gw->get_id() ); ?>><?php echo esc_html( $gw->get_title() ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
		</div>

		<div class="lucrsure-form-actions">
			<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_url( 'billing' ) ); ?>"><?php esc_html_e( 'Cancel', 'lucrativeit-subscription-recurring-billing' ); ?></a>
			<button type="submit" class="lucrsure-btn lucrsure-btn-primary"><?php esc_html_e( 'Create & Send Invoice', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</div>
	</form>
</div>
