<?php
/**
 * Branches 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.

$repo     = new LUCRSURE_Branch_Repository();
$branches = $repo->all( 'name', 'ASC' );

global $wpdb;
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Branches', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Manage regional offices and assign customers to branches.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'branches', array( 'action' => 'new' ) ) ); ?>">&#128666; <?php esc_html_e( 'Add Branch', 'lucrativeit-subscription-recurring-billing' ); ?></a>
</div>

<?php if ( empty( $branches ) ) : ?>
	<div class="lucrsure-card lucrsure-empty-state">
		<p><?php esc_html_e( 'No branches yet. Create your first regional office.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
<?php else : ?>
	<div class="lucrsure-cards-grid">
		<?php
		foreach ( $branches as $branch ) :
			$customer_count = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter
				$wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE branch_id = %d', lucrsure_table( 'customers' ), (int) $branch['id'] )
			);
			$image_url      = LUCRSURE_Branch_Repository::image_url( $branch );
			?>
			<div class="lucrsure-card lucrsure-branch-card">
				<?php if ( $image_url ) : ?>
					<div class="lucrsure-branch-thumb">
						<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $branch['name'] ); ?>" />
					</div>
				<?php endif; ?>
				<div class="lucrsure-branch-top">
					<?php if ( ! $image_url ) : ?>
						<span class="lucrsure-branch-icon">&#128205;</span>
					<?php endif; ?>
					<div class="lucrsure-branch-title">
						<strong><?php echo esc_html( $branch['name'] ); ?></strong>
						<span><?php echo esc_html( $branch['region'] ); ?></span>
					</div>
					<?php if ( $branch['is_main'] ) : ?>
						<span class="lucrsure-pill lucrsure-pill-blue"><?php esc_html_e( 'Main', 'lucrativeit-subscription-recurring-billing' ); ?></span>
					<?php endif; ?>
				</div>
				<div class="lucrsure-branch-body">
					<p class="lucrsure-muted"><?php echo esc_html( $branch['description'] ? $branch['description'] : __( 'Branch', 'lucrativeit-subscription-recurring-billing' ) ); ?></p>
					<p>&#9737; <?php echo esc_html( $branch['address'] ? $branch['address'] : $branch['region'] ); ?></p>
					<?php if ( ! empty( $branch['phone'] ) ) : ?>
						<p>&#128222; <?php echo esc_html( $branch['phone'] ); ?></p>
					<?php endif; ?>
					<?php
					$hours = lucrsure_format_branch_hours(
						isset( $branch['opening_time'] ) ? $branch['opening_time'] : '',
						isset( $branch['closing_time'] ) ? $branch['closing_time'] : ''
					);
					if ( $hours ) :
						?>
						<p>&#128337; <?php echo esc_html( $hours ); ?></p>
					<?php endif; ?>
				</div>
				<div class="lucrsure-branch-foot">
					<span class="lucrsure-muted"><?php echo esc_html( sprintf( /* translators: %d: count */ _n( '%d customer', '%d customers', $customer_count, 'lucrativeit-subscription-recurring-billing' ), $customer_count ) ); ?></span>
					<div class="lucrsure-actions">
						<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'branches', array( 'action' => 'edit', 'id' => $branch['id'] ) ) ); ?>"><?php esc_html_e( 'Edit', 'lucrativeit-subscription-recurring-billing' ); ?></a>
						<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'customers', array( 'branch' => $branch['id'] ), 'lucrsure_filter_customers' ) ); ?>"><?php esc_html_e( 'Details', 'lucrativeit-subscription-recurring-billing' ); ?></a>
					</div>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
<?php endif; ?>
