<?php
/**
 * Single leave request detail template.
 *
 * @package MerchantKits\AttendanceManager
 *
 * @var array $leave Leave row.
 */

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

if ( empty( $leave ) ) {
	echo '<div class="wrap"><p>' . esc_html__( 'Leave request not found.', 'merchantkits-attendance-manager' ) . '</p></div>';
	return;
}

$employee = MKAM_Employee::get( $leave['employee_id'] );
$policy   = MKAM_Leave::get_policy( $leave['leave_type'] );
$balance  = MKAM_Leave::get_balance( $leave['employee_id'], $leave['leave_type'] );

$approve_url = wp_nonce_url( admin_url( 'admin.php?page=mkam-leaves&action=approve&id=' . $leave['id'] ), 'mkam_leave_approve_' . $leave['id'] );
$reject_url  = wp_nonce_url( admin_url( 'admin.php?page=mkam-leaves&action=reject&id=' . $leave['id'] ), 'mkam_leave_reject_' . $leave['id'] );
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Leave Request Detail', 'merchantkits-attendance-manager' ); ?></h1>
	<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=mkam-leaves' ) ); ?>">&larr; <?php esc_html_e( 'Back to list', 'merchantkits-attendance-manager' ); ?></a>

	<table class="widefat striped" style="margin-top:1em;max-width:640px;">
		<tbody>
			<tr><th style="width:200px;"><?php esc_html_e( 'Employee', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $employee ? MKAM_Employee::full_name( $employee ) : '—' ); ?></td></tr>
			<tr><th><?php esc_html_e( 'Leave Type', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $policy ? $policy['label'] : $leave['leave_type'] ); ?></td></tr>
			<tr><th><?php esc_html_e( 'From', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $leave['start_date'] ); ?></td></tr>
			<tr><th><?php esc_html_e( 'To', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $leave['end_date'] ); ?></td></tr>
			<tr><th><?php esc_html_e( 'Total Days', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $leave['total_days'] ); ?></td></tr>
			<tr><th><?php esc_html_e( 'Half Day', 'merchantkits-attendance-manager' ); ?></th><td><?php echo $leave['is_half_day'] ? esc_html( ucwords( str_replace( '_', ' ', (string) $leave['half_day_period'] ) ) ) : esc_html__( 'No', 'merchantkits-attendance-manager' ); ?></td></tr>
			<tr><th><?php esc_html_e( 'Reason', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $leave['reason'] ); ?></td></tr>
			<tr><th><?php esc_html_e( 'Status', 'merchantkits-attendance-manager' ); ?></th><td><span class="mkam-badge mkam-badge--<?php echo esc_attr( $leave['status'] ); ?>"><?php echo esc_html( ucwords( $leave['status'] ) ); ?></span></td></tr>
			<tr><th><?php esc_html_e( 'Current Balance', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $balance['balance'] ); ?></td></tr>
			<?php if ( ! empty( $leave['review_note'] ) ) : ?>
				<tr><th><?php esc_html_e( 'Review Note', 'merchantkits-attendance-manager' ); ?></th><td><?php echo esc_html( $leave['review_note'] ); ?></td></tr>
			<?php endif; ?>
		</tbody>
	</table>

	<?php if ( 'pending' === $leave['status'] ) : ?>
		<p style="margin-top:1em;">
			<a class="button button-primary" href="<?php echo esc_url( $approve_url ); ?>"><?php esc_html_e( 'Approve', 'merchantkits-attendance-manager' ); ?></a>
			<a class="button" href="<?php echo esc_url( $reject_url ); ?>"><?php esc_html_e( 'Reject', 'merchantkits-attendance-manager' ); ?></a>
		</p>
	<?php endif; ?>
</div>
