<%+header%>
<!--
    This module is a demo to configure MTK' proprietary WiFi driver.
    Basic idea is to bypass uci and edit wireless profile (mt76xx.dat) directly.
    LuCI's WiFi configuration is more logical and elegent, but it's quite tricky to
    translate uci into MTK's WiFi profile (like we did in "uci2dat").

    Hua Shao <nossiac@163.com>
-->
<%
local mtkwifi = require("mtkwifi")
local devs = mtkwifi.get_all_devs()
%>
    <h2><a name="content">无线概况</a></h2>

    <div id="loading" style="margin-top: 1em; display: none;">
    <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:e;" />
        &nbsp;&nbsp;&nbsp; Please waiting while the page is loading......
    </div>

    <% if #devs == 0 then %>
    <fieldset class="cbi-section">
        <legend>No wireless device found.</legend>
    </fieldset>
    <% end %>

    <% for _,dev in ipairs(devs) do %>
    <fieldset class="cbi-section">
        <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
            <!-- physical device -->
            <tbody>
                <tr>
                    <td style="width:34px">
                        <img src="/luci-static/resources/icons/wifi_big.png" style="float:left; margin-right:10px" />
                    </td>
                    <td colspan="2" style="text-align:left">
                        <big><strong title="<%=dev.profile%>"> Generic Mediatek <%=dev.devname%></strong></big>
                        <%
                        local diff = mtkwifi.diff_profile(dev.profile)
                        %>
                        <br/>
                        <span><strong>模式:</strong>  <% if dev.ApCliEnable == "1" then %> APCli <% else %> Master AP<% end %> </span>
                    </td>
                    <td style="width:310px;text-align:right">
                        <input class="cbi-button cbi-button-reload" style="width:100px" title="重启无线" value="重启无线" type="button" onclick='wifi_reload("<%=luci.dispatcher.build_url("admin", "network", "wifi", "reload", dev.devname)%>");' />
                        <input class="cbi-button cbi-button-edit" style="width:100px" title="配置无线高级设置" value="高级设置" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_view", dev.devname)%>'" />
                        <input class="cbi-button cbi-button-add" style="width:100px" title="添加SSID" value="添加" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_add_view", dev.devname,dev.vifs.__prefix)%>'" />
                    </td>
                </tr>
                <!-- /physical device -->
                <!-- network list -->
                <% for _,vif in ipairs(dev.vifs) do %>
                <tr class="cbi-section-table-row cbi-rowstyle-1">
                    <td></td>
                    <td class="cbi-value-field" style="width:16px; padding:3px">
                        <% if vif.state == "up" then %>
                        <img src="/luci-static/resources/icons/signal-75-100.png" />
                        <% else %>
                        <img src="/luci-static/resources/icons/signal-none.png" />
                        <% end %>
                    </td>
                    <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px"><strong>接口:</strong> <%=vif.vifname%> | <strong>无线名称:</strong> <%=vif.__ssid%> | <strong>信道:</strong> <%=vif.__channel or dev.Channel%>
                        <br/>
                        <% if vif.state == "up" then %>
                        <strong>BSSID:</strong>:<%=vif.__bssid%> | <strong>无线模式:</strong> <%=dev.WirelessModeList[tonumber(vif.__wirelessmode or dev.WirelessMode)]%>
                        <% else %>
                        Wireless is disabled or not associated
                        <% end %>
                    </td>
                    <td class="cbi-value-field" style="text-align:right">
                        <% if not vif.state then %>
                        <input class="cbi-button cbi-button-apply" style="width:100px" title="启用无线" value="启用" type="button" onclick="alert('Please restart manually to create this interface.')" />
                        <% elseif vif.state == "up" then %>
                        <input class="cbi-button cbi-button-reset" style="width:100px" title="禁用无线" value="停用" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_disable", vif.vifname)%>'" />
                        <% else %>
                        <input class="cbi-button cbi-button-apply" style="width:100px" title="禁用无线" value="启用" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_enable", vif.vifname)%>'" />
                        <% end %>
                        <input class="cbi-button cbi-button-edit" style="width:100px" title="修改无线基本设置" value="基本设置" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_cfg_view", dev.devname, vif.vifname)%>'" />
                        <input class="cbi-button cbi-button-remove" style="width:100px" title="删除网络" value="移除" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_del", dev.devname, vif.vifname)%>'" />
                    </td>
                </tr>
                <% end %>
                <!-- /network list -->
                <!-- apcli list -->
                <% if dev.apcli then %>
                <tr>
                    <td></td>
                    <td class="cbi-value-field" style="width:16px; padding:3px">
                        <% if dev.apcli.state == "up" then %>
                        <img src="/luci-static/resources/icons/signal-75-100.png" />
                        <% else %>
                        <img src="/luci-static/resources/icons/signal-none.png" />
                        <% end %>
                    </td>
                    <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px"><strong>接口:</strong> <%=dev.apcli.devname%> | <strong>类型:</strong> 中继 | <strong>状态:</strong> <%=dev.apcli.status%>
                        <br/>
                        <% if dev.apcli.status == "已连接" then %>
                        <strong>BSSID:</strong>:<%=dev.apcli.bssid%> |
                        <strong>上级SSID:</strong>:<%=dev.apcli.ssid%>
                        <% else %>
                        无线中继未连接
                        <% end %>
                    </td>
                    <td style="text-align:right">

                        <input class="cbi-button cbi-button-reload" style="width:100px" title="连接中继" value="连接中继" type="button" onclick='wifi_reload("<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_connect", dev.devname,dev.apcli.vifname)%>")' />

                        <input class="cbi-button cbi-button-remove" style="width:100px" title="断开中继" value="断开中继" type="button" onclick='wifi_reload("<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_disconnect", dev.devname, dev.apcli.vifname)%>")' />

                        <input class="cbi-button cbi-button-edit" style="width:100px" title="修改中继参数" value="中继设置" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_cfg_view", dev.devname, dev.apcli.vifname)%>'" />
                    </td>
                </tr>
                <% end %>
                <!-- /apcli list -->
            </tbody>
        </table>
    </fieldset>
    <% end %>

<!--
    <h2><a id="content" name="content"><%:Station List%></a></h2>

    <fieldset class="cbi-section">
        <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
            <tr class="cbi-section-table-titles">
                <th class="cbi-section-table-cell"></th>
                <th class="cbi-section-table-cell"><%:SSID%></th>
                <th class="cbi-section-table-cell"><%:MAC-Address%></th>
                <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
                <th class="cbi-section-table-cell"><%:Signal%></th>
                <th class="cbi-section-table-cell"><%:Noise%></th>
                <th class="cbi-section-table-cell"><%:RX Rate%></th>
                <th class="cbi-section-table-cell"><%:TX Rate%></th>
            </tr>
            <tr class="cbi-section-table-row cbi-rowstyle-2">
                <td class="cbi-value-field" colspan="8">
                    <em><%:Collecting data...%></em>
                </td>
            </tr>
        </table>
    </fieldset>
-->

<script type="text/javascript">
function wifi_reload(url) {
    window.scrollTo(0, 0);
    document.getElementById('loading').style.display="";
    XHR.get(url, null,
        function(x)
        {
            console.log(x);
            document.getElementById("loading").style.display="none";
            window.location.reload();
        }
    );
}
</script>

<%+footer%>
