<!-- فونت وزیر -->
<link href="https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/font-face.css" rel="stylesheet" />

<h1 style="text-align:center;font-family:Vazir,Tahoma;color:#333;font-weight:600;">
مستندات اعتبار بخشی
</h1>

<!-- جستجو و فیلتر -->
<div id="search-filters" style="display:flex;justify-content:center;flex-wrap:wrap;gap:10px;margin:30px 0;">
  <input id="search" placeholder="جستجو بر اساس عنوان"
    style="flex:1 1 300px;max-width:400px;padding:10px 15px;border:none;background:#f3f3f3;border-radius:30px;font-family:Vazir;outline:none;text-align:right;">

  <select id="filter-mahvar"
    style="padding:10px 15px;border-radius:25px;border:1px solid #ccc;font-family:Vazir;">
    <option value="">انتخاب محور</option>
  </select>

  <select id="filter-zirmohvar"
    style="padding:10px 15px;border-radius:25px;border:1px solid #ccc;font-family:Vazir;">
    <option value="">انتخاب زیرمحور</option>
  </select>
</div>

<div id="sections-container" style="padding:0 40px 60px;direction:rtl;"></div>

<div id="no-result"
  style="display:none;text-align:center;color:#b40074;font-weight:600;margin-top:20px;">
هیچ سندی پیدا نشد
</div>

<style>
.card{
  background:#fff;border-radius:18px;box-shadow:0 4px 10px rgba(0,0,0,.1);
  padding:20px;display:flex;justify-content:space-between;flex-direction:row-reverse;
  font-family:Vazir;transition:.3s
}
.card:hover{transform:scale(1.03)}
.card-content{text-align:right;flex:1;padding-right:15px}
.card-content h3{margin:0 0 8px;font-size:14px}
.card-content ul{margin:0;padding-right:15px;font-size:13px}
.open-btn{
  background:#00A9C3;color:#fff;border-radius:25px;
  padding:4px 18px;text-decoration:none;font-size:13px;display:inline-block;margin-top:12px
}
.card-icon{background:#f7f7f7;border-radius:16px;padding:12px}
.card-icon img{width:2cm;height:2cm}
.section-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:20px}
@media(max-width:900px){.section-grid{grid-template-columns:1fr}}
</style>

<script>
/* ================= تنظیمات ================= */
const pageId = "85045112";
const fileName = "form-data.json";
let docs = [];

const sections = [
  {title:"رهبری و مدیریت",value:"رهبری و مدیریت"},
  {title:"مراقبت و درمان",value:"مراقبت و درمان"},
  {title:"حمایت از گیرنده خدمت",value:"حمایت از گیرنده خدمت"}
];

const zirmohvarOptions = {
  "رهبری و مدیریت":[
    "رهبری و مدیریت کیفیت","مدیریت خطر، حوادث و بلایا",
    "مدیریت منابع انسانی و سلامت حرفه‌ای","مدیریت خدمات پرستاری",
    "فناوری و مدیریت اطلاعات سلامت","بهداشت محیط","مدیریت تجهیزات پزشکی"
  ],
  "مراقبت و درمان":[
    "مراقبت‌های عمومی بالینی","مراقبت‌های حاد و اورژانس",
    "مراقبت‌های جراحی و بیهوشی","مراقبت‌های مادر و نوزاد",
    "پیشگیری و کنترل عفونت","مدیریت دارویی",
    "خدمات تصویربرداری","خدمات آزمایشگاه","طب انتقال خون","خدمات سرپایی"
  ],
  "حمایت از گیرنده خدمت":[
    "تأمین تسهیلات برای گیرنده خدمت","احترام به حقوق گیرنده خدمت"
  ]
};

const sanadIcons = {
  "آیین":"https://kb.h-soft.ir/download/thumbnails/85045112/ایین نامه.png",
  "پروتکل":"https://kb.h-soft.ir/download/thumbnails/85045112/پروتکل.png",
  "خط":"https://kb.h-soft.ir/download/thumbnails/85045112/خط مشی.png",
  "دستور":"https://kb.h-soft.ir/download/thumbnails/85045112/دستورالعمل.png",
  "راهنما":"https://kb.h-soft.ir/download/thumbnails/85045112/راهنما.png",
  "روش":"https://kb.h-soft.ir/download/thumbnails/85045112/روش اجرایی.png",
  "فلوچارت":"https://kb.h-soft.ir/download/thumbnails/85045112/فلوچارت.png",
  "کتابچه":"https://kb.h-soft.ir/download/thumbnails/85045112/کتابچه.png",
  "سند":"https://kb.h-soft.ir/download/thumbnails/85045112/سند.png"
};

function detectSanadIcon(type){
  if(!type) return sanadIcons["سند"];
  for(const k in sanadIcons){
    if(type.includes(k)) return sanadIcons[k];
  }
  return sanadIcons["سند"];
}

/* ================= کارت ================= */
function createCard(doc){
  let fileUrl = null;

  if(doc.document_file){
    if(doc.document_file.startsWith("data:")){
      try{
        const [m,d]=doc.document_file.split(",");
        const mime=m.match(/:(.*?);/)[1];
        const bin=atob(d);
        const arr=new Uint8Array(bin.length);
        for(let i=0;i<bin.length;i++)arr[i]=bin.charCodeAt(i);
        fileUrl=URL.createObjectURL(new Blob([arr],{type:mime}));
      }catch(e){console.error(e);}
    }
    else if(doc.document_file.startsWith("http") || doc.document_file.startsWith("/")){
      fileUrl=doc.document_file;
    }
    else{
      fileUrl=`/download/attachments/${pageId}/${doc.document_file}`;
    }
  }

  const card=document.createElement("div");
  card.className="card";
  card.innerHTML=`
    <div class="card-content">
      <h3>${doc.document_name||"-"}</h3>
      <ul>
        <li>زیرمحور: ${doc.zirmohvar||"-"}</li>
        <li>نوع سند: ${doc.noe_sanad||"-"}</li>
        <li>شماره استاندارد: ${doc.standard_no||"-"}</li>
        <li>تاریخ بازنگری: ${doc.date||"-"}</li>
      </ul>
      ${fileUrl
        ? `<a href="${fileUrl}" target="_blank" class="open-btn">مشاهده</a>`
        : `<span class="open-btn" style="background:#ccc">فایل ندارد</span>`
      }
    </div>
    <div class="card-icon">
      <img src="${detectSanadIcon(doc.noe_sanad)}">
    </div>`;
  return card;
}

/* ================= رندر ================= */
function renderSections(){
  const s=document.getElementById("search").value.toLowerCase();
  const m=document.getElementById("filter-mahvar").value;
  const z=document.getElementById("filter-zirmohvar").value;
  const c=document.getElementById("sections-container");
  c.innerHTML="";
  let found=false;

  sections.forEach(sec=>{
    const list=docs.filter(d=>
      d.mahvar===sec.value &&
      (!s||(d.document_name||"").toLowerCase().includes(s)) &&
      (!m||d.mahvar===m)&&(!z||d.zirmohvar===z)
    );
    if(!list.length)return;
    found=true;
    const h=document.createElement("h2");
    h.style.textAlign="center";h.textContent=sec.title;
    const g=document.createElement("div");g.className="section-grid";
    list.reverse().forEach(d=>g.appendChild(createCard(d)));
    c.append(h,g);
  });

  document.getElementById("no-result").style.display=found?"none":"block";
}

/* ================= JSON ================= */
async function loadJSON(){
  const r=await fetch(`/rest/api/content/${pageId}/child/attachment`,{credentials:"include"});
  const j=await r.json();
  const f=j.results.find(a=>a.title===fileName);
  if(!f)return;
  const url=new URL(f._links.download,location.origin);
  docs=JSON.parse(await fetch(url,{credentials:"include"}).then(r=>r.text()));
  sections.forEach(s=>{
    const o=document.createElement("option");
    o.value=s.value;o.textContent=s.title;
    document.getElementById("filter-mahvar").appendChild(o);
  });
  renderSections();
}

document.getElementById("search").addEventListener("input",renderSections);
document.getElementById("filter-mahvar").addEventListener("change",()=>{
  const v=filterMahvar.value;
  filterZirmohvar.innerHTML='<option value="">انتخاب زیرمحور</option>';
  (zirmohvarOptions[v]||[]).forEach(x=>{
    const o=document.createElement("option");
    o.value=o.textContent=x;
    filterZirmohvar.appendChild(o);
  });
  renderSections();
});
document.getElementById("filter-zirmohvar").addEventListener("change",renderSections);

loadJSON();
</script>