| Linux in-mum-web1499.main-hosting.eu 5.14.0-503.40.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 5 06:06:04 EDT 2025 x86_64 Path : /home/u901718425/domains/portal.urbanpillar.in/public_html/ |
| Current File : /home/u901718425/domains/portal.urbanpillar.in/public_html/task.js |
// Calender
function renderCalendar(year, month) {
const date = new Date(year, month);
const currentMonth = date.getMonth();
const currentYear = date.getFullYear();
const firstDay = new Date(currentYear, currentMonth, 1).getDay();
const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
const today = new Date();
let calendarHtml = `
<div class="d-flex justify-content-between align-items-center mb-2">
<button class="btn btn-sm btn-outline-secondary rounded-0 border-0 p-2 d-flex justify-content-center align-content-center fs-8 dateCol" id="prevMonth"><i class="fas fa-chevron-left"></i></button>
<strong class="fs-5 text-dark">${date.toLocaleString('default', { month: 'long' })} ${currentYear}</strong>
<button class="btn btn-sm btn-outline-secondary rounded-0 border-0 p-2 d-flex justify-content-center align-content-center fs-8 dateCol" id="nextMonth"><i class="fas fa-chevron-right"></i></button>
</div>
<div class="row m-0 gap-1 fs-8">
<div class="dateCol fw-bold">Sun</div>
<div class="dateCol fw-bold">Mon</div>
<div class="dateCol fw-bold">Tue</div>
<div class="dateCol fw-bold">Wed</div>
<div class="dateCol fw-bold">Thu</div>
<div class="dateCol fw-bold">Fri</div>
<div class="dateCol fw-bold">Sat</div>
`;
let day = 1;
for (let i = 0; i < 6; i++) {
calendarHtml += '';
for (let j = 0; j < 7; j++) {
if ((i === 0 && j < firstDay) || day > daysInMonth) {
calendarHtml += '<div class="dateCol"></div>';
} else {
const isToday =
day === today.getDate() &&
currentMonth === today.getMonth() &&
currentYear === today.getFullYear();
const isFutureDate =
new Date(currentYear, currentMonth, day) > today;
calendarHtml += `<div class="dateCol ${isFutureDate ? 'disable':'day'} ${isToday ? 'today' : ''}" data-date="${day}">${day}</div>`;
day++;
}
}
calendarHtml += '';
if (day > daysInMonth) break;
}
calendarHtml += '</div>';
$('#calender').html(calendarHtml);
highLightDate(currentYear, currentMonth);
}
const today = new Date();
renderCalendar(today.getFullYear(), today.getMonth());
function highLightDate(currentYear, currentMonth) {
$.get('/api/get_attendance_dates.php', {
month: currentMonth + 1,
year: currentYear
}, function (response) {
const attendanceDays = JSON.parse(response);
attendanceDays.forEach(day => {
$(`#calender .day[data-date="${day}"]`).addClass('highlight');
});
});
$.get('/api/get_leave_dates.php', {
month: currentMonth + 1,
year: currentYear
}, function (response) {
const attendanceDays = JSON.parse(response);
console.log(attendanceDays);
attendanceDays.forEach(({day, type}) => {
const $dayElement = $(`#calender .day[data-date="${day}"]`);
if (type.toLowerCase() === 'leave') {
$dayElement.addClass('bg-danger text-white');
} else if (type.toLowerCase() === 'wfh') {
$dayElement.addClass('bg-warning text-dark');
} else if (type.toLowerCase().includes('first half')) {
$dayElement.addClass('bg-info text-dark'); // Optional class for first half
} else if (type.toLowerCase().includes('second half')) {
$dayElement.addClass('bg-primary text-white'); // Optional class for second half
} else {
$dayElement.addClass('highlight text-dark'); // default case
}
});
});
}
$(document).ready(function () {
const $btn = $('#login');
const $edit = $('#edit');
$edit.hide();
const now = new Date();
let year = now.getFullYear();
let month = now.getMonth();
const todayDate = now.getDate();
const monthNames = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
$(document).on('click','#prevMonth',() => {
month = month - 1;
renderCalendar(year, month);
});
$(document).on('click', '#nextMonth', () => {
month = month + 1;
renderCalendar(year, month);
});
const modal = new bootstrap.Modal(document.getElementById('taskModal'));
// Modal logic
let taskList = [];
let selectedDateStr = null;
let clickDate = null;
function fetchTask(date){
selectedDateStr = date; // e.g., "2025-05-06"
clickDate = `${year}-${month + 1}-${selectedDateStr}`;
const selectedDate = new Date(clickDate);
let taskList;
const today = new Date();
today.setHours(0, 0, 0, 0);
const allowedStartDate = new Date(today);
allowedStartDate.setDate(today.getDate() - 3); // today - 3 days
// Check if the selected date is today's date
const isToday = selectedDate.getTime() === today.getTime();
$('#selectedDateLabel').text(`${monthNames[month]} ${selectedDateStr}, ${year}`);
$('#taskDisplayList').empty().html('');
// Fetch tasks
$.get('/api/fetch_task.php', { date: clickDate }, function (response) {
const tasks = JSON.parse(response);
if (tasks && tasks.length) {
taskList = tasks;
tasks.forEach(task => {
const taskItems = task.split(',').map(t => t.trim()).filter(t => t !== '');
taskItems.forEach((taskText, index) => {
$('#taskDisplayList').append(`
<div class="d-flex align-items-center border-bottom py-2">
<div class="w-100 ms-3">
<div class="d-flex align-items-start w-100 align-items-center gap-2">
<span>${index + 1}</span>
<span class="w-auto">${taskText}</span>
</div>
</div>
</div>`);
});
});
} else {
$('#taskDisplayList').append(`
<div class="d-flex align-items-center border-bottom py-2">
<div class="w-100 ms-3">
<div class="d-flex align-items-start w-100 align-items-center gap-2">
<span>0</span>
<span class="w-auto">No Task Found.</span>
</div>
</div>
</div>`);
}
console.log(allowedStartDate, selectedDate);
// ✅ Allow modal only if within today and last 3 days
if (selectedDate >= allowedStartDate && selectedDate <= today) {
$('#taskInput').val(tasks || '');
$('#taskList').empty();
$edit.show();
modal.show();
}
});
}
$(document).on('click', '#calender .day', function () {
fetchTask($(this).data('date'));
});
$('#edit').click(function (e) {
e.preventDefault();
modal.show();
});
$('#taskInput').on('input', function () {
const value = $(this).val().trim();
$('#taskList').empty(); // Clear previous list
if (value !== '') {
const tasks = value.split(',').map(task => task.trim()).filter(task => task !== '');
tasks.forEach(task => {
$('#taskList').append(`<li class="list-group-item">${task}</li>`);
});
}
});
$('#addTask').click(function () {
const task = $('#taskInput').val().trim();
if (!task) return;
const $button = $(this);
const BtnHtml = $button.html();
$button.html(`<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>`);
navigator.geolocation.getCurrentPosition(async function (position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const ipData = await fetch("https://ipinfo.io/json?token=2fe79d49197052") // Replace with your token from ipinfo.io
.then(res => res.json())
.catch(() => null);
if (!ipData) {
alert("Failed to get IP info");
return;
}
const payload = {
task: task,
latitude: lat,
longitude: lon,
ip: ipData.ip,
hostname: ipData.hostname,
city: ipData.city,
region: ipData.region,
country: ipData.country,
loc: ipData.loc,
org: ipData.org,
postal: ipData.postal,
timezone: ipData.timezone,
date: clickDate
};
$.ajax({
url: '/api/save_task.php',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(payload),
success: function (response) {
const tasks = JSON.parse(response);
$('#taskDisplayList').empty();
$('#taskDisplayList').html('');
fetchTask(selectedDateStr);
$('#taskInput').val('');
modal.hide();
$button.html(BtnHtml);
},
error: function () {
alert("Failed to save task.");
}
});
}, function (error) {
alert("Geolocation failed: " + error.message);
}, {
enableHighAccuracy: true
});
});
});