این کد رو جوری اصلاح کن که رزرو شده ها رو نشون نده

let data_visit;

function timeToMinutes(time) {
    const [hours, minutes] = time.split(':').map(Number);
    return hours * 60 + minutes;
}

function gregorianToJalali(gy, gm, gd) {
    const g_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    const j_days_in_month = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29];

    gy -= 1600;
    gm -= 1;
    gd -= 1;

    let g_day_no = 365 * gy + Math.floor((gy + 3) / 4) - Math.floor((gy + 99) / 100) + Math.floor((gy + 399) / 400);

    for (let i = 0; i < gm; ++i) {
        g_day_no += g_days_in_month[i];
    }

    if (gm > 1 && ((gy % 4 === 0 && gy % 100 !== 0) || (gy % 400 === 0))) {
        g_day_no++;
    }
    g_day_no += gd;

    let j_day_no = g_day_no - 79;
    const j_np = Math.floor(j_day_no / 12053);
    j_day_no %= 12053;

    let jy = 979 + 33 * j_np + 4 * Math.floor(j_day_no / 1461);
    j_day_no %= 1461;

    if (j_day_no >= 366) {
        jy += Math.floor((j_day_no - 1) / 365);
        j_day_no = (j_day_no - 1) % 365;
    }

    let jm;
    for (jm = 0; jm < 11 && j_day_no >= j_days_in_month[jm]; ++jm) {
        j_day_no -= j_days_in_month[jm];
    }

    const jd = j_day_no + 1;
    return [jy, jm + 1, jd];
}

async function calculateFirstAvailableSlot(data) {
    const startMinutes = timeToMinutes(data.starttime);
    const endMinutes = timeToMinutes(data.endtime);
    const totalSlots = (endMinutes - startMinutes) / data.time_each_visit;

    const totalReserved = data.reserved_internet_turn + data.reserved_manualt_turn + data.reserved_organization_turn;

    if (totalReserved < totalSlots) {
        const firstAvailableSlotMinutes = startMinutes + totalReserved * data.time_each_visit;
        const hours = Math.floor(firstAvailableSlotMinutes / 60);
        const minutes = firstAvailableSlotMinutes % 60;
        const timeString = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;

        const [gy, gm, gd] = data.visit_date.split('-').map(Number);
        const [jy, jm, jd] = gregorianToJalali(gy, gm, gd);

        return `${timeString} ${jy}/${jm.toString().padStart(2, '0')}/${jd.toString().padStart(2, '0')}`;
    } else {
        return "زمانی برای ویزیت خالی نیست.";
    }
}

async function get_data() {
    const url = 'http://localhost/TakingTurns/visits/';

    try {
        const response = await send_requests(url, {}, 'GET');

        if (!response.ok) {
            throw new Error('Network response was not ok');
        }

        return await response.json();
    } catch (error) {
        console.error('Error fetching data:', error);
    }
}

function addRow(data) {
    const bodyTable = document.getElementById("data_table");

    data.forEach(async (item, index) => {
        const firstAvailableSlot = await calculateFirstAvailableSlot(item);

        bodyTable.insertAdjacentHTML('beforeend', `
            <tr>
                <td>${item.doctor}</td>
                <td>${"اطفال"}</td>
                <td>${item.medical_center}</td>
                <td>${firstAvailableSlot}</td>
                <td><button class="btn btn-primary waves-effect waves-light" data-bs-target="#editUser" data-bs-toggle="modal" type="button" id="data-${index}">نمایش</button></td>
            </tr>
        `);
    });
}

document.addEventListener('DOMContentLoaded', async function() {
    data_visit = await get_data();
    if (data_visit && data_visit.length) {
        addRow(data_visit);
    }
});

document.getElementById('filterInput').addEventListener('input', function() {
    const filterValue = this.value.toLowerCase();
    const rows = document.querySelectorAll('#data_table tr');

    rows.forEach(row => {
        const cells = row.querySelectorAll('td');
        const match = Array.from(cells).some(cell => cell.textContent.toLowerCase().includes(filterValue));
        row.style.display = match ? '' : 'none';
    });
});

document.addEventListener('click', (event) => {
    if (event.target.matches('.btn.btn-primary.waves-effect.waves-light')) {
        const buttonId = event.target.id;
        const index = parseInt(buttonId.split('-')[1], 10);
        add_button(data_visit[index]);
    }
});

function add_button(doctorData) {
    const slots = generateTimeSlots(doctorData.starttime, doctorData.endtime, doctorData.time_each_visit, doctorData.reserved_internet_turn + doctorData.reserved_manualt_turn + doctorData.reserved_organization_turn);

    // استفاده از gregorianToJalali برای تبدیل تاریخ
    const [gy, gm, gd] = doctorData.visit_date.split('-').map(Number);
    const [jy, jm, jd] = gregorianToJalali(gy, gm, gd);

    document.getElementById('visitInfo').innerHTML = `
        <div class="col-12 col-md-6">
            <strong>نام دکتر:</strong> ${doctorData.doctor}
        </div>
        <div class="col-12 col-md-6">
            <strong>تخصص:</strong> ${doctorData.specialty}
        </div>
        <div class="col-12 col-md-6">
            <strong>تاریخ ویزیت:</strong> ${jy}/${jm.toString().padStart(2, '0')}/${jd.toString().padStart(2, '0')}
        </div>
        <div class="col-12 col-md-6">
            <strong>مرکز درمانی:</strong> ${doctorData.medical_center}
        </div>
        <div class="col-12">
            <strong>بازه‌های زمانی قابل رزرو:</strong>
            <ul class="list-group">
                ${slots.map(slot => `
                    <li class="list-group-item ${slot.reserved ? 'disabled' : 'list-group-item-action'}">
                        ${slot.timeStart} - ${slot.timeEnd} ${slot.reserved ? '(رزرو شده)' : '<button class="btn btn-sm btn-primary">رزرو</button>'}
                    </li>
                `).join('')}
            </ul>
        </div>
    `;
}

function generateTimeSlots(startTime, endTime, timeEachVisit, reservations) {
    let slots = [];
    const startMinutes = timeToMinutes(startTime);
    const endMinutes = timeToMinutes(endTime);
    const totalSlots = (endMinutes - startMinutes) / timeEachVisit;

    for (let i = 0; i < totalSlots; i++) {
        const slotStart = startMinutes + i * timeEachVisit;
        const slotEnd = slotStart + timeEachVisit;
        const hoursStart = Math.floor(slotStart / 60);
        const minutesStart = slotStart % 60;
        const hoursEnd = Math.floor(slotEnd / 60);
        const minutesEnd = slotEnd % 60;
        const timeStringStart = `${hoursStart.toString().padStart(2, '0')}:${minutesStart.toString().padStart(2, '0')}`;
        const timeStringEnd = `${hoursEnd.toString().padStart(2, '0')}:${minutesEnd.toString().padStart(2, '0')}`;
        const reserved = i < reservations;
        slots.push({ timeStart: timeStringStart, timeEnd: timeStringEnd, reserved });
    }

    return slots;
}