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

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

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

<div class="filter-wrapper" style="display: flex; margin-bottom: 40px; column-gap: 10px; flex-wrap: wrap;">

		<?php
		/**
		 * Check if user has admin or manager capabilities
		 */
		if ( current_user_can( 'voidempo_manager_access' )|| current_user_can( 'manage_options' ) ) {
			?>

		<!-- Admin/Manager View -->
		<div style="width: 20%; padding:10px;">
						<?php
						/**
						 * Displays the project filter dropdown in the voidempo
						 *
						 * @package voidempo
						 */

						?>
			<label for="report_project_filter"><strong>Filter by Project</strong></label>
			<select id="report_project_filter" class="form-control">
			<option value="">Select Project</option>
				<?php
				global $wpdb;

				$voidempo_cache_key = 'voidempo_projects_all';
				$voidempo_projects  = wp_cache_get( $voidempo_cache_key, 'voidempo_projects_group' );

				if ( false === $voidempo_projects ) {
					// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
					$voidempo_projects = $wpdb->get_results(
						"SELECT related_post_id, project_title FROM {$wpdb->prefix}voidempo_projects ORDER BY project_title ASC"
					);

					if ( false === $voidempo_projects ) {
						$voidempo_projects = array();
					} else {
						wp_cache_set( $voidempo_cache_key, $voidempo_projects, 'voidempo_projects_group', 600 );
					}
				}

				if ( ! empty( $voidempo_projects ) && is_array( $voidempo_projects ) ) {
					foreach ( $voidempo_projects as $voidempo_project ) {
						echo '<option value="' . esc_attr( $voidempo_project->related_post_id ) . '">' . esc_html( $voidempo_project->project_title ) . '</option>';
					}
				} else {
					echo '<option value="">No projects found</option>';
				}
				?>
			</select>
		</div>

		<div style="width: 20%; padding:10px;">
			<label for="report_task_filter"><strong>Filter by Task</strong></label>
			<select id="report_task_filter" class="form-control">
				<option value="">Select Task</option>
			</select>
		</div>

	<?php } else { ?>
		<!-- Normal User View -->
		<div style="width: 20%; padding:10px;">
			<label for="report_project_filter"><strong>Filter by Project</strong></label>
			<select id="report_project_filter" class="form-control">
				<option value="">Select Project</option>
						<?php
						global $wpdb;
						$voidempo_current_user_id = get_current_user_id();
						$voidempo_cache_key       = 'voidempo_projects_user_' . $voidempo_current_user_id;
						$voidempo_cache_group     = 'voidempo_projects';
						$voidempo_projects        = wp_cache_get( $voidempo_cache_key, 'voidempo_projects' );
						if ( false === $voidempo_projects ) {
							// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery
							$voidempo_projects = $wpdb->get_results(
								$wpdb->prepare(
									"
								SELECT related_post_id, project_title  
								FROM {$wpdb->prefix}voidempo_projects 
								WHERE FIND_IN_SET(%s, member_id) > 0 
								OR FIND_IN_SET(%s, manager_id) > 0
								",
									$voidempo_current_user_id,
									$voidempo_current_user_id
								)
							);

							if ( false === $voidempo_projects ) {
								$voidempo_projects = array();
							} else {
								wp_cache_set( $voidempo_cache_key, $voidempo_projects, $voidempo_cache_group, 300 );
							}
						}

						if ( ! empty( $voidempo_projects ) && is_array( $voidempo_projects ) ) {
							foreach ( $voidempo_projects as $voidempo_project ) {
								echo '<option value="' . esc_attr( $voidempo_project->related_post_id ) . '">' . esc_html( $voidempo_project->project_title ) . '</option>';
							}
						} else {
							echo '<option value="">No projects found</option>';
						}
						?>
			</select>
		</div>
		<div style="width: 20%; padding:10px;">
			<label for="report_task_filter"><strong>Filter by Task</strong></label>
			<select id="report_task_filter" class="form-control">
				<option value="">Select Task</option>
			</select>
		</div>
	<?php } ?>

	<!-- Common Duration Field -->
	<div style="width: 25%; padding:10px;">
		<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>

	<!-- Hidden Date Filters -->
	<div class="row date-filters" style="display:none;">
		<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 -->
<table id="voidempo_report_table" class="table table-striped table-bordered" style="width:100%">
	<thead>
		<tr>
			<th>Project Details</th>
			<th>Tasks Details</th>
			<th>Tasks Description</th>
			<th>Tasks Status</th>
			<th>Member</th>
			<th>Members task status</th>
			<th>Estimated Hours</th>
			<th>Actual Hours</th>
			<th>Remaining Hours</th>
		</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>

<!-- JS for Dynamic Task Dropdown -->


<?php
require VOIDEMPO_PLUGIN_PATH . 'templates/footer.php';
?>