<?php
/**
 * Billing & invoices list view.
 *
 * @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_Invoice_Repository();
$status = '';

if ( isset( $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_filter_billing' )
) {
	$status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
}

$invoices = $repo->query( array( 'status' => $status ) );
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Billing & Invoices', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Generate, track, and manage customer invoices.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<?php /*
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'billing', array( 'action' => 'new' ) ) ); ?>">+ <?php esc_html_e( 'Create Invoice', 'lucrativeit-subscription-recurring-billing' ); ?></a>
	*/ ?>
</div>

<form class="lucrsure-card lucrsure-toolbar" method="get">
	<input type="hidden" name="page" value="lucrsure-billing" />
	<?php wp_nonce_field( 'lucrsure_filter_billing', '_wpnonce', false ); ?>
	<select name="status" class="lucrsure-auto-submit">
		<option value=""><?php esc_html_e( 'Status: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<?php foreach ( lucrsure_invoice_statuses() as $key => $label ) : ?>
			<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $status, $key ); ?>><?php echo esc_html( $label ); ?></option>
		<?php endforeach; ?>
	</select>
	<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Filter', 'lucrativeit-subscription-recurring-billing' ); ?></button>
</form>

<div class="lucrsure-card lucrsure-table-card">
	<table class="lucrsure-table">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Invoice', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Customer', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Amount', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Due Date', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Status', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $invoices ) ) : ?>
				<tr><td colspan="6" class="lucrsure-empty"><?php esc_html_e( 'No invoices found.', 'lucrativeit-subscription-recurring-billing' ); ?></td></tr>
			<?php else : ?>
				<?php foreach ( $invoices as $inv ) : ?>
					<tr>
						<td>
							<a href="<?php echo esc_url( lucrsure_admin_nonce_url( 'billing', array( 'action' => 'view', 'id' => $inv['id'] ) ) ); ?>"><strong><?php echo esc_html( $inv['invoice_number'] ); ?></strong></a>
						</td>
						<td><?php echo esc_html( $inv['customer_name'] ); ?></td>
						<td><?php echo esc_html( lucrsure_format_money( $inv['amount'], $inv['currency'] ) ); ?></td>
						<td><?php echo esc_html( $inv['due_date'] ? date_i18n( 'M j, Y', strtotime( $inv['due_date'] ) ) : '—' ); ?></td>
						<td><span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $inv['status'] ); ?>"><?php echo esc_html( $inv['status'] ); ?></span></td>
						<td class="lucrsure-right">
							<div class="lucrsure-row-actions">
								<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'billing', array( 'action' => 'view', 'id' => $inv['id'] ) ) ); ?>"><?php esc_html_e( 'View', 'lucrativeit-subscription-recurring-billing' ); ?></a>
								<?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( $inv['id'] ); ?>" />
										<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Mark Paid', 'lucrativeit-subscription-recurring-billing' ); ?></button>
									</form>
									<?php if ( ! empty( $inv['payment_url'] ) ) : ?>
										<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( $inv['payment_url'] ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Pay Link', 'lucrativeit-subscription-recurring-billing' ); ?></a>
									<?php endif; ?>
								<?php endif; ?>
							</div>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
