{% extends 'components/base_nhncloud.html' %}
{% load static %}
{% block main_area %}

<section>
    <div class="mt-3 row">
        <div class="mt-5 col-12 text-center">Create NKS Public-Subnet
            <br>
            <samp>충돌 여부 확인 후 문제 없을시 기본값 사용 가능.</samp>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4">
        </div>
        <div class="col-lg-4">
            <form method="post" action="{% url 'nhncloud:createOnlyCluster' %}">{% csrf_token %}
                <!-- vpc -->
                <div class="mb-3 mt-5">
                    <div class="row">
                        <div class="col-md-4 form-group">
                            <label for="vpc">VPC</label>
                        </div>
                        <div class="col-md-7">
                            <select class="form-control" name="vpc" id="vpc" onchange="vpcToSubnetList();">
                                <option value="select...">select...</option>
                                {% for var in vpcList %}
                                    {% for key, value in var.items %}
                                    <option value="{{ value }}">{{ key }}</option>
                                    <!-- <option value="{{ value }}">{{ key }}</option> -->
                                    {% endfor %}
                                {% endfor %}
                            </select>                                
                        </div>
                        <div class="col-md-1">
                            <a class="btn btn-primary float-right" href="#" data-bs-toggle="modal" data-bs-target="#getVpcListeModal"><i class="fa-solid fa-magnifying-glass"></i></a>
                        </div>
                    </div>        
                </div>
                <!-- subnet -->
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4 form-group">
                            <label for="publicSubnet">Subnet</label>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="publicSubnet" id="publicSubnet" readonly>
                        </div>
                    </div>        
                </div>
                <!-- region -->
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4 form-group">
                            <label for="region">Region</label>
                        </div>
                        <div class="col-md-8">
                            <select class="form-control" name="region" id="region" onchange="selectRegionPost(this.value);">
                                <option value="select...">select...</option>
                                <option value="kr1">판교</option>
                                <option value="kr2">평촌</option>
                            </select>                                
                        </div>
                        
                    </div>        
                </div>
                <hr>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4">
                            <label for="clusterName">Cluster Name</label>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="clusterName" id="clusterName" value="demo-cluster">
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4 form-group">
                            <label for="keyPair">KeyPair Name</label>
                        </div>
                        <div class="col-md-8">
                            <select class="form-control" name="KeyPair" id="KeyPair" value="selectKeyPair">
                                <option value="select...">select...</option>
                                <option value="keyPair-kr1">keyPair-kr1</option>
                                <option value="keyPair-kr2">keyPair-kr2</option>
                            </select>                                
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4">
                            <label for="tenantId">Tenant ID</label>
                        </div>
                        <div class="col-md-8">
                            <input type="password" class="form-control" name="tenantId" id="tenantId" value="{{ user.nhnc_api_tenant_id }}" readonly>
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4">
                            <label for="usrEmail">User Email</label>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="usrEmail" id="usrEmail" value="{{ user.nhnc_id }}">
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-4">
                            <label for="apiPw">API Password</label>
                        </div>
                        <div class="col-md-8">
                            <input type="password" class="form-control" name="apiPw" id="apiPw">
                        </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary float-right" onclick="createClickMsg();">Create</button>
            </form>
        </div>
        <div class="col-lg-4"></div>
    </div>
    <!-- modal -->
    <form method="post" action=".">{% csrf_token %}
        <div class="modal fade" id="getVpcListeModal" tabindex="-1" aria-labelledby="getVpcListeModal" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="getVpcListeModalTitle">API 비밀번호를 입력해주세요.</h5>
                        <!-- <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button> -->
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <div class="row">
                                <div class="col-md-4 form-group">
                                    <label for="region">Subnet Type</label>
                                </div>
                                <div class="col-md-8">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="subnetRadio" id="publicSubnetId" value="option1" checked>
                                        <label class="form-check-label" for="subnetRadio1">
                                            Public subnet
                                        </label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="subnetRadio" id="privateSubnetId" value="option2">
                                        <label class="form-check-label" for="subnetRadio2">
                                            Private subnet
                                        </label>
                                    </div>                               
                                </div>
                            </div>        
                        </div>
                        <!-- region -->
                        <div class="mb-3">
                            <div class="row">
                                <div class="col-md-4 form-group">
                                    <label for="region-pre">Region</label>
                                </div>
                                <div class="col-md-7">
                                    <select class="form-control" name="region-pre" id="region-pre">
                                        <option value="select...">select...</option>
                                        <option value="kr1">판교</option>
                                        <option value="kr2">평촌</option>
                                    </select>                                
                                </div>
                                <div class="col-md-1">
                                </div>
                            </div>        
                        </div>
                        <div class="mb-3">
                            <div class="row">
                                <div class="col-md-4">
                                    <label for="apiPw">API Password</label>
                                </div>
                                <div class="col-md-7">
                                    <input type="password" class="form-control" name="apiPw" id="apiPw">
                                    <input type="hidden" class="form-control" name="usrEmail" id="usrEmail" value="{{ user.nhnc_id }}">
                                    <input type="hidden" name="tenantId-pre" id="tenantId-pre" value="{{ user.nhnc_api_tenant_id }}">
                                    <input type="hidden" name="searchVpcList" id="searchVpcList" value="yes">
                                </div>
                                <div class="col-md-1">
                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-info float-right" onclick="alert('VPC리스트가 조회됩니다...')">Request</button>
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</section>
<script>
    function selectRegion(){
        var selectRegionValue;
        var regionPreValue;

        regionPreValue = document.getElementById('region-pre').value;
        
        console.log(selectRegionValue);

        if (regionPreValue == "kr1") {
            document.getElementById('vpc').value = "kr1";
        }
        else if (regionPreValue == "kr2") {
            document.getElementById('vpc').value = "kr2";
        }
        else {
            console.log('pass');
        }
        vpcToSubnetList();
        
    }

    function vpcToSubnetList(){
        let selectRegionValue = document.getElementById('vpc').value;
        
        console.log(selectRegionValue);

        var dict = {};
        var subDict = {};
        {% for x, y in subnetList.items %}
            {% for a, b in y.items %}
                subDict["{{ a }}"] = "{{ b | safe}}";
            {% endfor %}
            dict["{{ x }}"] = subDict;
            subDict = {};
        {% endfor %}

        document.getElementById('publicSubnet').value = dict[selectRegionValue]["id"];

    }

    function selectRegionPost(){
        var regionValue;
        
        regionValue = document.getElementById('region').value;

        if (regionValue == "kr1") {
            document.getElementById('KeyPair').value = "keyPair-kr1";
        }
        else if (regionValue == "kr2") {
            document.getElementById('KeyPair').value = "keyPair-kr2";
        }
        else {
            document.getElementById('KeyPair').value = "none";
        }
    }
    function createClickMsg(){
        alert('약 15분 이내로 Cluster가 생성됩니다.\n페이지 새로고침을 하지 말아주세요.\n생성이 완료되면 페이지가 변경됩니다.');
    }
</script>
{% endblock %}