<?php
/**
 * Employee self-service leave dashboard.
 *
 * @package MerchantKits\AttendanceManager
 *
 * @var array $employee Employee row.
 * @var array $policies Active leave policies.
 * @var array $leaves   Leave rows for the selected year.
 * @var int   $year     Selected year.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="mkam-widget mkam-my-leaves">
	<h3 class="mkam-widget__title"><?php esc_html_e( 'My Leave', 'merchantkits-attendance-manager' ); ?></h3>

	<div class="mkam-leave-balances">
		<?php foreach ( $policies as $policy ) : ?>
			<?php $balance = MKAM_Leave::get_balance( $employee['id'], $policy['leave_type'], $year ); ?>
			<div class="mkam-balance-card">
				<span class="mkam-balance-card__value"><?php echo esc_html( $balance['used'] ); ?> / <?php echo esc_html( $balance['entitlement'] ); ?></span>
				<span class="mkam-balance-card__label"><?php echo esc_html( $policy['label'] ); ?></span>
			</div>
		<?php endforeach; ?>
	</div>

	<table class="mkam-table">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Type', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Dates', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Days', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Status', 'merchantkits-attendance-manager' ); ?></th>
				<th><?php esc_html_e( 'Applied', 'merchantkits-attendance-manager' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $leaves ) ) : ?>
				<tr><td colspan="6"><?php esc_html_e( 'No leave requests for this year.', 'merchantkits-attendance-manager' ); ?></td></tr>
			<?php else : ?>
				<?php foreach ( $leaves as $leave ) : ?>
					<?php $policy = MKAM_Leave::get_policy( $leave['leave_type'] ); ?>
					<tr>
						<td><?php echo esc_html( $policy ? $policy['label'] : $leave['leave_type'] ); ?></td>
						<td><?php echo esc_html( $leave['start_date'] . ' → ' . $leave['end_date'] ); ?></td>
						<td><?php echo esc_html( $leave['total_days'] ); ?></td>
						<td><span class="mkam-badge mkam-badge--<?php echo esc_attr( $leave['status'] ); ?>"><?php echo esc_html( ucwords( $leave['status'] ) ); ?></span></td>
						<td><?php echo esc_html( mysql2date( get_option( 'date_format', 'Y-m-d' ), $leave['created_at'] ) ); ?></td>
						<td>
							<?php if ( 'pending' === $leave['status'] ) : ?>
								<button type="button" class="mkam-btn mkam-btn--small mkam-cancel-leave" data-leave-id="<?php echo esc_attr( $leave['id'] ); ?>"><?php esc_html_e( 'Cancel', 'merchantkits-attendance-manager' ); ?></button>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
