<?php
/**
 * Template to display a Eodlists.
 *
 * @package voidempo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! is_user_logged_in() ) {
	wp_safe_redirect( wp_login_url() );
	exit;
}
?>

<div class="row">
	<?php
	/**
	 * Access control for administrators and managers.
	 *
	 * This file restricts content visibility to users with admin or manager roles.
	 *
	 * @package YourPluginName
	 */

	if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) {
		?>

<div class="col-md-4">
<label for="eod_project_filter"><strong>Project:</strong></label>
<select class="form-control" id="eod_project_filter">
	<option value="0">--Select Project--</option>
		<?php
		global $wpdb;

		$voidempo_cache_key    = 'all_projects_cache';
		$voidempo_all_projects = wp_cache_get( $voidempo_cache_key );

		if ( false === $voidempo_all_projects ) {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching -- Caching is applied above.
		
				$voidempo_all_projects = $wpdb->get_results(
					"SELECT related_post_id, project_title FROM {$wpdb->prefix}voidempo_projects"
				);

			if ( ! empty( $voidempo_all_projects ) ) {
				wp_cache_set( $voidempo_cache_key, $all_projects, '', 12 * HOUR_IN_SECONDS );
			}
		}
		wp_cache_delete( 'all_projects_cache' );

		usort(
			$all_projects,
			function ( $a, $b ) {
				return strcasecmp( $a->project_title, $b->project_title );
			}
		);

		foreach ( $voidempo_all_projects as $voidempo_project ) {
			$voidempo_project_id    = $voidempo_project->related_post_id;
			$voidempo_project_title = $voidempo_project->project_title;
			?>
		<option value="<?php echo esc_attr( $voidempo_project_id ); ?>"><?php echo esc_html( $voidempo_project_title ); ?></option>
		<?php } ?>
</select>
</div>

<div class="col-md-4">
<label for="eod_task_filter"><strong>Task filter:</strong></label>
<select class="form-control" id="eod_task_filter">
	<option value="0">--Select Task--</option>
		<?php
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching -- Caching is applied above.
	
		$voidempo_all_tasks = $wpdb->get_results( "SELECT id, task_title FROM {$wpdb->prefix}voidempo_task" );
		usort(
			$voidempo_all_tasks,
			function ( $a, $b ) {
				return strcasecmp( $a->task_title, $b->task_title );
			}
		);

		foreach ( $voidempo_all_tasks as $voidempo_task ) {
			$voidempo_task_id    = $voidempo_task->id;
			$voidempo_task_title = $voidempo_task->task_title;
			?>
		<option value="<?php echo esc_attr( $voidempo_task_id ); ?>"><?php echo esc_html( $voidempo_task_title ); ?></option>
		<?php } ?>
</select>
</div>

<?php } ?>
		<div class="col-md-4">
					<div class="form-group">
						<label for="durationselection"><strong>Duration</strong></label>
						<input type="text" id="durationselection_" class="form-control durationselection_" value="<?php echo esc_attr( gmdate( 'Y/m/d', strtotime( '- 1 day' ) ) . ' - ' . gmdate( 'Y/m/d', strtotime( '- 1 day' ) ) ); ?>" >
					</div>
		</div>
		<div class="row date-filters">
			<input type="hidden" class="form-control date-filter" id="from_date">
			<input type="hidden" class="form-control date-filter" id="to_date">
		</div>
</div>
<table id="voidempo_eod_list_view" class="table table-striped table-bordered voidempo_table_filter voidempo_eod_list_view" style="width:100%">
	<thead>
		<tr>
			<th>Project Details</th>
			<th>Tasks Details</th>
			<th>Tasks Description</th>
			<th>Member</th>
			<?php /* } */ ?>
			<th>Hours</th>
			<th>status</th>
			<th>Notes</th>
			<?php if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) { ?>
				<th>Action</th>
			<?php } ?>
		</tr>
	</thead>
	<tbody>
	</tbody>
</table>
<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>

<!-- Modal for updating EOD -->
<div class="modal fade" id="updateEODModal" tabindex="-1" aria-labelledby="updateEODModalLabel" aria-hidden="true">
	<div class="modal-dialog">
	<div class="modal-content">
		<div class="modal-header">
			<h5 class="modal-title" id="updateEODModalLabel">Update EOD</h5>
			<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
		</div>
		<div class="modal-body">
			<form id="updateEODForm">
				<input type="hidden" id="eod_id" name="eod_id">
				<div class="form-group">
					<label for="project">Project</label>
					<input type="text" class="form-control" id="project" name="project" readonly>
				</div>
				<div class="form-group">
					<label for="task">Task</label>
					<input type="text" class="form-control" id="task" name="task" readonly>
				</div>
				<div class="form-group">
					<label for="task_status">Task Status</label>
					<select class="form-control" id="task_status" name="task_status">
						<option value="Inprogress">Inprogress</option>
						<option value="Completed">Completed</option>
					</select>
				</div>
				<div class="form-group">
					<label for="hours">Hours</label>
					<input type="text" class="form-control" id="hours" name="hours">
				</div>
			</form>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
			<button type="button" class="btn btn-primary" id="updateEODBtn">Update EOD</button>
		</div>
	</div>
	</div>
</div>



