<?php
/**
 * Template to display a Project Task Details.
 *
 * @package voidempo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! is_user_logged_in() ) {
	wp_safe_redirect( wp_login_url() );
	exit;
}
ob_start();
global $wpdb;

check_ajax_referer( 'voidempo_dashboard_nonce', 'security' );

$voidempo_project_id = 0;
if ( ! $voidempo_project_id ) {
	$voidempo_project_id = isset( $_POST['project_id'] ) ? intval( $_POST['project_id'] ) : 0;
}

$voidempo_current_uid  = get_current_user_ID();

?>


<div class="mt-3">
	<?php
	$voidempo_my_projects = VOIDEMPO_fetch_my_projects( $voidempo_current_uid  );

	?>
	<div class="row">
		<div class="col-md-4">
			<button class="btn btn-primary load-page-btn" data-page="projects" style=" display: block;">Go to projects</button></a>
		</div>
		<div class="col-md-4">
			<h3>
				<center>My Project Tasks</center>
			</h3>
		</div>
		<div class="col-md-4 d-flex justify-content-end">
		<button class="btn btn-primary load-page-btn" data-page="voidempo_create_task">Create New Task</button>
		</div>
	</div>

	<div class="form-row pb-4">
		<div class="col-md-12"><label for="voidempo_view_task_projects_lists">Select Project</label>
		</div>
		<div class="col-md-11">
			<?php
			if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) {
				?>
			<select class="form-control max-width_unset" id="voidempo_task_project1">
				<option value="0">--Select Project--</option>
				<?php
				global $wpdb;
				// 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' );

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

				foreach ( $voidempo_all_projects as $voidempo_project ) {
					$voidempo_project_id_select  = $voidempo_project->related_post_id;
					$voidempo_project_title      = $voidempo_project->project_title;
					?>
					<option value="<?php echo esc_attr( $voidempo_project_id_select ); ?>" <?php echo ( $voidempo_project_id === $voidempo_project_id_select ) ? 'selected' : ''; ?>>
						<?php echo esc_html( $voidempo_project_title  ); ?>
					</option>
				<?php } ?>
			</select>
		<?php } else { ?>
		<div class="col-md-11">
   
		<select class="form-control max-width_unset" id="voidempo_task_project1">
			<option value="0">--Select Project--</option>
				<?php
				$voidempo_my_projects = VOIDEMPO_fetch_my_projects( $voidempo_current_uid  );

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

				foreach ( $voidempo_my_projects as $voidempo_my_projects_data ) {
					$voidempo_project_id_select = $voidempo_my_projects_data->related_post_id;
					$voidempo_project_title      = $voidempo_my_projects_data->project_title;
					$voidempo_posts            = get_post( $voidempo_project_id_select );

					if ( 'publish' === $voidempo_posts->post_status && 'Completed' !== $voidempo_my_projects_data->status ) {
						?>
					<option value="<?php echo esc_attr( $voidempo_project_id_select ); ?>" <?php echo ( $voidempo_project_id === $voidempo_project_id_select ) ? 'selected' : ''; ?>>
						<?php echo esc_html( $voidempo_project_title  ); ?>
					</option>
						<?php
					}
				}
				?>
		</select>
<?php } ?>
</div>

		
	</div>
	<input type="hidden" class="hidprojectid" name="" value="<?php echo esc_attr( $voidempo_project_id ); ?>">
	<div class="voidempo_task_view_table_div" style="overflow:auto;">
		<?php
		$voidempo_is_manager = voidempo_fn_check_is_manager_( $project_id, $voidempo_current_uid  );
		?>
		<table id="voidempo_task_view_table" class="table table-striped table-bordered" style="width:100%">
			<thead>
				<tr>
					<th>Project Title</th>
					<th>Task Title</th>
					<th>Task Description</th>
					<th>Employee Name</th>
					<th>Task Status</th>
					<th>Members task status</th>
					<th>Estimated Hours</th>
					<th>Actual Hours</th>
					<th>Task Created Date</th>
					<?php
					if ( 'is_manager' === $voidempo_is_manager || current_user_can( ' manage_options ')  || current_user_can( 'voidempo_manager_access' )) {
						?>
						<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>
	</div>
</div>


<div class="modal fade" id="editTaskModal" tabindex="-1" aria-labelledby="editTaskModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg"> <!-- Make it larger for big forms -->
	<div class="modal-content">
		<div class="modal-header">
		<h5 class="modal-title" id="editTaskModalLabel">Edit Task</h5>
		<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
		</div>

		<div class="modal-body">
		<form id="edit_task_form">
			<input type="hidden" id="edit_task_id" name="task_id">

			<div class="mb-3">
			<label for="task_title" class="form-label">Task Title</label>
			<input type="text" class="form-control" id="task_title" name="voidempo_task_title">
			</div>

			<div class="mb-3">
			<label for="task_description" class="form-label">Task Description</label>
			<textarea class="form-control" id="task_description" name="voidempo_task_description"></textarea>
			</div>

			<div class="mb-3">
			<label for="task_status" class="form-label">Status</label>
			<select class="form-select" id="task_status" name="voidempo_task_status">
				<option value="">--Select--</option>
				<option value="Pending">Not Started</option>
				<option value="Inprogress">In Progress</option>
				<option value="Completed">Completed</option>
			</select>
			</div>

			<div class="mb-3">
			<label for="task_manager" class="form-label">Task Manager</label>
			<select class="form-select" id="task_manager" name="voidempo_task_manager">
				<!-- Options will be loaded dynamically -->
			</select>
			</div>

			<div class="mb-3">
			<label for="task_members" class="form-label">Assign Members</label>
			<select class="form-select" id="voidempo_taskc_assign_mem" name="voidempo_taskc_assign_mem[]" multiple>
				<!-- Options will be loaded dynamically -->
			</select>
			</div>

			<div class="mb-3">
			<label class="form-label">Estimated Hours & Minutes</label>
			<div class="d-flex gap-2">
				<input type="number" class="form-control" id="estimated_hours" name="voidempo_task_estimated_hours" placeholder="Hours" style="width: 100px;">
				<input type="number" class="form-control" id="estimated_minutes" name="voidempo_task_estimated_min" placeholder="Minutes" style="width: 100px;">
			</div>
			<div id="minutes_error" class="text-danger small"></div>
			</div>

			<div id="edit_task_message" class="mt-2"></div>
		</form>
		</div>

		<div class="modal-footer">
		<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
		<button type="submit" form="edit_task_form" class="btn btn-primary" id="saveTaskChanges">Save changes</button>
		</div>
	</div>
	</div>
</div>

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