@model OpcPks.Core.Models.CertRequestModel

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h4>🛡️ 하니웰 Experion 전용 인증서 생성기</h4>
        </div>
        <div class="card-body">
            <form asp-action="GenerateCertificate" method="post">
                <div class="row mb-4">
                    <div class="col-md-6">
                        <label class="form-label fw-bold">시스템 구성</label>
                        <select asp-for="IsRedundant" class="form-select" id="systemType">
                            <option value="false">Standalone (단일 서버)</option>
                            <option value="true">Redundant (이중화 서버)</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-bold">어플리케이션 이름</label>
                        <input asp-for="ApplicationName" class="form-control" placeholder="OpcPksClient" />
                    </div>
                </div>

                <hr />

                <h5 class="text-secondary"><span class="badge bg-info">Primary</span> 서버 정보</h5>
                <div class="row mb-3">
                    <div class="col-md-4">
                        <label class="form-label">Host Name</label>
                        <input asp-for="PrimaryHostName" class="form-control" placeholder="예: HONPKS" />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label text-warning">IP Address (Yellow)</label>
                        <input asp-for="PrimaryIpA" class="form-control" placeholder="192.168.0.20" />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label text-success">IP Address (Green)</label>
                        <input asp-for="PrimaryIpB" class="form-control" placeholder="192.168.1.20" />
                    </div>
                </div>

                <h5 class="text-secondary mt-4"><span class="badge bg-secondary" id="secondaryBadge">Secondary</span> 서버 정보</h5>
                <div class="row mb-3" id="secondaryFields">
                    <div class="col-md-4">
                        <label class="form-label">Host Name</label>
                        <input asp-for="SecondaryHostName" class="form-control sec-input" placeholder="예: HONPKS" />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label text-warning">IP Address (Yellow)</label>
                        <input asp-for="SecondaryIpA" class="form-control sec-input" placeholder="192.168.0.21" />
                    </div>
                    <div class="col-md-4">
                        <label class="form-label text-success">IP Address (Green)</label>
                        <input asp-for="SecondaryIpB" class="form-control sec-input" placeholder="192.168.1.21" />
                    </div>
                </div>

                <div class="card bg-light border-warning mt-5">
                    <div class="card-body">
                        @if (ViewBag.IsCertExists == true)
                        {
                            <div class="alert alert-warning d-flex align-items-center">
                                <span class="fs-4 me-3">⚠️</span>
                                <div>
                                    <strong>기존 인증서가 이미 존재합니다!</strong><br />
                                    새로 생성 시 기존 서버와의 통신 신뢰관계(Trust)가 깨질 수 있습니다.
                                </div>
                            </div>
                            
                            <div class="form-check mb-3">
                                <input class="form-check-input" type="checkbox" id="chkUnlock" onchange="toggleCertBtn()">
                                <label class="form-check-label text-danger fw-bold" for="chkUnlock">
                                    [위험 인지] 기존 인증서를 무시하고 새로 생성하는 것에 동의합니다.
                                </label>
                            </div>

                            <button type="submit" id="btnGenerate" class="btn btn-danger btn-lg w-100" disabled>
                                🔒 인증서가 이미 존재하여 잠겨있습니다
                            </button>
                        }
                        else
                        {
                            <button type="submit" id="btnGenerate" class="btn btn-primary btn-lg w-100">
                                🚀 인증서 생성 및 자동 적용 시작
                            </button>
                        }
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        $(document).ready(function () {
            // 1. 이중화 선택 로직
            $('#systemType').change(function () {
                const isRedundant = $(this).val() === 'true';
                $('.sec-input').prop('disabled', !isRedundant);
                
                if(isRedundant) {
                    $('#secondaryBadge').removeClass('bg-secondary').addClass('bg-info');
                } else {
                    $('#secondaryBadge').removeClass('bg-info').addClass('bg-secondary');
                }
            });

            $('#systemType').trigger('change');
        });

        // 2. 버튼 잠금 해제 로직
        function toggleCertBtn() {
            const isChecked = document.getElementById('chkUnlock').checked;
            const btn = document.getElementById('btnGenerate');
            
            if(isChecked) {
                btn.disabled = false;
                btn.innerText = "🔥 인증서 새로 생성 (강제 실행)";
                btn.classList.replace('btn-danger', 'btn-warning');
            } else {
                btn.disabled = true;
                btn.innerText = "🔒 인증서가 이미 존재하여 잠겨있습니다";
                btn.classList.replace('btn-warning', 'btn-danger');
            }
        }
    </script>
}