<?php
/**
 * Template to Create Tasks.
 *
 * @package voidempo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! is_user_logged_in() ) {
	wp_safe_redirect( wp_login_url() );
	exit;
}
$voidempo_current_uid = get_current_user_ID();
if ( ! empty( $_GET['task_id'] ) ) {

	if ( ! isset( $_POST['security'] ) ) {
		wp_send_json_error( array( 'message' => 'Missing nonce!' ) );
		wp_die();
	}

	check_ajax_referer( 'voidempo_dashboard_nonce', 'security' );

	$voidempo_task_id= intval( wp_unslash( $_GET['task_id'] ) );
}
 else {
	?>
	<div class="mt-3">
		<h2>
			<center>Create Task</center>
		</h2>
		<form class="form-horizontal">
		<?php
		/**
		 * Sorts an array of projects alphabetically by their title.
		 *
		 * Used as a callback for usort().
		 *
		 * @param object $a First project object, must have 'project_title' property.
		 * @param object $b Second project object, must have 'project_title' property.
		 * @return int Comparison result for usort() (-1, 0, 1).
		 */
		function voidempo_sort_projects_by_title( $a, $b ) {
			return strcmp( $a->project_title, $b->project_title );
		}
		?>

<div class="form-group">
		<?php wp_nonce_field( 'voidempo_filter_eod_action', 'voidempo_eodfilter_nonce' ); ?>
	<label class="control-label" for="voidempo_task_project">Project</label>
	<?php if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) { ?>
		<select class="form-control max-width_unset" id="voidempo_task_project">
			<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 * FROM ' . $wpdb->prefix . 'voidempo_projects' );
			usort( $voidempo_all_projects, 'voidempo_sort_projects_by_title' );  // Sort projects by 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>
	<?php } else { ?>
		<select class="form-control max-width_unset" id="voidempo_task_project">
			<option value="0">--Select Project--</option>
			<?php
			if ( isset( $_POST['voidempo_eodfilter_nonce'] ) ) {

				check_ajax_referer( 'voidempo_filter_eod_action', 'voidempo_eodfilter_nonce' );

				$voidempo_project_id = isset( $_GET['project_id'] ) 
					? intval( wp_unslash( $_GET['project_id'] ) ) 
					: null;
			}

			$voidempo_my_projects = VOIDEMPO_fetch_my_projects( $voidempo_current_uid );
			usort( $voidempo_my_projects, 'voidempo_sort_projects_by_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 ) {
					$voidempo_selected = ( $voidempo_project_id === $voidempo_project_id_select ) ? 'selected' : '';
					?>
					<option <?php echo esc_attr( $voidempo_selected ); ?> value="<?php echo esc_attr( $voidempo_project_id_select ); ?>"><?php echo esc_html( $voidempo_project_title ); ?></option>
					<?php
				}
			}
			?>
		</select>
			<?php } ?>
</div>

<div class="form-group">
	<label class="control-label" for="voidempo_task_title">Task Title</label>
	<input type="text" class="form-control" id="voidempo_task_title" />
</div>

<div class="form-group">
	<label class="control-label" for="voidempo_task_description">Task Description</label>
		<textarea class="form-control" rows="5" id="voidempo_task_description"></textarea>
	</div>

	<div class="form-group" style="display: grid;">
		<label class="control-label" for="voidempo_taskc_status">Status</label>
			<select class="voidempo_taskc_status max-width_unset vw-w" id="voidempo_taskc_status" name="voidempo_taskc_status" style="width: 100%;">
				<option value="Inprogress">Inprogress</option>
				<option value="Pending">Not Started</option>
			<select>	
	</div>
				<?php if ( current_user_can( 'manage_options' ) || current_user_can( 'voidempo_manager_access' ) ) { ?>

				<div class="form-group">
					<label class="control-label" for="voidempo_create_task_manager">Task Manager</label>
					<select class="form-control" id="voidempo_create_task_manager" name="voidempo_create_task_manager"></select>
				</div>

				<div class="form-group ">
					<label class="control-label" for="voidempo_taskc_assign_mem">Assign Members</label>
					
					<select class="voidempo_taskc_assign_mem" id="voidempo_taskc_assign_mem" name="voidempo_taskc_assign_mem[]" multiple></select>
				
				</div>

			<?php } ?>
			<div class="form-group voidempo_taskc_assign_mem_div voidempo_project_select2_div">
				<?php
				if ( ! empty( $_GET['project_id'] ) ) {


					if ( isset( $_POST['voidempo_eodfilter_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['voidempo_eodfilter_nonce'] ) ), 'voidempo_filter_eod_action' ) ) {
						$voidempo_is_manager = voidempo_fn_check_is_manager_( sanitize_text_field( wp_unslash( $_GET['project_id'] ) ), get_current_user_id() );
						$voidempo_project_id = isset( $_GET['project_id'] ) ? intval( wp_unslash( $_GET['project_id'] ) ) : null;
					}
					if ( 'is_manager' === $voidempo_is_manager ) {
						$voidempo_project_members  = explode( ',', get_post_meta( $voidempo_project_id, 'voidempo_project_members', true ) );
						$voidempo_project_managers = explode( ',', get_post_meta( $voidempo_project_id, 'voidempo_project_manager', true ) );
						$voidempo_all_members               = array_merge( $voidempo_project_managers, $voidempo_project_members );
						?>
						
						<label for="voidempo_taskc_assign_mem">Assign Members (If only you then please assign only your name to task)</label>
						<select class="voidempo_taskc_assign_mem" id="voidempo_taskc_assign_mem" name="voidempo_taskc_assign_mem[]" multiple>
							<?php
							foreach ( $voidempo_all_members as $voidempo_project_members_id ) {
								$voidempo_user_data = get_user_by( 'id', $voidempo_project_members_id );
								if ( get_user_meta( $voidempo_user_data->ID, '_employee_disabled', true ) !== 1 ) {
									?>
								<option value="<?php echo esc_attr( $voidempo_user_data->ID ); ?>">
									<?php echo esc_html( $voidempo_user_data->display_name ); ?>
								</option>
									<?php
								}
							}
							?>
						</select>
						<?php
					}
				}
				?>
				</div>
				
				<div class="form-check form-check-inline mb-2">
					<b>Estimated time</b> &nbsp;&nbsp;
					<input type="number" class="form-control" id="voidempo_task_estimated_hours" Placeholder="01" style="width:20%;" />&nbsp;&nbsp;<b>: </b>&nbsp;&nbsp;
					<input type="number" class="form-control" id="voidempo_task_estimated_min" Placeholder="00" style="width:20%;" />
				</div>
				
				<div class="form-group" style="display: flex;">
					<div class="">
						<button type="button" class="btn btn-primary" onclick="voidempo_task.voidempo_task_create(this);">Save</button>
						<a href="
						<?php
						if ( ! empty( $_GET['project_id'] ) ) {
							echo esc_url( '?page=voidempo_view_tasks&project_id=' . sanitize_text_field( wp_unslash( $_GET['project_id'] ) ) );
						} else {
							echo 'projects';
						}
						?>
								"><button type="button" class="btn btn-primary">Cancel</button></a>
								</div>
							</div>
						</form>
					</div>
					<?php
}
?>


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