<div class="standardContainer">
    <div class="ui basic segment">
        <h2>Subdomain</h2>
        <p>Subdomains are a way to organize and identify different sections of a website or domain. They are essentially a prefix to the main domain name, separated by a dot. <br>For example, in the domain "blog.example.com," "blog" is the subdomain.</p>
    </div>
    <div style="width: 100%; overflow-x: auto; margin-bottom: 1em;">
        <table class="ui celled sortable unstackable compact table">
            <thead>
                <tr>
                    <th>Matching Domain</th>
                    <th>Proxy To</th>
                    <th>TLS/SSL Verification</th>
                    <th>Basic Auth</th>
                    <th class="no-sort" style="min-width: 7.2em;">Actions</th>
                </tr>
            </thead>
            <tbody id="subdList">
            
            </tbody>
        </table>
    </div>
    <button class="ui icon right floated basic button" onclick="listSubd();"><i class="green refresh icon"></i> Refresh</button>
    <br><br>
</div>

<script>
    function listSubd(){
        $.get("/api/proxy/list?type=subd", function(data){
            $("#subdList").html(``);
            if (data.error !== undefined){
                    $("#subdList").append(`<tr>
                    <td data-label="" colspan="3"><i class="remove icon"></i> ${data.error}</td>
                </tr>`);
            }else if (data.length == 0){
                $("#subdList").append(`<tr>
                    <td data-label="" colspan="3"><i class="checkmark icon"></i> No Subdomain Proxy Record</td>
                </tr>`);
            }else{
                data.forEach(subd => {
                    let tlsIcon = "";
                    let subdData = encodeURIComponent(JSON.stringify(subd));
                    if (subd.RequireTLS){
                        tlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
                    }

                    $("#subdList").append(`<tr eptuuid="${subd.RootOrMatchingDomain}" payload="${subdData}" class="subdEntry">
                        <td data-label="" editable="false"><a href="//${subd.RootOrMatchingDomain}" target="_blank">${subd.RootOrMatchingDomain}</a></td>
                        <td data-label="" editable="true" datatype="domain">${subd.Domain} ${tlsIcon}</td>
                        <td data-label="" editable="true" datatype="skipver">${!subd.SkipCertValidations?`<i class="ui green check icon"></i>`:`<i class="ui yellow exclamation circle icon" title="TLS/SSL Verification will be skipped on this host"></i>`}</td>
                        <td data-label="" editable="true" datatype="basicauth">${subd.RequireBasicAuth?`<i class="ui green check icon"></i>`:`<i class="ui grey remove icon"></i>`}</td>
                        <td class="center aligned" editable="true" datatype="action" data-label="">
                            <button class="ui circular mini basic icon button editBtn" onclick='editEndpoint("subd","${subd.RootOrMatchingDomain}")'><i class="edit icon"></i></button>
                            <button class="ui circular mini red basic icon button" onclick='deleteEndpoint("subd","${subd.RootOrMatchingDomain}")'><i class="trash icon"></i></button>
                        </td>
                    </tr>`);
                });
            }
        });
    }   

    //Bind on tab switch events
    tabSwitchEventBind["subd"] = function(){
        listSubd();
    }
</script>