let jsonData=[],filteredData=[];const SHEET_ID="1aQiwbtrQu1QOIOkFrOYl9WkzT0mqnZqIwI9KJCXGb4E",API_KEY="AIzaSyDsqxZhhN2llBnbX13DldQ0ChLD1VZLs5s",SHEET_NAMES=["學校總表_英文"],COLLEGES_EN=["College of Medicine","College of Engineering","College of Management","College of Computing Intelligence"],COLLEGE_ALL_EN="Available to all four Colleges",PROGRAMS_EN=["Undergraduate students","Master's students","PHD students"],PROGRAM_ALL_EN="Available to all programs";async function fetchSheetData(e){document.getElementById("loading-message").style.display="block";let t=`https://sheets.googleapis.com/v4/spreadsheets/1aQiwbtrQu1QOIOkFrOYl9WkzT0mqnZqIwI9KJCXGb4E/values/${e}?key=AIzaSyDsqxZhhN2llBnbX13DldQ0ChLD1VZLs5s`;try{let l=await fetch(t),n=await l.json();if(!n.values||n.values.length<=1)return console.warn(`Sheet ${e} has no data. Skipped.`),[];return n.values.slice(1).map((t,l)=>{let n=extractPlainText(t[6]),a=extractPlainText(t[7]);return{id:l,rowIndex:l,國家:extractPlainText(t[1]),校名:formatCell(t[2],t[3]),網址:t[3]||"",學院:n,colleges:parseCollegesEN(n),學制:a,programs:parseProgramsEN(a),學系:formatCell(t[8]),更多資訊:"",sheetName:e}})}catch(a){return console.error("Failed to fetch Google Sheets:",a),[]}finally{document.getElementById("loading-message").style.display="none"}}async function fetchAllSheets(){for(let e of(jsonData=[],filteredData=[],SHEET_NAMES)){let t=await fetchSheetData(e);t.length>0&&(filteredData=[...jsonData=jsonData.concat(t)])}displayResults(),populateFilters(jsonData)}function extractPlainText(e){return e?e.toString().replace(/<\/?[^>]+(>|$)/g,""):""}function parseCollegesEN(e){let t=extractPlainText(e||"").trim();if(!t)return[];if(t.toLowerCase().includes("available to all four colleges"))return[...COLLEGES_EN];let l=t.split(/[,\s\/;|]+/g).map(e=>e.trim()).filter(Boolean),n=COLLEGES_EN.filter(e=>t.toLowerCase().includes(e.toLowerCase()));return n.length>0?n:[...new Set(l.filter(e=>COLLEGES_EN.includes(e)))]}function parseProgramsEN(e){let t=extractPlainText(e||"").trim();if(!t)return[];let l=t.toLowerCase(),n=l.includes("available to all programs")||l.includes("no restriction")||l.includes("no limit")||l.includes("unlimited")||l.includes("all programs")||"all"===l||"any"===l;if(n)return[...PROGRAMS_EN];let a=PROGRAMS_EN.filter(e=>l.includes(e.toLowerCase()));if(a.length>0)return a;let r=t.split(/[,\s\/;|]+/g).map(e=>e.trim()).filter(Boolean);return[...new Set(r.filter(e=>PROGRAMS_EN.includes(e)))]}function formatCell(e,t=null){if(!e)return"";let l=e.toString().replace(/\n/g,"
"),n=t&&t.startsWith("http")?t:"";return n?`${l}`:`${l}`}function populateFilters(e){let t=document.getElementById("country-filter"),l=document.getElementById("college-filter"),n=document.getElementById("type-filter");populateFilterOptions(t,[...new Set(e.map(e=>extractPlainText(e.國家)))]),populateFilterOptions(l,COLLEGES_EN),populateFilterOptions(n,PROGRAMS_EN)}function populateFilterOptions(e,t){e.innerHTML='',t.forEach(t=>{let l=document.createElement("option");l.value=t,l.textContent=t,e.appendChild(l)})}function search(){let e=document.getElementById("search-input").value.trim().toLowerCase(),t=document.getElementById("country-filter").value,l=document.getElementById("college-filter").value,n=document.getElementById("type-filter").value;filteredData=jsonData.filter(a=>(""===e||Object.values(a).some(t=>"string"==typeof t&&t.toLowerCase().includes(e)))&&(""===t||a.國家===t)&&(""===l||(a.colleges||[]).includes(l))&&(""===n||(a.programs||[]).includes(n))),displayResults()}function displayResults(){let e=document.getElementById("table-body"),t=document.getElementById("total-count");e.innerHTML="",filteredData.forEach(t=>{let l=t.sheetName,n=document.createElement("tr");n.innerHTML=`