
//外部文件调用，根据程序返回值来决定是否显示wifi和4g页面
window.addEventListener('DOMContentLoaded', () => {
  const langPara = Array.from(document.querySelectorAll('div > p')).find(p =>
    getComputedStyle(p.parentElement).float === 'right' &&
    (p.textContent.includes('Language') || p.textContent.includes('语言'))
  );

  if (langPara) {
    for (let node of langPara.childNodes) {
      if (node.nodeType === Node.TEXT_NODE && /(Language|语言)/.test(node.textContent)) {
        node.textContent = node.textContent.replace(/(Language|语言)/, 'Language');
        break;
      }
    }
  }
});

function updatePageTitle() {
    const titleMap = {
        "编码器": "H.265/H.264 Encoder",
        "H.264 Encoder": "H.265/H.264 Encoder",
		"Encoder": "H.265/H.264 Encoder"
        // 可以继续添加其他映射关系
    };

    if (titleMap[document.title]) {
        document.title = titleMap[document.title];
    }
}

document.addEventListener('DOMContentLoaded', updatePageTitle);

function ShowWifi()
{
	$.ajax({
		type:"GET",
		url: "/get_sys",
		dataType:"xml",
		cache:false,
		success:function(data){
			var wifi_dev_exist=$(data).find("sys").children("wifi_dev_exist").text();
			var b_frame_mode=$(data).find("sys").children("b_frame_mode").text();
			if(b_frame_mode==0) {
				$("#osd_3").show();
				$("#output3").show();
			}
			else {
				$("#osd_3").hide();
				$("#output3").hide();
			}
			if(wifi_dev_exist==0) {
				$("#wifi").hide();
			}
			else {
				$("#wifi").show();
			}
		}
	});
}

function findstr(str, cha, num) {
	var x = str.indexOf(cha);
	for (var i = 0; i < num; i++) {
		x = str.indexOf(cha, x + i);
	}
	return x;
}

function changeIP(str, tmp1) {
	var sn = findstr(str, '/', 3);
	if (sn == "-1") {
		sn = findstr(str, ':', 2);
	}
	var newstr = "";
	var tmp2 = str.substring(sn, str.length);
	newstr = tmp1 + hostip + tmp2;
	return newstr;
}

// SVG 图标定义
const menuIcons = {
	status: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M5 12l-2 0l9 -9l9 9l-2 0" /><path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" /><path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" /></svg>',
	encoder: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z" /><path d="M8 4l0 16" /><path d="M16 4l0 16" /><path d="M4 8l4 0" /><path d="M4 16l4 0" /><path d="M4 12l16 0" /><path d="M16 8l4 0" /><path d="M16 16l4 0" /></svg>',
	osd: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M13 15c-2 0 -5 1 -5 5" /><path d="M4 11m0 2a2 2 0 0 1 2 -2h5a2 2 0 0 1 2 2v5a2 2 0 0 1 -2 2h-5a2 2 0 0 1 -2 -2z" /><path d="M4 7v-2a1 1 0 0 1 1 -1h2" /><path d="M11 4h2" /><path d="M17 4h2a1 1 0 0 1 1 1v2" /><path d="M20 11v2" /><path d="M20 17v2a1 1 0 0 1 -1 1h-2" /></svg>',
	system: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.325 4.317c.426 -1.756 2.924 -1.756 3.35 0a1.724 1.724 0 0 0 2.573 1.066c1.543 -.94 3.31 .826 2.37 2.37a1.724 1.724 0 0 0 1.065 2.572c1.756 .426 1.756 2.924 0 3.35a1.724 1.724 0 0 0 -1.066 2.573c.94 1.543 -.826 3.31 -2.37 2.37a1.724 1.724 0 0 0 -2.572 1.065c-.426 1.756 -2.924 1.756 -3.35 0a1.724 1.724 0 0 0 -2.573 -1.066c-1.543 .94 -3.31 -.826 -2.37 -2.37a1.724 1.724 0 0 0 -1.065 -2.572c-1.756 -.426 -1.756 -2.924 0 -3.35a1.724 1.724 0 0 0 1.066 -2.573c-.94 -1.543 .826 -3.31 2.37 -2.37c1 .608 2.296 .07 2.572 -1.065z"/><path d="M9 12a3 3 0 1 0 6 0a3 3 0 0 0 -6 0"/></svg>'
};

/**
 * 显示菜单（统一接口）
 * @param {string} first - 一级菜单标识 (index, encode, osd, system)
 * @param {string} second - 二级菜单标识
 *
 * 注意：为了向后兼容，第一个参数如果是数字则忽略（旧代码传 0/1 表示语言）
 */
function showMenu(first, second) {
	// 向后兼容：如果第一个参数是数字，则视为旧调用方式
	if (typeof first === 'number') {
		first = second;
		second = arguments[2] || '';
	}
	generateMenu(first, second);
}

/**
 * 生成菜单 HTML
 * 使用 i18n.t() 获取翻译文本
 */
function generateMenu(first, second) {
	const t = key => i18n.t(key);

	let menu = '<ul class="navbar-nav pt-lg-3">';

	// 状态显示 (无子菜单)
	menu += `<li class="nav-item">
		<a class="nav-link${first === 'index' ? ' active' : ''}" href="index.html">
			<span class="nav-link-icon">${menuIcons.status}</span>
			<span class="nav-link-title">${t('menu.status')}</span>
		</a>
	</li>`;

	// 编码设置 (有子菜单)
	menu += `<li class="nav-item${first === 'encode' ? ' active' : ''} dropdown">
		<a class="nav-link dropdown-toggle${first === 'encode' ? ' show' : ''}" href="Output.html?output=0" aria-expanded="${first === 'encode'}">
			<span class="nav-link-icon">${menuIcons.encoder}</span>
			<span class="nav-link-title">${t('menu.encoder')}</span>
		</a>
		<div class="dropdown-menu${first === 'encode' ? ' show' : ''}">
			<a class="dropdown-item${second === 'OutputMain' ? ' active' : ''}" href="Output.html?output=0">${t('menu.mainStream')}</a>
			<a class="dropdown-item${second === 'OutputSub1' ? ' active' : ''}" href="Output.html?output=1">${t('menu.subStream1')}</a>
			<a class="dropdown-item${second === 'OutputSub2' ? ' active' : ''}" href="Output.html?output=2">${t('menu.subStream2')}</a>
			<a class="dropdown-item${second === 'OutputSub3' ? ' active' : ''}" id="output3" href="Output.html?output=3">${t('menu.subStream3')}</a>
			<a class="dropdown-item${second === 'VideoSet' ? ' active' : ''}" href="VideoSet.html">${t('menu.videoSettings')}</a>
			<a class="dropdown-item${second === 'AudioSet' ? ' active' : ''}" href="AudioSet.html">${t('menu.audioSettings')}</a>
			${(typeof i18n !== 'undefined' && typeof i18n.isZh === 'function' ? i18n.isZh() : true)
        ? `<a class="dropdown-item${second === 'GB28181' ? ' active' : ''}" href="GB28181.html">${t('menu.gb28181')}</a>`
        : ''}
		</div>
	</li>`;

	// OSD设置 (有子菜单)
	menu += `<li class="nav-item${first === 'osd' ? ' active' : ''} dropdown">
		<a class="nav-link dropdown-toggle${first === 'osd' ? ' show' : ''}" href="OutputOsd.html?output=0" aria-expanded="${first === 'osd'}">
			<span class="nav-link-icon">${menuIcons.osd}</span>
			<span class="nav-link-title">${t('menu.osd')}</span>
		</a>
		<div class="dropdown-menu${first === 'osd' ? ' show' : ''}">
			<a class="dropdown-item${second === 'OutputMainOsd' ? ' active' : ''}" href="OutputOsd.html?output=0">${t('menu.mainStream')}</a>
			<a class="dropdown-item${second === 'OutputSub1Osd' ? ' active' : ''}" href="OutputOsd.html?output=1">${t('menu.subStream1')}</a>
			<a class="dropdown-item${second === 'OutputSub2Osd' ? ' active' : ''}" href="OutputOsd.html?output=2">${t('menu.subStream2')}</a>
			<a class="dropdown-item${second === 'OutputSub3Osd' ? ' active' : ''}" id="osd_3" href="OutputOsd.html?output=3">${t('menu.subStream3')}</a>
			<a class="dropdown-item${second === 'LogoUpload' ? ' active' : ''}" href="LogoUpload.html">${t('menu.logoUpload')}</a>
		</div>
	</li>`;

	// 系统设置 (有子菜单)
	menu += `<li class="nav-item${first === 'system' ? ' active' : ''} dropdown">
		<a class="nav-link dropdown-toggle${first === 'system' ? ' show' : ''}" href="NetworkSet.html" aria-expanded="${first === 'system'}">
			<span class="nav-link-icon">${menuIcons.system}</span>
			<span class="nav-link-title">${t('menu.system')}</span>
		</a>
		<div class="dropdown-menu${first === 'system' ? ' show' : ''}">
			<a class="dropdown-item${second === 'NetworkSet' ? ' active' : ''}" href="NetworkSet.html">${t('menu.network')}</a>
			<a class="dropdown-item${second === 'AdvancedSet' ? ' active' : ''}" href="AdvancedSet.html">${t('menu.advancedSettings')}</a>
			<a class="dropdown-item${second === 'RecordSet' ? ' active' : ''}" href="RecordSet.html">${t('menu.record')}</a>
			<a class="dropdown-item${second === 'WifiSet' ? ' active' : ''}" id="wifi" href="WifiSet.html" style="display:none;">${t('menu.wifi')}</a>
			<a class="dropdown-item${second === 'PasswordSet' ? ' active' : ''}" href="PasswordSet.html">${t('menu.password')}</a>
			<a class="dropdown-item${second === 'SerialSet' ? ' active' : ''}" href="SerialSet.html">${t('menu.serial')}</a>
			<a class="dropdown-item${second === 'SystemUpdate' ? ' active' : ''}" href="SystemUpdate.html">${t('menu.update')}</a>
			<a class="dropdown-item${second === 'Reboot' ? ' active' : ''}" href="Reboot.html">${t('menu.reboot')}</a>
			<a class="dropdown-item${second === 'Restart' ? ' active' : ''}" href="Restart.html">${t('menu.timedReboot')}</a>
			<a class="dropdown-item${second === 'Reset' ? ' active' : ''}" href="Reset.html">${t('menu.factoryReset')}</a>
		</div>
	</li>`;

	menu += '</ul>';
	$("#mainnav_div").html(menu);
}

// 保留原函数名以向后兼容（旧页面可能直接调用）
function get_menu_chn(first, second) {
	generateMenu(first, second);
}

function get_menu_eng(first, second) {
	generateMenu(first, second);
}

function isAppleDevice() {
  const userAgent = navigator.userAgent.toLowerCase();
  return (
    userAgent.includes('mac os x') ||    // macOS
    userAgent.includes('iphone') ||      // iOS (iPhone)
    userAgent.includes('ipad') ||        // iOS (iPad)
    userAgent.includes('ipod')           // iOS (iPod)
  );
}
