<div class="standardContainer">
    <div class="ui basic segment">
        <h2>Set Proxy Root</h2>
        <p>For all routing not found in the proxy rules, request will be redirected to the proxy root server.</p>
        <div class="ui form">
            <div class="field">
                <label>Proxy Root</label>
                <input type="text" id="proxyRoot" onchange="checkRootRequireTLS(this.value);">
                <small>E.g. localhost:8080</small>
            </div>
            <div class="field">
                <div class="ui checkbox">
                    <input type="checkbox" id="rootReqTLS" >
                    <label>Root require TLS Connection <br><small>(i.e. Your proxy target starts with https://)</small></label>
                </div>
            </div>
        </div>
        <br>
        <button class="ui basic button" onclick="setProxyRoot()"><i class="teal home icon" ></i> Update Proxy Root</button>
    </div>
</div>
<script>
    function initRootInfo(){
        $.get("/api/proxy/list?type=root", function(data){
            if (data == null){

            }else{
                $("#proxyRoot").val(data.Domain);
                checkRootRequireTLS(data.Domain);
            }
        });
    }
    initRootInfo();

    function checkRootRequireTLS(targetDomain){
       $.ajax({
            url: "/api/proxy/tlscheck",
            data: {url: targetDomain},
            success: function(data){
                if (data.error != undefined){

                }else if (data == "https"){
                    $("#rootReqTLS").parent().checkbox("set checked");
                }else if (data == "http"){
                    $("#rootReqTLS").parent().checkbox("set unchecked");
                }
            }
       })
    }


    function setProxyRoot(){
        var newpr = $("#proxyRoot").val();
        if (newpr.trim() == ""){
            $("#proxyRoot").parent().addClass('error');
            return
        }else{
            $("#proxyRoot").parent().removeClass('error');
        }

        var rootReqTls = $("#rootReqTLS")[0].checked;

        //Create the endpoint by calling add
        $.ajax({
            url: "/api/proxy/add",
            data: {"type": "root", tls: rootReqTls, ep: newpr},
            success: function(data){
                if (data.error != undefined){
                    alert(data.error);
                }else{
                    //OK
                    initRootInfo();
                    msgbox("Proxy Root Updated")
                }
            }
        });

    }
</script>