<?php
/**
 * Template to display a Dashboard With Tasks ,Projects And Missing Eods Details .
 *
 * @package voidempo
 */

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

$voidempo_user_id        = get_current_user_id();
$voidempo_project_status = 'publish';
$voidempo_project_type   = 'voidempo_projects';

// Build a safe cache key
$voidempo_cache_key           = 'voidempo_projects_statuscnt_' . md5( $voidempo_user_id . $voidempo_project_status . $voidempo_project_type );
$voidempo_projectsdatacntwise = wp_cache_get( $voidempo_cache_key, 'voidempo_projects' );

if ( false === $voidempo_projectsdatacntwise ) {
	// Use only placeholders, not raw SQL conditions

	$voidempo_projectsdatacntwise = $wpdb->get_results(
		$wpdb->prepare(
			"
		SELECT cp.*, COUNT(cp.status) AS statuscnt 
		FROM {$wpdb->prefix}voidempo_projects AS cp
		INNER JOIN {$wpdb->prefix}posts AS p ON p.ID = cp.related_post_id
		WHERE p.post_status = %s
		AND p.post_type = %s
		AND (
			FIND_IN_SET( %d, cp.member_id ) > 0
			OR FIND_IN_SET( %d, cp.manager_id ) > 0
		)
		GROUP BY cp.status
	",
			$voidempo_project_status,
			$voidempo_project_type,
			$voidempo_user_id,
			$voidempo_user_id
		)
	);

	wp_cache_set( $voidempo_cache_key, $voidempo_projectsdatacntwise, 'voidempo_projects', 600 );
}


$voidempo_post_status    = 'publish';
$post_type      = 'voidempo_projects';
$voidempo_is_delete_flag = 1;

$voidempo_cache_key       = 'voidempo_task_statuscnt_' . md5( $voidempo_user_id . $voidempo_post_status . $post_type . $voidempo_is_delete_flag );
$voidempo_taskdatacntwise = wp_cache_get( $voidempo_cache_key, 'voidempo_tasks' );

if ( false === $voidempo_taskdatacntwise ) {
	$voidempo_taskdatacntwise = $wpdb->get_results(
		$wpdb->prepare(
			"
        SELECT T.*, COUNT(T.status) AS statuscnt
        FROM {$wpdb->prefix}voidempo_task AS T
        INNER JOIN {$wpdb->prefix}posts AS p ON p.ID = T.project_id
        WHERE p.post_status = %s
        AND p.post_type = %s
        AND T.is_delete != %d
        AND (
            FIND_IN_SET( %d, T.user_id ) > 0
            OR FIND_IN_SET( %d, T.manager_id ) > 0
        )
        GROUP BY T.status
        ",
			$voidempo_post_status,
			$post_type,
			$voidempo_is_delete_flag,
			$voidempo_user_id,
			$voidempo_user_id
		)
	);

	wp_cache_set( $voidempo_cache_key, $voidempo_taskdatacntwise, 'voidempo_tasks', 600 );
}


$voidempo_taskcountarray = array();
foreach ( $voidempo_taskdatacntwise as $voidempo_taskmeta ) {
	$voidempo_taskcountarray[ $voidempo_taskmeta->status ] = $voidempo_taskmeta->statuscnt;
}
?>
<br>
<div class="mt-4">
	<h3>
		<center>Dashboard</center>
	</h3>

	<?php
		do_action( 'voidempo_display_leave_details_box' );
	?>

<div class="row mt-3">
		<div class=" text-center">

		<div class="dbbox">
				<div style="border-bottom: 1px solid #ccc;">
					<h5>Projects</h5>
				</div>
				<div class="roundhouse d-flex justify-content-center">
					<?php
					$voidempo_projectscnt = array();
					foreach ( $voidempo_projectsdatacntwise as $voidempo_pdatas ) {
						$voidempo_projectscnt[ $voidempo_pdatas->status ] = $voidempo_pdatas->statuscnt;
					}
					?>
				<div class="tooltip-box blue-tooltip"  style="background-color:#007bff;border-color:#007bff;" data-tooltip="Pending Project">
					<?php echo esc_html( $voidempo_projectscnt['Pending'] ?? 0 ); ?>
				</div>
				<div class="tooltip-box green-tooltip" style="background-color:#28a745;border-color:#28a745;" data-tooltip="Completed Project">
					<?php echo esc_html( $voidempo_projectscnt['Completed'] ?? 0 ); ?>
				</div>
				</div>
			</div>

			<div class="dbbox" style="margin-top:40px;">
				<div class="" style="border-bottom: 1px solid #ccc;">
					<h5>Tasks</h5>
				</div>
				<div class="roundhouse d-flex justify-content-center">
				<div  class="tooltip-box yellow-tooltip" style="background-color:#ffc107;border-color:#ffc107;" data-tooltip="Pending Task">
					<?php echo esc_html( isset( $voidempo_taskcountarray['Pending'] ) ? $voidempo_taskcountarray['Pending'] : 0 ); ?>
				</div>
				<div   class="tooltip-box blue-tooltip" style="background-color:#007bff;border-color:#007bff;" data-tooltip="Inprogress Task">
					<?php echo esc_html( isset( $voidempo_taskcountarray['Inprogress'] ) ? $voidempo_taskcountarray['Inprogress'] : 0 ); ?>
				</div>
				<div  class="tooltip-box green-tooltip" style="background-color:#28a745;border-color:#28a745;" data-tooltip ="Completed Task">
					<?php echo esc_html( isset( $voidempo_taskcountarray['Completed'] ) ? $voidempo_taskcountarray['Completed'] : 0 ); ?>
				</div>
				</div>
			</div>
		</div>
		</div>
	</div>
	<div class="mt-5" style="border: 1px solid #5c5c5c; background-color:#f3f3f3; padding: 10px; border-radius: 5px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); text-align: center; font-weight: bold;">
		<h6 style="margin-bottom: 0; color: #000; font-size: 16px;">Missing EOD</h6>
	</div>
	<?php
	$voidempo_style = 'display:none;';
	if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) {
		$voidempo_style    = '';
		$voidempo_user_i_d = 0;
	}
	?>
	<div class="row mt-3">
		<div class="col-md-4" style="<?php echo esc_attr( $voidempo_style ); ?>">
			<div class="form-group">
				<label for="misemployeename" style="font-weight: bold; color: #000;">Employee Name</label>
				<select class="form-control misemployeename" id="misemployeename">
				<?php
					if ( current_user_can( 'voidempo_manager_access' ) || current_user_can( 'manage_options' ) ) {
				?>
						<option value="0"> All </option>
						<?php
						foreach ( get_users() as $voidempo_usersmeta ) {
							if ( get_user_meta( $voidempo_usersmeta->ID, '_employee_disabled', true ) != 1 ) {
								?>
								<option value="<?php echo esc_attr( $voidempo_usersmeta->ID ); ?>"><?php echo esc_html( $voidempo_usersmeta->display_name ); ?></option>
								<?php
							}
						}
					} else {
							$voidempo_usersmeta = get_user_by( 'id', $voidempo_user_id );
						?>
							<option value="<?php echo esc_attr( $voidempo_usersmeta->ID ); ?>">
								<?php echo esc_html( $voidempo_usersmeta->display_name ); ?>
							</option>                    
							<?php
					}
					?>
				</select>
			</div>
		</div>
		<div class="col-md-4">
			<div class="form-group">
				<label for="durationselection_"  style="font-weight: bold; color: #000;">Duration</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' ) ) ); ?>"
				style="width: 100%; padding: 8px; border: 1px solid #5c5c5c; border-radius: 5px; background-color: #fff; color: #333;text-align: center">
			</div>
		</div>
		<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 class="col-md-2">
		<div class="form-group" style="margin-top: 37px;">
			<button class="btn btn-primary form-control searchmissingeod" type="button"
					style="background-color: #007bff; border: none; padding: 10px; border-radius: 5px; color: white; font-weight: bold; display: flex; align-items: center; justify-content: center; cursor: pointer;">
				<i class="fa fa-search" style="margin-right: 5px;"></i> Search
			</button>
		</div>
	</div>
<div class="box" style="display: flex; align-items: center; gap: 20px; padding: 15px; border: 1px solid #5c5c5c; background: #f3f3f3; margin-top: 20px; border-radius: 8px; justify-content: center; flex-wrap: wrap;
	transition: all 0.3s ease-in-out; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1); margin-left: 10px;width:98% !important;">

	<!-- Leave Symbol -->
	<div style="display: flex; align-items: center; gap: 10px;">
		<div class="symbol leave-symboll" style="background: #ff6961; color: white; width: 55px; height: 55px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 20px;
			transition: all 0.3s ease-in-out;"
			onmouseover="this.style.transform='scale(1.2)';"
			onmouseout="this.style.transform='scale(1)'; this.style.boxShadow='none';"> L
		</div>
		<b style="color: #000; font-size: 16px;">: For Leaves</b>
	</div>

	<!-- Holiday Symbol -->
	<div style="display: flex; align-items: center; gap: 10px;">
		<div class="symbol holiday-symboll" style="background: rgb(100, 219, 100); color: white; width: 55px; height: 55px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 20px;
			transition: all 0.3s ease-in-out;"
			onmouseover="this.style.transform='scale(1.2)'; this.style.boxShadow='0px 0px 10px rgba(119, 221, 119, 0.6)';"
			onmouseout="this.style.transform='scale(1)'; this.style.boxShadow='none';"> H
		</div>
		<b style="color: #000; font-size: 16px;">: For Holidays</b>
	</div>
</div>
	<div class="mt-4 voidempo_missingeod_tablediv">
		<?php
		$voidempo_stdate   = $voidempo_eddate = gmdate( 'Y-m-d' );
		$voidempo_user_i_d = $voidempo_user_id;
		voidempo_getMissingEodByDateandUser_( $voidempo_stdate, $voidempo_eddate, $voidempo_user_i_d );
		?>
	</div>		   
<?php
require VOIDEMPO_PLUGIN_PATH . 'templates/footer.php';
?>
