<?php
/**
 * Shift management template.
 *
 * @package MerchantKits\AttendanceManager
 *
 * @var array $shifts All shift rows.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$shifts   = isset( $shifts ) ? $shifts : array();
$day_map  = array(
	1 => __( 'Mon', 'merchantkits-attendance-manager' ),
	2 => __( 'Tue', 'merchantkits-attendance-manager' ),
	3 => __( 'Wed', 'merchantkits-attendance-manager' ),
	4 => __( 'Thu', 'merchantkits-attendance-manager' ),
	5 => __( 'Fri', 'merchantkits-attendance-manager' ),
	6 => __( 'Sat', 'merchantkits-attendance-manager' ),
	7 => __( 'Sun', 'merchantkits-attendance-manager' ),
);
?>
<div class="wrap">
	<h1 class="wp-heading-inline"><?php esc_html_e( 'Shifts', 'merchantkits-attendance-manager' ); ?></h1>
	<span class="mkam-badge mkam-badge--pending"><?php esc_html_e( 'Shift calendar — Pro', 'merchantkits-attendance-manager' ); ?></span>
	<hr class="wp-header-end" />

	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Name', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Start', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'End', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Grace', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Break', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Working Days', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Overnight', 'merchantkits-attendance-manager' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $shifts ) ) : ?>
				<tr><td colspan="7"><?php esc_html_e( 'No shifts found.', 'merchantkits-attendance-manager' ); ?></td></tr>
			<?php else : ?>
				<?php foreach ( $shifts as $shift ) : ?>
					<?php
					$days       = array_filter( array_map( 'intval', explode( ',', $shift['working_days'] ) ) );
					$day_labels = array();
					foreach ( $days as $d ) {
						if ( isset( $day_map[ $d ] ) ) {
							$day_labels[] = $day_map[ $d ];
						}
					}
					?>
					<tr>
						<td><strong><?php echo esc_html( $shift['name'] ); ?></strong></td>
						<td><?php echo esc_html( $shift['start_time'] ); ?></td>
						<td><?php echo esc_html( $shift['end_time'] ); ?></td>
						<td><?php echo esc_html( $shift['grace_minutes'] ); ?> <?php esc_html_e( 'min', 'merchantkits-attendance-manager' ); ?></td>
						<td><?php echo esc_html( $shift['break_minutes'] ); ?> <?php esc_html_e( 'min', 'merchantkits-attendance-manager' ); ?></td>
						<td><?php echo esc_html( implode( ', ', $day_labels ) ); ?></td>
						<td><?php echo $shift['is_overnight'] ? esc_html__( 'Yes', 'merchantkits-attendance-manager' ) : esc_html__( 'No', 'merchantkits-attendance-manager' ); ?></td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
