<?php
/**
 * Template to display Apply Leave Form.
 *
 * @package voidempo
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! is_user_logged_in() ) {
	wp_safe_redirect( wp_login_url() );
	exit;
}
?>

<div class="mt-4">
<div style="display: flex; justify-content: space-between;">
	<div style="width: 70%; ">
	<h3 style="text-align:center;">Apply Leave </h3>   
		<form class="leave-form" id="leave-form" method="post">
		<div style=" display: flex;column-gap: 20px; padding: 15px;">
			<div style="width:100%; background-color: #f1eded; padding:15px;">
			<div class="form-group ">
				<label for="employee">Employee</label>
				<?php
				$current_user        = wp_get_current_user();
				$voidempo_is_admin_or_manager  = current_user_can( 'manage_options' ) || current_user_can( 'voidempo_manager_access' );

				if ( $voidempo_is_admin_or_manager  ) {
					?>
					<select id="employee" name="employee" required>
							<option value="">Select User</option>
								<?php
								$voidempo_users = get_users();
								foreach ( $voidempo_users as $voidempo_user ) {
									$voidempo_is_disabled = get_user_meta( $voidempo_user-> ID, '_employee_disabled', true );

									if ( '1' != $voidempo_is_disabled ) {
										echo "<option value='" . esc_attr( $voidempo_user-> ID ) . "'>" . esc_html( $voidempo_user-> display_name ) . '</option>';

									}
								}
								?>
						</select>

					<?php
				} else {
					echo "<input type='text' id='employee' name='employee' value='" . esc_attr( $current_user->display_name ) . "' readonly>";
				}
				?>
			</div>

			<div class="form-group " >
				<label for="leave-type">Leave Type</label>
				<select id="leave-type" name="leave-type" required >
					<option value="">Choose</option>
					<option value="first-half">First Half Leave</option>
					<option value="second-half">Second Half Leave</option>
					<option value="full-day">Full Day</option>
				</select>
			</div>
			</div>
			<div style="width:100%; background-color: #f1eded; padding:15px;">
			<div class="form-group ">
				<label for="from-date">From Date</label>
				<input type="date" id="from-date" name="from-date" required  style="border: solid 1px;">
				<input type="hidden" id="selected_month" name="selected_month_" value="">
			</div>

			<div class="form-group " id="to-date-group">
				<label for="to-date">To Date</label>
				<input type="date" id="to-date" name="to-date" style="border: solid 1px;">
			</div>
			</div>
			<div style="width:100%; background-color: #f1eded; padding:15px;">
			<div class="form-group ">
				<label for="reason">Reason for leave</label>
				<textarea id="reason" name="reason" required style="height: 120px; border: solid 1px;"></textarea>
			</div>
			</div>
			</div>
				<button type="submit" class="submit testsagar">Submit</button>
		</form>
	</div>
			<?php
				do_action( 'voidempo_render_calculate_leave_form' );
			?>
			</div>
</br>

<div id="filterContainer">
	<?php if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) { ?>
	<div class="filterGroup">
		<label for="userFilter">Filter by User:</label>
		<select id="userFilter">
			<option value="">All Users</option>
			<?php
			$voidempo_users = get_users();
			foreach ( $voidempo_users as $voidempo_user ) {
				$voidempo_is_disabled = get_user_meta( $voidempo_user->ID, '_employee_disabled', true );
				if ( $voidempo_is_disabled != '1' ) {
					echo "<option value='" . esc_attr( $voidempo_user->ID ) . "'>" . esc_attr( $voidempo_user->display_name ) . '</option>';
				}
			}
			?>
		</select>
	</div>
	<?php } ?>
	<div class="filterGroup">
		<label for="monthFilter">Filter by Month:</label>
		<select id="monthFilter">
			<option value="">All Months</option>
			<?php
			for ( $voidempo_i  = 1; $voidempo_i  <= 12; $voidempo_i  ++ ) {
				$voidempo_month_value = str_pad( $voidempo_i  , 2, '0', STR_PAD_LEFT );
				$voidempo_month_name   = gmdate( 'F', mktime( 0, 0, 0, $voidempo_i  , 10 ) );
				echo "<option value='" . esc_attr( $voidempo_month_value ) . "'>" . esc_html( $voidempo_month_name  ) . '</option>';
			}
			?>
		</select>
	</div>

	<div class="filterGroup">
		<label for="yearFilter">Filter by Year:</label>
		<select id="yearFilter">
			<option value="">All Years</option>
			<?php
			for ( $voidempo_i  = gmdate( 'Y' ); $voidempo_i  >= gmdate( 'Y' ) - 10; $voidempo_i-- ) {
				echo "<option value='" . esc_attr( $voidempo_i  ) . "'>" . esc_html( $voidempo_i  ) . '</option>';
			}
			?>
		</select>
	</div>
	</div>
	<div class="lp-ad-screen" style="display:none;">
		<span><img src="<?php echo esc_url( get_site_url() . '/wp-content/plugins/voidek-employee-portal/assets/images/loader.gif' ); ?>" alt="Loading..."></span>
	</div>

	<table id="leaveApplicationsTable" class="display" style="width:100%">
		<thead style="background-color:#305f8f !important;color:white;text-align:left;">
			<tr>
				<th>User Name</th>
				<th>Month</th>
				<th>Leave Type</th>
				<th>From Date</th>
				<th>To Date</th>
				<th>Reason</th>
				<th>Status</th>
				<?php if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) { ?>
					<th>Action</th>
				<?php } ?>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<?php
require VOIDEMPO_PLUGIN_PATH . 'templates/footer.php';
?>