{% extends "base.html" %}
{% block title %}Certificate Requests{% endblock %}
{% block cs %}
    <style>
        ul { list-style: none; margin-top:20px;}
        #status{ color: red; font-weight: bold;}
        .csr-row{width: 200px; margin-top:10px; float:left;}
    </style>
{% endblock %}
{% block content %}
    <div style="float: left;">
    <h1>Certificate Requests</h1>
    {% if not csrs %}
        <div>No Certificate Requests</div>
    {% else %}
        <div id="status"></div>
        <ul>
        {% for c in csrs %}
            <li>
                <div class="csr-row" data-common_name="{{c.identity}}">
                    <a class="approve"
                       data-common_name="{{c.identity}}"
                       data-status="{{c.status}}" href="#">Approve</a>
                    <a class="deny"
                       data-common_name="{{c.identity}}"
                       data-status="{{c.status}}" href="#">Deny</a>
                    <a class="delete"
                       data-common_name="{{c.identity}}"
                       data-status="{{c.status}}" href="#">Delete</a>
                </div>
                <div style="float: left;">
                    <!-- {{c}}<br /> -->
                    Status: {{c.status}}<br />
                    Common Name: {{c.identity}}<br/>
                    Remote IP: {{c.remote_ip_address}}<br/>
                </div>
                <div style="float:none;"></div>
            </li>
        {% endfor %}
        </ul>
    </div>
    {% endif %}
    <div style="float: left;">
    <h1>ZMQ Keys Pending Authorization</h1>
    {% if auths|length < 1 %}
        <div>No ZMQ keys requiring authorization at this time.</div>
    {% else %}
        <div id="status_zmq"></div>
        <ul>
        {% for auth in auths %}
            <li>
                <div class="csr-row" data-common_name="{{auth.user_id}}">
                    <a class="approve_credential"
                       data-common_name="{{auth.user_id}}"
                       data-status="PENDING" href="#">Approve</a>
                    <a class="deny_credential"
                       data-common_name="{{auth.user_id}}"
                       data-status="PENDING" href="#">Deny</a>
                    <a class="delete_credential"
                       data-common_name="{{auth.user_id}}"
                       data-status="PENDING" href="#">Delete</a>
                </div>
                <div style="float: left;">
                    <!-- {{c}}<br /> -->
                    Status: PENDING<br />
                    Public Key: {{auth.user_id}}<br/>
                    Remote IP: {{auth.address}}<br/>
                </div>
                <div style="float:none;"></div>
            </li>
        {% endfor %}
        </ul>
    {% endif %}
        <div id="status_zmq_approved"></div>
        <ul>
        {% for auth in approved_auths %}
            <li>
                <div class="csr-row" data-common_name="{{auth.user_id}}">
                    <a class="deny_approved"
                       data-common_name="{{auth.user_id}}"
                       data-status="APPROVED" href="#">Deny</a>
                    <a class="delete_approved"
                       data-common_name="{{auth.user_id}}"
                       data-status="APPROVED" href="#">Delete</a>
                </div>
                <div style="float: left;">
                    <!-- {{c}}<br /> -->
                    Status: APPROVED<br />
                    Common Name: {{auth.user_id}}<br/>
                    Remote IP: {{auth.address}}<br/>
                </div>
                <div style="float:none;"></div>
            </li>
        {% endfor %}
        </ul>

        <div id="status_zmq_denied"></div>
        <ul>
        {% for auth in denied_auths %}
            <li>
                <div class="csr-row" data-common_name="{{auth.user_id}}">
                    <a class="approve_denied"
                       data-common_name="{{auth.user_id}}"
                       data-status="DENIED" href="#">Approve</a>
                    <a class="delete_denied"
                       data-common_name="{{auth.user_id}}"
                       data-status="DENIED" href="#">Delete</a>
                </div>
                <div style="float: left;">
                    <!-- {{c}}<br /> -->
                    Status: DENIED<br />
                    Common Name: {{auth.user_id}}<br/>
                    Remote IP: {{auth.address}}<br/>
                </div>
                <div style="float:none;"></div>
            </li>
        {% endfor %}
        </ul>
    </div>
{% endblock %}
{% block js %}
    <script language="JavaScript">

        function set_status(new_text){
            $("#status").html(new_text);
            $("#status").show();
        }

        $(document).ready(() => {
            if (localStorage.status) {
                set_status(localStorage.status);
                localStorage.removeItem("status");
            }
            else{
                $("#status").hide();
            }

            $('a.approve').each((i, obj)=>{
                if (obj.getAttribute('data-status') === 'APPROVED'){
                    obj.setAttribute('hidden', true);
                }
            });

            $('a.deny').each((i, obj)=>{
                if (obj.getAttribute('data-status') === 'DENIED'){
                    obj.setAttribute('hidden', true);
                }
            });

            $('a.approve').click(function (e){
                e.preventDefault();
                var element = $(this);
                var common_name = element.data("common_name");
                var url = "/admin/api/approve_csr/"+ common_name;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+common_name;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.deny').click(function (e){
                e.preventDefault();
                var element = $(this);
                var common_name = element.data("common_name");
                var url = "/admin/api/deny_csr/"+ common_name;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+common_name;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.delete').click(function (e){
                e.preventDefault();
                var element = $(this);
                var common_name = element.data("common_name");
                var url = "/admin/api/delete_csr/"+ common_name;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+common_name;
                        location.reload();
                    }
                })  ;

                return false;
            });

            $('a.approve_credential').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/approve_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });
                return false;
            });

            $('a.deny_credential').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/deny_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.delete_credential').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/delete_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.deny_approved').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/deny_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.delete_approved').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/delete_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.approve_denied').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/approve_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });

            $('a.delete_denied').click(function (e){
                e.preventDefault();
                var element = $(this);
                var user_id = element.data("common_name");
                var url = "/admin/api/delete_credential/"+ user_id;
                $.get({
                    url: url,
                    success: (data) => {
                        localStorage.status = data['status']+ " for "+user_id;
                        location.reload();
                    }
                });

                return false;
            });
        });
    </script>
{% endblock %}
