<?php
/**
 * Invoice detail / edit view.
 *
 * @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_Invoice_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'] ) );
}

$inv = $id ? $repo->find_with_customer( $id ) : null;

if ( ! $inv ) {
	echo '<div class="lucrsure-card"><p class="lucrsure-empty">' . esc_html__( 'Invoice not found.', 'lucrativeit-subscription-recurring-billing' ) . '</p></div>';
	return;
}

$gateways   = LUCRSURE_Gateway_Manager::instance()->all();
$statuses   = lucrsure_invoice_statuses();
$paid_at    = ! empty( $inv['paid_at'] ) ? gmdate( 'Y-m-d\TH:i', strtotime( $inv['paid_at'] ) ) : '';
$due_date   = ! empty( $inv['due_date'] ) ? gmdate( 'Y-m-d', strtotime( $inv['due_date'] ) ) : '';
$line_items = ! empty( $inv['line_items'] ) ? json_decode( $inv['line_items'], true ) : array();
?>

<div class="lucrsure-page-head">
	<div>
		<p class="lucrsure-muted lucrsure-mb-0-4">
			<a href="<?php echo esc_url( lucrsure_admin_url( 'billing' ) ); ?>">&larr; <?php esc_html_e( 'Back to Invoices', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		</p>
		<h1><?php echo esc_html( $inv['invoice_number'] ); ?></h1>
		<p class="lucrsure-sub">
			<?php
			printf(
				/* translators: %s: formatted created date */
				esc_html__( 'Created %s', 'lucrativeit-subscription-recurring-billing' ),
				esc_html( $inv['created_at'] ? date_i18n( 'M j, Y g:i a', strtotime( $inv['created_at'] ) ) : '—' )
			);
			?>
		</p>
	</div>
	<span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $inv['status'] ); ?>"><?php echo esc_html( isset( $statuses[ $inv['status'] ] ) ? $statuses[ $inv['status'] ] : $inv['status'] ); ?></span>
</div>

<div class="lucrsure-grid-2">
	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Customer', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
		</div>
		<dl class="lucrsure-dl">
			<dt><?php esc_html_e( 'Name', 'lucrativeit-subscription-recurring-billing' ); ?></dt>
			<dd>
				<?php if ( ! empty( $inv['customer_id'] ) ) : ?>
					<a href="<?php echo esc_url( lucrsure_admin_nonce_url( 'customers', array( 'action' => 'edit', 'id' => $inv['customer_id'] ) ) ); ?>"><?php echo esc_html( $inv['customer_name'] ); ?></a>
				<?php else : ?>
					<?php echo esc_html( $inv['customer_name'] ?: '—' ); ?>
				<?php endif; ?>
			</dd>
			<dt><?php esc_html_e( 'Email', 'lucrativeit-subscription-recurring-billing' ); ?></dt>
			<dd><?php echo esc_html( $inv['customer_email'] ?: '—' ); ?></dd>
			<?php if ( ! empty( $inv['customer_company'] ) ) : ?>
				<dt><?php esc_html_e( 'Company', 'lucrativeit-subscription-recurring-billing' ); ?></dt>
				<dd><?php echo esc_html( $inv['customer_company'] ); ?></dd>
			<?php endif; ?>
			<?php
			$billing_display = lucrsure_format_billing_address( $inv );
			if ( '' !== $billing_display ) :
				?>
				<dt><?php esc_html_e( 'Billing address', 'lucrativeit-subscription-recurring-billing' ); ?></dt>
				<dd><?php echo nl2br( esc_html( $billing_display ) ); ?></dd>
			<?php endif; ?>
			<?php if ( ! empty( $inv['subscription_id'] ) ) : ?>
				<dt><?php esc_html_e( 'Subscription', 'lucrativeit-subscription-recurring-billing' ); ?></dt>
				<dd>
					<a href="<?php echo esc_url( lucrsure_admin_nonce_url( 'subscriptions', array( 'action' => 'edit', 'id' => $inv['subscription_id'] ) ) ); ?>">
						<?php
						printf(
							/* translators: %d: subscription ID */
							esc_html__( 'Subscription #%d', 'lucrativeit-subscription-recurring-billing' ),
							(int) $inv['subscription_id']
						);
						?>
					</a>
				</dd>
			<?php endif; ?>
		</dl>

		<?php if ( ! empty( $line_items ) && is_array( $line_items ) ) : ?>
			<div class="lucrsure-card-head lucrsure-mt-20">
				<h2><?php esc_html_e( 'Line Items', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
			</div>
			<table class="lucrsure-mini-table">
				<?php foreach ( $line_items as $item ) : ?>
					<tr>
						<td><?php echo esc_html( isset( $item['description'] ) ? $item['description'] : ( isset( $item['name'] ) ? $item['name'] : '' ) ); ?></td>
						<td class="lucrsure-right"><?php echo esc_html( isset( $item['amount'] ) ? lucrsure_format_money( $item['amount'], $inv['currency'] ) : '' ); ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>

	<div class="lucrsure-card lucrsure-form-card lucrsure-max-w-none">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Payment Details', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
		</div>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( 'lucrsure_update_invoice' ); ?>
			<input type="hidden" name="action" value="lucrsure_update_invoice" />
			<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( 'Payment Status', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<select name="status">
						<?php foreach ( $statuses as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $inv['status'], $key ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Payment Method', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<select name="gateway">
						<?php foreach ( $gateways as $gw ) : ?>
							<option value="<?php echo esc_attr( $gw->get_id() ); ?>" <?php selected( $inv['gateway'], $gw->get_id() ); ?>><?php echo esc_html( $gw->get_title() ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>

			<div class="lucrsure-form-row">
				<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="<?php echo esc_attr( $inv['amount'] ); ?>" 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( $inv['currency'] ); ?>" />
				</div>
			</div>

			<div class="lucrsure-form-row">
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Due Date', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="date" name="due_date" value="<?php echo esc_attr( $due_date ); ?>" />
				</div>
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Paid At', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="datetime-local" name="paid_at" value="<?php echo esc_attr( $paid_at ); ?>" />
				</div>
			</div>

			<div class="lucrsure-form-row">
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Transaction / Payment ID', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="text" name="gateway_payment_id" value="<?php echo esc_attr( $inv['gateway_payment_id'] ); ?>" placeholder="<?php esc_attr_e( 'e.g. Stripe session or bank reference', 'lucrativeit-subscription-recurring-billing' ); ?>" />
				</div>
			</div>

			<div class="lucrsure-form-row">
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Payment URL', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="url" name="payment_url" value="<?php echo esc_attr( $inv['payment_url'] ); ?>" placeholder="https://" />
					<?php if ( ! empty( $inv['payment_url'] ) ) : ?>
						<p class="lucrsure-muted lucrsure-mt-6">
							<a href="<?php echo esc_url( $inv['payment_url'] ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Open payment link', 'lucrativeit-subscription-recurring-billing' ); ?></a>
						</p>
					<?php endif; ?>
				</div>
			</div>

			<h3 class="lucrsure-form-section-title"><?php esc_html_e( 'Billing Address', 'lucrativeit-subscription-recurring-billing' ); ?></h3>
			<?php $billing = lucrsure_sanitize_billing_address( $inv ); ?>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Street address', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_address_1" value="<?php echo esc_attr( $billing['billing_address_1'] ); ?>" />
			</div>
			<div class="lucrsure-field">
				<label><?php esc_html_e( 'Apartment, suite, etc.', 'lucrativeit-subscription-recurring-billing' ); ?></label>
				<input type="text" name="billing_address_2" value="<?php echo esc_attr( $billing['billing_address_2'] ); ?>" />
			</div>
			<div class="lucrsure-form-row">
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'City', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="text" name="billing_city" value="<?php echo esc_attr( $billing['billing_city'] ); ?>" />
				</div>
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'State / Province', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="text" name="billing_state" value="<?php echo esc_attr( $billing['billing_state'] ); ?>" />
				</div>
			</div>
			<div class="lucrsure-form-row">
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Postal / ZIP code', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="text" name="billing_postcode" value="<?php echo esc_attr( $billing['billing_postcode'] ); ?>" />
				</div>
				<div class="lucrsure-field">
					<label><?php esc_html_e( 'Country', 'lucrativeit-subscription-recurring-billing' ); ?></label>
					<input type="text" name="billing_country" value="<?php echo esc_attr( $billing['billing_country'] ); ?>" />
				</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( 'Save Changes', 'lucrativeit-subscription-recurring-billing' ); ?></button>
			</div>
		</form>
	</div>
</div>

<div class="lucrsure-card lucrsure-mt-20">
	<div class="lucrsure-card-head">
		<h2><?php esc_html_e( 'Actions', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
	</div>
	<div class="lucrsure-row-actions lucrsure-p-row-actions">
		<?php if ( 'paid' !== $inv['status'] ) : ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-inline-form">
				<?php wp_nonce_field( 'lucrsure_invoice_action' ); ?>
				<input type="hidden" name="action" value="lucrsure_invoice_action" />
				<input type="hidden" name="do" value="paid" />
				<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
				<input type="hidden" name="return_view" value="1" />
				<button type="submit" class="lucrsure-btn lucrsure-btn-primary"><?php esc_html_e( 'Mark as Paid', 'lucrativeit-subscription-recurring-billing' ); ?></button>
			</form>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-inline-form">
				<?php wp_nonce_field( 'lucrsure_invoice_action' ); ?>
				<input type="hidden" name="action" value="lucrsure_invoice_action" />
				<input type="hidden" name="do" value="resend" />
				<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
				<input type="hidden" name="return_view" value="1" />
				<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Resend Payment Link', 'lucrativeit-subscription-recurring-billing' ); ?></button>
			</form>
		<?php endif; ?>
		<?php if ( lucrsure_is_pro() ) : ?>
			<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=lucrsure_download_invoice_pdf&id=' . (int) $id ), 'lucrsure_download_invoice_pdf' ) ); ?>"><?php esc_html_e( 'Download PDF', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		<?php endif; ?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-inline-form lucrsure-confirm-submit" data-lucrsure-confirm="<?php echo esc_attr__( 'Delete this invoice permanently?', 'lucrativeit-subscription-recurring-billing' ); ?>">
			<?php wp_nonce_field( 'lucrsure_invoice_action' ); ?>
			<input type="hidden" name="action" value="lucrsure_invoice_action" />
			<input type="hidden" name="do" value="delete" />
			<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
			<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Delete Invoice', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</form>
	</div>
</div>
