<?php
/**
 * Customers 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_Customer_Repository();
$invoice_repo = new LUCRSURE_Invoice_Repository();
$subs_repo    = new LUCRSURE_Subscription_Repository();
$branch_repo  = new LUCRSURE_Branch_Repository();

$search    = '';
$status    = '';
$branch_id = 0;

if ( isset( $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_filter_customers' )
) {
	$search    = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
	$status    = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
	$branch_id = isset( $_GET['branch'] ) ? absint( wp_unslash( $_GET['branch'] ) ) : 0;
}

$customers = $repo->query(
	array(
		'search'    => $search,
		'status'    => $status,
		'branch_id' => $branch_id,
	)
);

$branches      = $branch_repo->active();
$total_revenue = $invoice_repo->total_paid();
$trials        = $subs_repo->count_status( 'trialing' );
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Customers', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Manage your subscriber base, billing details, and active plans.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'customers', array( 'action' => 'new' ) ) ); ?>">&#128100; <?php esc_html_e( 'Add Customer', 'lucrativeit-subscription-recurring-billing' ); ?></a>
</div>

<div class="lucrsure-kpis lucrsure-kpis-3">
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'TOTAL REVENUE', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( lucrsure_format_money( $total_revenue ) ); ?></strong>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'TOTAL CUSTOMERS', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( number_format_i18n( $repo->count() ) ); ?></strong>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'ACTIVE TRIALS', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( number_format_i18n( $trials ) ); ?></strong>
	</div>
</div>

<form class="lucrsure-card lucrsure-toolbar" method="get">
	<input type="hidden" name="page" value="lucrsure-customers" />
	<?php wp_nonce_field( 'lucrsure_filter_customers', '_wpnonce', false ); ?>
	<div class="lucrsure-search lucrsure-search-wide">
		<span class="lucrsure-search-icon" aria-hidden="true">&#128269;</span>
		<input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Filter by name, email, or ID...', 'lucrativeit-subscription-recurring-billing' ); ?>" />
	</div>
	<select name="status" class="lucrsure-auto-submit">
		<option value=""><?php esc_html_e( 'Status: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<option value="active" <?php selected( $status, 'active' ); ?>><?php esc_html_e( 'Active', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<option value="inactive" <?php selected( $status, 'inactive' ); ?>><?php esc_html_e( 'Inactive', 'lucrativeit-subscription-recurring-billing' ); ?></option>
	</select>
	<select name="branch" class="lucrsure-auto-submit">
		<option value="0"><?php esc_html_e( 'Branch: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<?php foreach ( $branches as $branch ) : ?>
			<option value="<?php echo esc_attr( $branch['id'] ); ?>" <?php selected( $branch_id, $branch['id'] ); ?>><?php echo esc_html( $branch['name'] ); ?></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( 'Customer', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Branch', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Company', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Status', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $customers ) ) : ?>
				<tr><td colspan="5" class="lucrsure-empty"><?php esc_html_e( 'No customers found.', 'lucrativeit-subscription-recurring-billing' ); ?></td></tr>
			<?php else : ?>
				<?php
				foreach ( $customers as $customer ) :
					$branch = $customer['branch_id'] ? $branch_repo->find( $customer['branch_id'] ) : null;
					?>
					<tr>
						<td>
							<div class="lucrsure-cell-user">
								<span class="lucrsure-avatar"><?php echo esc_html( strtoupper( substr( $customer['name'], 0, 2 ) ) ); ?></span>
								<div>
									<strong><?php echo esc_html( $customer['name'] ); ?></strong>
									<span class="lucrsure-muted"><?php echo esc_html( $customer['email'] ); ?></span>
								</div>
							</div>
						</td>
						<td><?php echo esc_html( $branch ? $branch['name'] : '—' ); ?></td>
						<td><?php echo esc_html( $customer['company'] ? $customer['company'] : '—' ); ?></td>
						<td><span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $customer['status'] ); ?>"><?php echo esc_html( $customer['status'] ); ?></span></td>
						<td class="lucrsure-right">
							<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'customers', array( 'action' => 'edit', 'id' => $customer['id'] ) ) ); ?>"><?php esc_html_e( 'Edit', 'lucrativeit-subscription-recurring-billing' ); ?></a>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
