<?php
if (!empty($startDate) && !empty($endDate)) {
    // $currentDate = $startDate;
    $currentDate = date('Y-m-d');
    while (strtotime($currentDate) <= strtotime($endDate)) {
        $currentDay = date('w', strtotime($currentDate)); // Get day of the week (0 = Sunday, ..., 6 = Saturday)
        // if($value->refDataName=="Service Test Tithi"){

        $dayVisible = false;
        if (isset($value->dayTypes) && !empty($value->dayTypes)) {

            $dayVisible = false;

            $startDateArray = getThithis($value->dayTypes);





            if ($value->dayTypes == getWeekday($currentDate)) {
                // echo "<br>".$value->refDataName."/22<br>";
                $dayVisible = true;
            } else if (getWeekdayOccurrence($currentDate) == $value->dayTypes) {
                // echo "<br>".$value->refDataName."/33<br>";
                $dayVisible = true;
            } else {
                // echo "<br>".$value->refDataName."/44<br>";
                if (!empty($startDateArray)) {
                    $dateToCheck = date('Y-m-d', strtotime($currentDate));


                    $dayVisible = in_array($dateToCheck, $startDateArray) && (strtotime($currentDate) >= strtotime(date('Y-m-d'))) && in_array($value->dayTypes, $allowedTithis);
                }
            }
        } else if (!isset($value->dayTypes) && empty($value->dayTypes)) {
            $dayVisible = true;
        } else if (strtotime($currentDate) <= strtotime($endDate) && strtotime($currentDate) >= strtotime($endDate)) {
            $dayVisible = true;
        }

        if ($dayVisible) {
        }
        // }

        $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
    }
} else if (empty($startDate) && empty($endDate) && isset($value->dayTypes) && !empty($value->dayTypes)) {

    // Handle daily events when startDate and endDate are blank
    $currentDate = date('Y-m-d'); // Start from today
    $endDate = date('Y-m-d', strtotime('+365 days')); // Display for the next 30 days (or adjust as needed)

    while (strtotime($currentDate) <= strtotime($endDate)) {

        $dayVisible = false;
        if (isset($value->dayTypes) && !empty($value->dayTypes)) {

            $dayVisible = false;

            $startDateArray = getThithis($value->dayTypes);





            if ($value->dayTypes == getWeekday($currentDate)) {
                // echo "<br>".$value->refDataName."/22<br>";
                $dayVisible = true;
            } else if (getWeekdayOccurrence($currentDate) == $value->dayTypes) {
                // echo "<br>".$value->refDataName."/33<br>";
                $dayVisible = true;
            } else {
                // echo "<br>".$value->refDataName."/44<br>";
                if (!empty($startDateArray)) {
                    $dateToCheck = date('Y-m-d', strtotime($currentDate));


                    $dayVisible = in_array($dateToCheck, $startDateArray) && (strtotime($currentDate) >= strtotime(date('Y-m-d'))) && in_array($value->dayTypes, $allowedTithis);
                }
            }
        } else if (!isset($value->dayTypes) && empty($value->dayTypes)) {
            $dayVisible = true;
        } else if (strtotime($currentDate) <= strtotime($endDate) && strtotime($currentDate) >= strtotime($endDate)) {
            $dayVisible = true;
        }



        if ($dayVisible) {
        }
        $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
    }
} else if (empty($startDate) && empty($endDate)) {

    // Handle daily events when startDate and endDate are blank
    $currentDate = date('Y-m-d'); // Start from today
    $endDate = date('Y-m-d', strtotime('+365 days')); // Display for the next 30 days (or adjust as needed)

    while (strtotime($currentDate) <= strtotime($endDate)) {
        $dayVisible = false;
        if (isset($value->dayTypes) && !empty($value->dayTypes)) {

            $dayVisible = false;

            $startDateArray = getThithis($value->dayTypes);





            if ($value->dayTypes == getWeekday($currentDate)) {
                // echo "<br>".$value->refDataName."/22<br>";
                $dayVisible = true;
            } else if (getWeekdayOccurrence($currentDate) == $value->dayTypes) {
                // echo "<br>".$value->refDataName."/33<br>";
                $dayVisible = true;
            } else {
                // echo "<br>".$value->refDataName."/44<br>";
                if (!empty($startDateArray)) {
                    $dateToCheck = date('Y-m-d', strtotime($currentDate));


                    $dayVisible = in_array($dateToCheck, $startDateArray) && (strtotime($currentDate) >= strtotime(date('Y-m-d'))) && in_array($value->dayTypes, $allowedTithis);
                }
            }
        } else if (!isset($value->dayTypes) && empty($value->dayTypes)) {
            $dayVisible = true;
        } else if (strtotime($currentDate) <= strtotime($endDate) && strtotime($currentDate) >= strtotime($endDate)) {
            $dayVisible = true;
        }

        if ($dayVisible) {
        }
        $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
    }
}

?>

<script>
    let dayVisible = false;
    let startDateArray = result;
    if (startDate && endDate) {
        let currentDate = new Date(startDate);
        const endDateObj = new Date(endDate);
        while (currentDate <= endDateObj) {
            let currentDay = currentDate.getDay();
            if (dayVal) {


                ;





                if (dayVal == getWeekday(currentDate)) {
                    dayVisible = true;
                } else if (getWeekdayOccurrence(currentDate) == dayVal) {
                    dayVisible = true;
                } else {



                    if (startDateArray) {

                        const dateToCheck = currentDate.toISOString().split('T')[0];

                        // Check if the dateToCheck is in startDateArray and other conditions
                        dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(currentDate.toISOString().split('T')[0]) && tithis.includes(dayVal);
                    }

                }
            } else if (!dayVal || dayVal == '') {
                dayVisible = true;
            } else if (currentDate <= endDateObj && currentDate >= endDateObj) {
                dayVisible = true;
            }
            currentDate.setDate(currentDate.getDate() + 1);
        }
    } else if ((!startDate || startDate == '') && (!endDate || endDate == '') && dayVal && dayVal != '') {



        currentDate = new Date();
        endDate = new Date();
        endDate.setDate(endDate.getDate() + 365);



        while (currentDate <= endDate) {

            if (dayVal && dayVal.length > 0) {
                dayVisible = false;



                // Check conditions
                if (dayVal === getWeekday(currentDate)) {
                    // Example: value.refDataName logic here (optional)
                    dayVisible = true;
                } else if (getWeekdayOccurrence(currentDate) === dayVal) {
                    // Example: value.refDataName logic here (optional)
                    dayVisible = true;
                } else {
                    if (startDateArray && startDateArray.length > 0) {
                        const dateToCheck = currentDate.toISOString().split('T')[0]; // Format to 'YYYY-MM-DD'

                        dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(new Date().toISOString().split('T')[0]) && tithis.includes(dayVal);
                    }
                }
            } else if (dayVal || dayVal.length === 0) {
                dayVisible = true;
            } else if (currentDate <= endDate && currentDate >= endDate) {
                dayVisible = true;
            }
            currentDate.setDate(currentDate.getDate() + 1);


        }
    } else if ((!startDate || startDate == '') && (!endDate || endDate == '')) {


        currentDate = new Date();
        endDate = new Date();
        endDate.setDate(endDate.getDate() + 365);

        while (currentDate <= endDate) {
            dayVisible = false;
            if (dayVal || dayVal.length > 0) {

                dayVisible = false;



                if (dayVal === getWeekday(currentDate)) {
                    // Example: value.refDataName logic here (optional)
                    dayVisible = true;
                } else if (getWeekdayOccurrence(currentDate) === dayVal) {
                    // Example: value.refDataName logic here (optional)
                    dayVisible = true;
                } else {
                    if (startDateArray && startDateArray.length > 0) {
                        const dateToCheck = currentDate.toISOString().split('T')[0]; // Format to 'YYYY-MM-DD'

                        dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(new Date().toISOString().split('T')[0]) && tithis.includes(dayVal);
                    }
                }
            } else if (dayVal || dayVal.length === 0) {
                dayVisible = true;
            } else if (currentDate <= endDate && currentDate >= endDate) {
                dayVisible = true;
            }

            currentDate.setDate(currentDate.getDate() + 1);
        }
    }





    function getWeekdayOccurrence(date) {
        // Convert the date string to a Date object
        const dateObj = new Date(date);

        // Get the weekday (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
        const weekday = dateObj.getDay();

        // Get the day of the month
        const dayOfMonth = dateObj.getDate();

        // Calculate the occurrence of the weekday in the month
        const occurrence = Math.ceil(dayOfMonth / 7);

        const weekdayNames = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'];
        const occurrences = ['', 'FIRST', 'SECOND', 'THIRD', 'FOURTH', 'FIFTH', 'SIXTH'];

        return `${occurrences[occurrence]} ${weekdayNames[weekday]}`;
    }


    function getWeekday(date) {
        // Convert the date string to a Date object
        const dateObj = new Date(date);

        // Get the weekday (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
        const weekday = dateObj.getDay();

        const weekdayNames = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'];

        return weekdayNames[weekday];
    }






    // console.log("today: " + today);
								// console.log("date: " + date);
								// console.log(date >= today);

								// let currentDate = new Date();
								// 								console.log("getWeekdayOccurrence: "+ getWeekdayOccurrence(currentDate));
								// console.log("dayVal: "+ dayVal);
								// console.log("getWeekday: "+ getWeekday(currentDate));

								/*let dayVisible = false;
								let startDateArray = result;
								if (startDate && endDate) {
									let currentDate = new Date(startDate);
									const endDateObj = new Date(endDate);
									while (currentDate <= endDateObj) {
										let currentDay = currentDate.getDay();
										console.log("getWeekdayOccurrence: " + getWeekdayOccurrence(currentDate));
										console.log("dayVal: " + dayVal);
										console.log("getWeekday: " + currentDate);
										if (dayVal) {


											if (dayVal == getWeekday(currentDate)) {
												dayVisible = true;
											} else if (getWeekdayOccurrence(currentDate) == dayVal) {
												dayVisible = true;
											} else {



												if (startDateArray) {

													const dateToCheck = currentDate.toISOString().split('T')[0];

													// Check if the dateToCheck is in startDateArray and other conditions
													dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(currentDate.toISOString().split('T')[0]) && tithis.includes(dayVal);
												}

											}
										} else if (!dayVal || dayVal == '') {
											dayVisible = true;
										} else if (currentDate <= endDateObj && currentDate >= endDateObj) {
											dayVisible = true;
										}
										currentDate.setDate(currentDate.getDate() + 1);
									}
								} else if ((!startDate || startDate == '') && (!endDate || endDate == '') && dayVal && dayVal != '') {



									currentDate = new Date();
									endDate = new Date();
									endDate.setDate(endDate.getDate() + 365);



									while (currentDate <= endDate) {
										console.log("getWeekdayOccurrence: " + getWeekdayOccurrence(currentDate));
										console.log("dayVal: " + dayVal);
										console.log("getWeekday: " + currentDate);
										if (dayVal && dayVal.length > 0) {
											dayVisible = false;



											// Check conditions
											if (dayVal === getWeekday(currentDate)) {
												// Example: value.refDataName logic here (optional)
												dayVisible = true;
											} else if (getWeekdayOccurrence(currentDate) === dayVal) {
												// Example: value.refDataName logic here (optional)
												dayVisible = true;
											} else {
												if (startDateArray && startDateArray.length > 0) {
													const dateToCheck = currentDate.toISOString().split('T')[0]; // Format to 'YYYY-MM-DD'

													dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(new Date().toISOString().split('T')[0]) && tithis.includes(dayVal);
												}
											}
										} else if (dayVal || dayVal.length === 0) {
											dayVisible = true;
										} else if (currentDate <= endDate && currentDate >= endDate) {
											dayVisible = true;
										}
										currentDate.setDate(currentDate.getDate() + 1);


									}
								} else if ((!startDate || startDate == '') && (!endDate || endDate == '')) {


									currentDate = new Date();
									endDate = new Date();
									endDate.setDate(endDate.getDate() + 365);

									while (currentDate <= endDate) {
										console.log("getWeekdayOccurrence: " + getWeekdayOccurrence(currentDate));
										console.log("dayVal: " + dayVal);
										console.log("getWeekday: " + currentDate);
										dayVisible = false;
										if (dayVal || dayVal.length > 0) {

											dayVisible = false;



											if (dayVal === getWeekday(currentDate)) {
												// Example: value.refDataName logic here (optional)
												dayVisible = true;
											} else if (getWeekdayOccurrence(currentDate) === dayVal) {
												// Example: value.refDataName logic here (optional)
												dayVisible = true;
											} else {
												if (startDateArray && startDateArray.length > 0) {
													const dateToCheck = currentDate.toISOString().split('T')[0]; // Format to 'YYYY-MM-DD'

													dayVisible = startDateArray.includes(dateToCheck) && currentDate >= new Date(new Date().toISOString().split('T')[0]) && tithis.includes(dayVal);
												}
											}
										} else if (dayVal || dayVal.length === 0) {
											dayVisible = true;
										} else if (currentDate <= endDate && currentDate >= endDate) {
											dayVisible = true;
										}

										currentDate.setDate(currentDate.getDate() + 1);
									}
								}*/
</script>