<?php
/**
 * Template to display a Add Projects Details.
 *
 * @package voidempo
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

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

$voidempo_all_users       = get_users();
$voidempo_project_manager = $voidempo_project_manager ?? array();
$voidempo_project_members = $voidempo_project_members ?? array();
$voidempo_project_status  = $voidempo_project_status ?? '';

?>

<div class="mt-5">
	<h3 class="mb-0" style="color:#000">Add New Project</h3>
	<div class="card-body p-4" style="box-shadow: 0 1px 12px rgb(0 0 0 / 29%);background-color:#F3F3F3;border-radius: 8px;margin-top: 15px;margin-bottom: 15px;">
		<form id="add-project-form">
			<div class="row mb-3">
				<div class="col-md-6">
					<label class="form-label">Project Title</label>
					<input type="text" class="form-control" name="project_title" id="project_title" required>
					<p class="error" id="error_project_title"></p>
				</div>
				<div class="col-md-6">
					<label class="form-label">Project Description</label>
					<textarea name="project_description" id="project_description" class="form-control" rows="1"></textarea>
				</div>
			</div>

			<div class="row mb-3">
				<div class="col-md-6">
					<label class="form-label">Start Date</label>
					<input type="text" class="form-control" id="voidempo_project_start_date" name="voidempo_project_start_date">
				</div>
				<div class="col-md-6">
					<label class="form-label">End Date</label>
					<input type="text" class="form-control" id="voidempo_project_end_date" name="voidempo_project_end_date">
				</div>
			</div>

			<div class="row mb-3">
				<div class="col-md-6">
					<label class="form-label">Manager</label>
					<select class="form-control voidempo_project_manager" id="voidempo_project_manager" name="voidempo_project_manager">
						<option value='0'>--Select--</option>
						<?php
						foreach ( $voidempo_all_users as $voidempo_all_users_data ) {
							if ( get_user_meta( $voidempo_all_users_data->ID, '_employee_disabled', true ) != 1 ) {
								echo '<option value="' . esc_attr( $voidempo_all_users_data->ID ) . '" ' . selected( in_array( $voidempo_all_users_data->ID, $voidempo_project_manager ), true, false ) . '>' . esc_html( $voidempo_all_users_data->display_name ) . '</option>';
							}
						}

						?>
					</select>
				</div>
				<div class="col-md-6">
					<label class="form-label">Members</label>
					<select class="form-control voidempo_project_members" id="voidempo_project_members" name="voidempo_project_members[]" multiple>
						<?php
						foreach ( $voidempo_all_users as $voidempo_all_users_data ) {
							if ( get_user_meta( $voidempo_all_users_data->ID, '_employee_disabled', true ) != 1 ) {
								echo '<option value="' . esc_attr( $voidempo_all_users_data->ID ) . '" ' . selected( in_array( $voidempo_all_users_data->ID, $voidempo_project_manager ), true, false ) . '>' . esc_html( $voidempo_all_users_data->display_name ) . '</option>';
							}
						}

						?>
					</select>
				</div>
			</div>

			<div class="row mb-4">
				<div class="col-md-6">
					<label class="form-label">Status</label>
					<select class="form-control" id="voidempo_project_status" name="voidempo_project_status">
						<option 
						<?php
						if ( 'Pending' === $voidempo_project_status ) {
							echo 'selected';}
						?>
						value="Pending">Inprogress</option>
						<option 
						<?php
						if ( 'Completed' === $voidempo_project_status ) {
							echo 'selected';}
						?>
						value="Completed">Completed</option>
					</select>
				</div>
			</div>

			<div class="text-center">
				<button type="submit" id="add-project-btn" class="btn btn-primary px-4 rounded-pill">Add Project</button>
			</div>
		</form>
	</div>
</div>


<table class="display table table-bordered" style="width:100%" id="project-list-table">
	<thead class="thead-dark">
		<tr>  
			<th>Project Title</th>
			<th>Start Date</th>
			<th>End Date</th>
			<th>Manager</th>
			<th>Members</th>
			<th>Status</th>
			<th>Actions</th>
		</tr>
	</thead>
<tbody>
</tbody>
</table>
<div class="modal fade" id="editProjectModal" tabindex="-1" role="dialog" aria-labelledby="editProjectLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	<div class="modal-content">
		<form id="editProjectForm">
		<div class="modal-header">
			<h5 class="modal-title">Edit Project</h5>
			<button type="button" class="close" data-bs-dismiss="modal">&times;</button>
		</div>
		<div class="modal-body row">
			<input type="hidden" name="project_id" id="edit_project_id">
		   
			<div class="form-group col-md-6">
			<label for="edit_project_title">Project Title</label>
			<input type="text" class="form-control" name="project_title" id="edit_project_title" required>
			</div>

			<div class="form-group col-md-6">
			<label for="edit_status">Status</label>
			<select class="form-control" name="status" id="edit_status">
				<option value="Pending">Inprogress</option>
				<option value="Completed">Completed</option>
			</select>
			</div>

			<div class="form-group col-md-6">
			<label for="edit_start_date">Start Date</label>
			<input type="date" class="form-control" name="start_date" id="edit_start_date">
			</div>

			<div class="form-group col-md-6">
			<label for="edit_end_date">End Date</label>
			<input type="date" class="form-control" name="end_date" id="edit_end_date">
			</div>

			<div class="form-group col-md-6">
			<label for="edit_manager">Manager</label>
			<select class="form-control" name="manager_id" id="edit_manager">
			
			</select>
			</div>
			<div class="form-group col-md-6">
			<label for="edit_members">Members</label>
			<select multiple class="form-control select2" name="member_id[]" id="edit_members" style="width: 100%;">
				</select>

			</div>
		</div>
		<div class="modal-footer">
			<button type="submit" class="btn btn-primary edit_btn_project" >Update</button>
		</div>
		</form>
	</div>
	</div>
</div>
<?php
require VOIDEMPO_PLUGIN_PATH . 'templates/footer.php';
?>