<?php $awnTemplates=tpl_list();$awnUplink=(int)($_POST['uplink_router_id']??($uplink_default['id']??0));$awnMode=(string)($_POST['apply_wan_mode']??'dhcp'); ?>
<div class="content-wrapper android-network-wizard"><main class="awn-main"><header class="awn-heading"><div><span>Smart Network Wizard</span><h1>المعالج الذكي</h1><p>تشخيص واقتراح واختبار الإعدادات قبل أي تطبيق فعلي.</p></div><a href="automation_comms.php"><i class="fa fa-sitemap"></i><b>الشبكة</b></a></header><div class="awn-steps"><span class="is-active"><b>1</b>الخطة</span><span><b>2</b>التشخيص</span><span><b>3</b>التطبيق</span><span><b>4</b>الاختبار</span></div><?php if($test_result): ?><div class="awn-message is-<?= $test_result['ok']?'ok':'danger' ?>"><i class="fa fa-info-circle"></i><span><?= h($test_result['msg']) ?></span></div><?php endif; ?>
<section class="awn-section"><div class="awn-section-head"><span>1</span><div><small>Scenario Templates</small><h2>اقتراح خطة الشبكة</h2></div></div><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="tpl_recommend"><label><span>قالب السيناريو</span><select name="template"><?php foreach($awnTemplates as $key=>$tpl): ?><option value="<?= h($key) ?>" <?= $template===$key?'selected':'' ?>><?= h($tpl['title']) ?></option><?php endforeach; ?></select></label><button class="is-primary" type="submit"><i class="fa fa-star"></i><span>اقتراح الخطة</span></button></form><?php if($tpl_rec&&!empty($tpl_rec['ok'])): ?><?php $plan=$tpl_rec['plan']; ?><div class="awn-plan"><header><h3><?= h($tpl_rec['tpl']['title']??'الخطة المقترحة') ?></h3><p><?= h($tpl_rec['tpl']['desc']??'') ?></p></header><div><span>WAN</span><strong><?= h(($plan['wan_mode']??'-').' · '.($plan['wan_interface']??'-')) ?></strong></div><div><span>Transit</span><strong dir="ltr"><?= h($plan['transit_subnet']??'-') ?></strong></div><div><span>PPPoE</span><strong><?= (int)($plan['sectors_count']??0) ?> قطاع</strong></div><?php foreach($tpl_rec['notes']??[] as $note): ?><p class="awn-note"><?= $note ?></p><?php endforeach; ?><form method="post" onsubmit="return confirm('تطبيق الخطة وتحديث القطاعات بنمط Ensure؟')"><input type="hidden" name="action" value="tpl_apply"><input type="hidden" name="template" value="<?= h($plan['template']) ?>"><button class="is-apply" type="submit"><i class="fa fa-check"></i><span>تطبيق الخطة Ensure</span></button></form></div><?php endif; ?><?php if($tpl_apply): ?><div class="awn-result is-<?= !empty($tpl_apply['ok'])?'ok':'danger' ?>"><?= h($tpl_apply['msg']??$tpl_apply['error']??'') ?></div><?php endif; ?></section>
<section class="awn-section"><div class="awn-section-head"><span>2</span><div><small>WAN Detection</small><h2>تشخيص الاتصال</h2></div></div><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="detect_wan"><label><span>راوتر Uplink</span><select name="uplink_router_id"><option value="">اختر الراوتر</option><?php foreach($routers as $router): ?><option value="<?= (int)$router['id'] ?>" <?= $awnUplink===(int)$router['id']?'selected':'' ?>><?= h($router['name'].' · '.$router['role']) ?></option><?php endforeach; ?></select></label><div class="awn-pair"><label><span>WAN Interface</span><input name="wan_interface" dir="ltr" value="<?= h($_POST['wan_interface']??'ether1') ?>"></label><label><span>الوضع المتوقع</span><?php $chosen=(string)($_POST['chosen_wan_mode']??'auto'); ?><select name="chosen_wan_mode"><option value="auto">تلقائي</option><option value="static" <?= $chosen==='static'?'selected':'' ?>>Static</option><option value="dhcp" <?= $chosen==='dhcp'?'selected':'' ?>>DHCP</option><option value="pppoe" <?= $chosen==='pppoe'?'selected':'' ?>>PPPoE</option></select></label></div><button class="is-primary" type="submit"><i class="fa fa-search"></i><span>تشخيص WAN</span></button></form><?php if($recommendation): ?><div class="awn-result is-info"><?= $recommendation ?></div><?php endif; ?><?php if($diag): ?><div class="awn-result is-<?= !empty($diag['ok'])?'ok':'danger' ?>"><?php if(!empty($diag['ok'])): ?><?php $data=$diag['data']; ?><div class="awn-result-grid"><div><span>الوضع</span><strong><?= h($data['mode_detected']??'-') ?></strong></div><div><span>Gateway</span><strong dir="ltr"><?= h($data['gateway']??'-') ?></strong></div><div><span>Default Route</span><strong><?= !empty($data['has_default_route'])?'نعم':'لا' ?></strong></div><div><span>IP</span><strong dir="ltr"><?= h($data['static_ip']??'-') ?></strong></div></div><?php else: ?><?= h($diag['error']??'فشل التشخيص') ?><?php endif; ?></div><?php endif; ?></section>
<section class="awn-section"><div class="awn-section-head"><span>3</span><div><small>Connectivity Test</small><h2>اختبار الإنترنت</h2></div></div><form method="post" data-ua-android-native-layout><input type="hidden" name="action" value="ping_test"><label><span>راوتر Uplink</span><select name="uplink_router_id"><option value="">اختر الراوتر</option><?php foreach($routers as $router): ?><option value="<?= (int)$router['id'] ?>" <?= $awnUplink===(int)$router['id']?'selected':'' ?>><?= h($router['name'].' · '.$router['role']) ?></option><?php endforeach; ?></select></label><label><span>عنوان الاختبار</span><input name="ping_target" dir="ltr" value="<?= h($_POST['ping_target']??'1.1.1.1') ?>"></label><button class="is-test" type="submit"><i class="fa fa-exchange"></i><span>تنفيذ Ping</span></button></form><?php if($ping): ?><div class="awn-result is-<?= !empty($ping['ok'])?'ok':'danger' ?>"><?= !empty($ping['ok'])?'نجح اختبار Ping وتم استلام النتيجة.':h($ping['error']??'فشل الاختبار') ?></div><?php endif; ?></section>
<details class="awn-section awn-advanced"><summary><span>4</span><div><small>Advanced / Ensure</small><h2>تطبيق إعداد WAN</h2></div><i class="fa fa-chevron-down"></i></summary><form method="post" id="awnWanForm" data-ua-android-native-layout><input type="hidden" name="action" value="apply_wan"><label><span>راوتر Uplink</span><select name="uplink_router_id" required><option value="">اختر الراوتر</option><?php foreach($routers as $router): ?><option value="<?= (int)$router['id'] ?>" <?= $awnUplink===(int)$router['id']?'selected':'' ?>><?= h($router['name']) ?></option><?php endforeach; ?></select></label><div class="awn-pair"><label><span>WAN Interface</span><input name="wan_interface" dir="ltr" value="<?= h($_POST['wan_interface']??'ether1') ?>"></label><label><span>وضع WAN</span><select name="apply_wan_mode" id="awnWanMode"><option value="dhcp" <?= $awnMode==='dhcp'?'selected':'' ?>>DHCP</option><option value="static" <?= $awnMode==='static'?'selected':'' ?>>Static</option><option value="pppoe" <?= $awnMode==='pppoe'?'selected':'' ?>>PPPoE</option></select></label></div><div id="awnStatic" class="awn-mode-fields"><label><span>Static IP/Mask</span><input name="static_ip_cidr" dir="ltr" value="<?= h($_POST['static_ip_cidr']??'') ?>"></label><div class="awn-pair"><label><span>Gateway</span><input name="static_gateway" dir="ltr" value="<?= h($_POST['static_gateway']??'') ?>"></label><label><span>DNS</span><input name="static_dns" dir="ltr" value="<?= h($_POST['static_dns']??'') ?>"></label></div></div><div id="awnPppoe" class="awn-mode-fields awn-pair"><label><span>PPPoE Username</span><input name="pppoe_user" dir="ltr" value="<?= h($_POST['pppoe_user']??'') ?>"></label><label><span>PPPoE Password</span><input type="password" name="pppoe_pass" dir="ltr" value="<?= h($_POST['pppoe_pass']??'') ?>"></label></div><div id="awnDhcp" class="awn-mode-fields"><label><span>DNS اختياري</span><input name="dhcp_dns" dir="ltr" value="<?= h($_POST['dhcp_dns']??'') ?>"></label></div><button class="is-warning" type="submit" onclick="return confirm('تطبيق إعداد WAN على الراوتر بنمط Ensure؟')"><i class="fa fa-bolt"></i><span>تطبيق WAN الآن</span></button></form><?php if($apply): ?><div class="awn-result is-<?= !empty($apply['ok'])?'ok':'danger' ?>"><?= h($apply['msg']??$apply['error']??'') ?></div><?php endif; ?></details>
<section class="awn-section"><div class="awn-section-head"><span>5</span><div><small>Transit / Checklist</small><h2>فحص الربط الشامل</h2></div></div><div class="awn-action-grid"><form method="post"><input type="hidden" name="action" value="auto_detect_links"><button type="submit"><i class="fa fa-search"></i><span>اكتشاف الربط</span></button></form><form method="post"><input type="hidden" name="action" value="test_all"><button type="submit"><i class="fa fa-check-square-o"></i><span>اختبار شامل</span></button></form></div><form method="post" class="awn-transit" data-ua-android-native-layout><input type="hidden" name="action" value="apply_transit"><div class="awn-pair"><label><span>R1 Transit IP</span><input name="r1_transit_ip" dir="ltr" value="<?= h($_POST['r1_transit_ip']??'10.255.255.1/30') ?>"></label><label><span>R2 Transit IP</span><input name="r2_transit_ip" dir="ltr" value="<?= h($_POST['r2_transit_ip']??'10.255.255.2/30') ?>"></label></div><button class="is-apply" type="submit" onclick="return confirm('تطبيق الربط والمسارات بنمط Ensure؟')"><i class="fa fa-link"></i><span>تطبيق Transit</span></button></form><?php if($result): ?><div class="awn-result is-<?= !empty($result['ok'])?'ok':'danger' ?>"><?php if(($action??'')==='test_all'&&!empty($result['results'])): ?><div class="awn-check-list"><?php foreach($result['results'] as $check): ?><div><span><?= h($check['ar'].' · '.$check['k']) ?></span><b class="<?= !empty($check['ok'])?'is-pass':'is-fail' ?>"><?= !empty($check['ok'])?'PASS':'FAIL' ?></b></div><?php endforeach; ?></div><?php elseif(!empty($result['links'])): ?><div class="awn-result-grid"><?php foreach($result['links'] as $key=>$value): ?><div><span><?= h($key) ?></span><strong dir="ltr"><?= h($value) ?></strong></div><?php endforeach; ?></div><?php else: ?><?= h($result['msg']??implode(' · ',$result['msgs']??[])) ?><?php endif; ?></div><?php endif; ?></section></main></div><script>(function(){const mode=document.getElementById('awnWanMode');if(!mode)return;function toggle(){document.getElementById('awnStatic').style.display=mode.value==='static'?'grid':'none';document.getElementById('awnPppoe').style.display=mode.value==='pppoe'?'grid':'none';document.getElementById('awnDhcp').style.display=mode.value==='dhcp'?'grid':'none';}mode.addEventListener('change',toggle);toggle();})();</script>
