<?php
/**
 * Employee self-service attendance calendar.
 *
 * @package MerchantKits\AttendanceManager
 *
 * @var array $employee Employee row.
 * @var int   $month    Selected month (1-12).
 * @var int   $year     Selected year.
 * @var array $by_date  Attendance rows keyed by Y-m-d.
 */

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

$first_ts     = strtotime( sprintf( '%04d-%02d-01', $year, $month ) );
$days_in_month = (int) gmdate( 't', $first_ts );
$start_weekday = (int) gmdate( 'N', $first_ts ); // 1 (Mon) - 7 (Sun).

$totals = array(
	'present'  => 0,
	'absent'   => 0,
	'late'     => 0,
	'on_leave' => 0,
	'hours'    => 0.0,
	'overtime' => 0.0,
);

foreach ( $by_date as $record ) {
	if ( isset( $totals[ $record['status'] ] ) ) {
		$totals[ $record['status'] ]++;
	}
	$totals['hours']    += (float) $record['total_hours'];
	$totals['overtime'] += (float) $record['overtime_hours'];
}

$prev_month = $month - 1;
$prev_year  = $year;
if ( $prev_month < 1 ) {
	$prev_month = 12;
	$prev_year--;
}
$next_month = $month + 1;
$next_year  = $year;
if ( $next_month > 12 ) {
	$next_month = 1;
	$next_year++;
}

$base = get_permalink();
$prev_url = add_query_arg(
	array(
		'mkam_month' => $prev_month,
		'mkam_year'  => $prev_year,
	),
	$base
);
$next_url = add_query_arg(
	array(
		'mkam_month' => $next_month,
		'mkam_year'  => $next_year,
	),
	$base
);

$weekdays = array(
	__( 'Mon', 'merchantkits-attendance-manager' ),
	__( 'Tue', 'merchantkits-attendance-manager' ),
	__( 'Wed', 'merchantkits-attendance-manager' ),
	__( 'Thu', 'merchantkits-attendance-manager' ),
	__( 'Fri', 'merchantkits-attendance-manager' ),
	__( 'Sat', 'merchantkits-attendance-manager' ),
	__( 'Sun', 'merchantkits-attendance-manager' ),
);
?>
<div class="mkam-widget mkam-attendance">
	<div class="mkam-attendance__nav">
		<a class="mkam-btn mkam-btn--small" href="<?php echo esc_url( $prev_url ); ?>">&larr;</a>
		<span class="mkam-attendance__title"><?php echo esc_html( date_i18n( 'F Y', $first_ts ) ); ?></span>
		<a class="mkam-btn mkam-btn--small" href="<?php echo esc_url( $next_url ); ?>">&rarr;</a>
	</div>

	<div class="mkam-calendar" role="grid">
		<?php foreach ( $weekdays as $wd ) : ?>
			<div class="mkam-calendar__head"><?php echo esc_html( $wd ); ?></div>
		<?php endforeach; ?>

		<?php for ( $i = 1; $i < $start_weekday; $i++ ) : ?>
			<div class="mkam-calendar__cell mkam-calendar__cell--empty"></div>
		<?php endfor; ?>

		<?php for ( $day = 1; $day <= $days_in_month; $day++ ) : ?>
			<?php
			$date   = sprintf( '%04d-%02d-%02d', $year, $month, $day );
			$record = isset( $by_date[ $date ] ) ? $by_date[ $date ] : null;
			$status = $record ? $record['status'] : '';
			$title  = $record ? ucwords( str_replace( '_', ' ', $status ) ) : '';
			?>
			<div class="mkam-calendar__cell mkam-calendar__cell--<?php echo esc_attr( $status ? $status : 'none' ); ?>" title="<?php echo esc_attr( $title ); ?>">
				<span class="mkam-calendar__day"><?php echo esc_html( $day ); ?></span>
				<?php if ( $record && $record['clock_in'] ) : ?>
					<span class="mkam-calendar__time"><?php echo esc_html( mysql2date( get_option( 'time_format', 'g:i a' ), $record['clock_in'] ) ); ?></span>
				<?php endif; ?>
			</div>
		<?php endfor; ?>
	</div>

	<div class="mkam-attendance__summary">
		<span><?php esc_html_e( 'Present', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( $totals['present'] ); ?></strong></span>
		<span><?php esc_html_e( 'Absent', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( $totals['absent'] ); ?></strong></span>
		<span><?php esc_html_e( 'Late', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( $totals['late'] ); ?></strong></span>
		<span><?php esc_html_e( 'On Leave', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( $totals['on_leave'] ); ?></strong></span>
		<span><?php esc_html_e( 'Total Hours', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( number_format_i18n( $totals['hours'], 2 ) ); ?></strong></span>
		<span><?php esc_html_e( 'Overtime', 'merchantkits-attendance-manager' ); ?>: <strong><?php echo esc_html( number_format_i18n( $totals['overtime'], 2 ) ); ?></strong></span>
	</div>
</div>
