<div style="margin-bottom:20px; font-family:Vazir, Tahoma; direction:rtl; text-align:right;">

<!-- فیلتر کمیته -->
<div style="margin-top:10px; margin-bottom:15px;">
<label for="committeeFilter">کمیته:</label>
<select id="committeeFilter" style="padding:6px; border:1px solid #ccc; border-radius:6px; margin-right:10px; min-width:260px;">
<option value="">همه کمیته‌ها</option>
</select>

<input id="searchBox" type="text" placeholder="🔍 جستجو در عنوان مصوبات..."
style="padding:6px 10px; border:1px solid #ccc; border-radius:6px; width:240px; margin-right:10px;">
</div>

<!-- جدول مصوبات -->
<div style="max-height:450px; overflow-y:auto;">
<table id="resTable" class="aui" style="width:100%; border-collapse:collapse; text-align:center;">
<thead style="position:sticky; top:0;">
<tr>
<th style="width:180px;">کمیته</th>
<th>عنوان مصوبه</th>
<th style="width:110px;">تاریخ صورتجلسه</th>
</tr>
</thead>
<tbody id="resBody"></tbody>
</table>
</div>
</div>

<style>
 @font-face {
font-family: "Vazir";
src: url("/download/attachments/85041909/Vazir-Regular.woff2") format("woff2");
}
 

/* ===============================
Workflow / Document Report Styling
RTL – Hospital UI
Theme: Yellow Variant
=============================== */

#resTable {
width: 100% !important;
border-collapse: separate !important;
border-spacing: 0 !important;
font-family: Vazir, IRANSans, Tahoma;
background: #fff;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 8px 24px rgba(230,195,0,.08); /* زرد پررنگ */
}

#resTable thead th {
background: #E0D28C!important; /* زرد پررنگ */
color: #fff !important;
font-weight: 700;
font-size: 13px;
text-align: right !important;
padding: 10px 12px !important;
border: none !important;
}

#resTable tbody tr {
transition: background .2s ease;
}

#resTable tbody tr:nth-child(even) {
background: #FFF6BF; /* زرد کم‌رنگ */
}

#resTable tbody tr:hover {
background: #FFEFA3 !important; /* زرد کمی تیره‌تر */
}

#resTable tbody td {
padding: 10px 12px !important;
font-size: 13px;
color: #4A3E0F; /* رنگ متن تیره برای خوانایی */
border-bottom: 1px solid #FFEFA3; /* مرز زرد روشن */
text-align: right !important;
vertical-align: middle;
}

/* لینک عنوان */
#resTable tbody td a {
color: #E6C300; /* زرد پررنگ */
font-weight: 600;
text-decoration: none;
}
#resTable tbody td a:hover {
color: #C4A700; /* زرد تیره‌تر */
text-decoration: underline;
}
</style>

<script>
(async () => {
const resBody = document.getElementById("resBody");
const committeeFilter = document.getElementById("committeeFilter");
const searchBox = document.getElementById("searchBox");

const pageId = AJS?.params?.pageId || "106496438";
const baseUrl = AJS?.params?.baseURL || "";

let allData = [];

const committees = [
"کمیته بهداشت محیط","کمیته مدیریت اطلاعات سلامت و فناوری اطلاعات","کمیته پیشگیری و کنترل عفونت",
,"کمیته مدیریت خطر حوادث و بلایا و حفاظت فنی و بهداشت کار","کمیته طب انتقال خون",
"کمیته اخلاق بالینی","کارگروه استوارت شیپ","کارگروه آموزش","کمیته تغذیه بالینی",
"کارگروه اتاق عمل","کمیته مرگ و میر،عوارض و آسیب‌شناسی نسوج",
,"کمیته ترویج زایمان طبیعی و ایمن و ترویج تغذیه با شیر مادر",
"کمیته اقتصاد درمان","کمیته درمان، دارو و تجهیزات پزشکی",
"کمیته پایش و سنجش کیفیت", "تیم رهبری و مدیریت","کمیته ارتقای خدمات اورژانس"
];

committees.forEach(c => {
const opt = document.createElement("option");
opt.value = c;
opt.textContent = c;
committeeFilter.appendChild(opt);
});

function normalizeCommittee(str) {
return (str || "").replace(/\u200c/g, "").replace(/\s+/g, " ").trim().replace(/^کمیته\s+/g, "");
}

function parsePersianDate(dateStr) {
if (!dateStr) return 0;
const parts = String(dateStr).split("/");
if (parts.length !== 3) return 0;
return parseInt(parts[0].padStart(4,"0") + parts[1].padStart(2,"0") + parts[2].padStart(2,"0"), 10) || 0;
}

function getMeetingDate(item) {
return (item && (item.meetingDate || item.date || "")) ? String(item.meetingDate || item.date).trim() : "";
}

function renderTable(data) {
resBody.innerHTML = "";
if (!data || data.length === 0) {
resBody.innerHTML = `<tr><td colspan="3" style="color:#555;text-align:center;">❌ هیچ مصوبه‌ای یافت نشد</td></tr>`;
return;
}

data.sort((a, b) => parsePersianDate(getMeetingDate(b)) - parsePersianDate(getMeetingDate(a)));

data.forEach((item, index) => {
const tr = document.createElement("tr");
const dateVal = getMeetingDate(item) || "-";
tr.innerHTML = `
<td>${item.committee || "-"}</td>
<td>${item.title || "-"}</td>
<td>${dateVal}</td>
`;
resBody.appendChild(tr);
});
}

async function filterValidPages(data) {
const validData = [];
for (const item of data) {
try {
const res = await fetch(`${baseUrl}/rest/api/content/${item.pageId}`, { credentials: "include" });
if (res.ok) validData.push(item);
} catch {}
}
return validData;
}

async function updateJSON(cleanData) {
const fileName = "public_resolutions.json";
const blob = new Blob([JSON.stringify(cleanData, null, 2)], { type: "application/json" });
const formData = new FormData();
formData.append("file", blob, fileName);

await fetch(`${baseUrl}/rest/api/content/${pageId}/child/attachment?filename=${fileName}`, {
method: "POST",
credentials: "include",
headers: { "X-Atlassian-Token": "no-check" },
body: formData
});
}

try {
resBody.innerHTML = `<tr><td colspan="3" style="color:#555;text-align:center;">⏳ در حال بارگذاری...</td></tr>`;
const attRes = await fetch(`${baseUrl}/rest/api/content/${pageId}/child/attachment`, { credentials: "include" });
const attData = await attRes.json();
const jsonAttachment = (attData.results || []).find(a => (a.title || "").includes("public_resolutions.json"));
if (!jsonAttachment) throw new Error("فایل JSON یافت نشد!");

const jsonUrl = `${baseUrl}${jsonAttachment._links.download}`;
const response = await fetch(jsonUrl, { credentials: "include" });
if (!response.ok) throw new Error("خطا در بارگذاری JSON");

const data = await response.json();
allData = Array.isArray(data) ? data : [];

const validData = await filterValidPages(allData);
if (validData.length !== allData.length) {
await updateJSON(validData);
allData = validData;
}

renderTable(allData);

const filterFn = () => {
const selected = normalizeCommittee(committeeFilter.value).toLowerCase();
const search = (searchBox.value || "").trim().toLowerCase();
const filtered = allData.filter(d => {
const committee = normalizeCommittee(d.committee).toLowerCase();
const title = (d.title || "").trim().toLowerCase();
return (!selected || committee === selected) && (!search || title.includes(search));
});
renderTable(filtered);
};

committeeFilter.addEventListener("change", filterFn);
searchBox.addEventListener("input", filterFn);

} catch (err) {
resBody.innerHTML = `<tr><td colspan="3" style="color:red; text-align:center;">خطا در بارگذاری داده‌ها</td></tr>`;
}
})();
</script>