<?php
/**
 * Manage Channels View Class
 *
 * @package MOMCSAmazonSync
 * @since 1.0.0
 */

declare( strict_types=1 );

namespace MOMCS\AmazonSync\Views\Supplier;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use MOMCS\AmazonSync\Common\Models\Multichannels_Model;
use MOMCS\AmazonSync\Views\Components\Header_View;

/**
 * Render the manage channels view.
 */
class Manage_Channels_View {
	/**
	 * Render the view.
	 *
	 * @return void
	 */
	public function render(): void {
		$channels = Multichannels_Model::get_all();
		?>
		<div class="wrap">
			<?php ( new Header_View( 'connect' ) )->render(); ?>
			
			<div class="momcs-connect-container">
				<p><?php esc_html_e( 'Manage your marketplace integrations below.', 'multi-channel-for-woocommerce' ); ?></p>
				
				<table class="widefat">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Name', 'multi-channel-for-woocommerce' ); ?></th>
							<th><?php esc_html_e( 'Type', 'multi-channel-for-woocommerce' ); ?></th>
							<th><?php esc_html_e( 'Region', 'multi-channel-for-woocommerce' ); ?></th>
							<th><?php esc_html_e( 'Status', 'multi-channel-for-woocommerce' ); ?></th>
							<th><?php esc_html_e( 'Actions', 'multi-channel-for-woocommerce' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $channels as $channel ) : ?>
							<tr>
								<td><?php echo esc_html( $channel->name ); ?></td>
								<td><?php echo esc_html( ucfirst( $channel->type ) ); ?></td>
								<td><?php echo esc_html( $channel->region ); ?></td>
								<td>
									<span class="status-<?php echo esc_attr( $channel->status ); ?>">
										<?php echo esc_html( ucfirst( $channel->status ) ); ?>
									</span>
								</td>
								<td>
									<a href="?page=amazon-sync-dashboard&action=edit&id=<?php echo esc_attr( (string) $channel->id ); ?>" class="button">
										<?php esc_html_e( 'Edit', 'multi-channel-for-woocommerce' ); ?>
									</a>
									<a href="?page=amazon-sync-dashboard&action=delete&id=<?php echo esc_attr( (string) $channel->id ); ?>" class="button" onclick="return confirm('<?php esc_attr_e( 'Are you sure you want to delete this integration?', 'multi-channel-for-woocommerce' ); ?>');">
										<?php esc_html_e( 'Delete', 'multi-channel-for-woocommerce' ); ?>
									</a>
								</td>
							</tr>
						<?php endforeach; ?>
						
						<?php if ( empty( $channels ) ) : ?>
							<tr>
								<td colspan="5"><?php esc_html_e( 'No integrations found.', 'multi-channel-for-woocommerce' ); ?></td>
							</tr>
						<?php endif; ?>
					</tbody>
				</table>
				
				<p>
					<a href="?page=amazon-sync-dashboard&action=add" class="button button-primary">
						<?php esc_html_e( 'Add New Integration', 'multi-channel-for-woocommerce' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}
}
