@extends('admin.layouts.app')

@section('title', __('admin/ssl_certificates.manual_install_title'))

@section('content')
    <div class="container-fluid px-4">
        <x-admin.page-header
            :title="__('admin/ssl_certificates.manual_install_title')"
            :subtitle="__('admin/ssl_certificates.manual_install_subtitle')"
            icon="fas fa-certificate"
            :buttons="[
                ['label' => __('admin/common.cancel'), 'route' => route('admin.ssl-certificates.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
            ]"
        />

        <x-admin.alert />
        <x-admin.validation-errors />

        <div class="alert alert-info" role="alert">
            <i class="fas fa-info-circle me-1"></i>{{ __('admin/ssl_certificates.manual_install_intro') }}
        </div>

        <form method="POST" action="{{ route('admin.ssl-certificates.manual-install.store', $cert->id) }}">
            @csrf

            <x-admin.card>
                {{-- Certificate context (read-only) --}}
                <div class="row g-3 mb-4">
                    <div class="col-md-3">
                        <div class="form-text small mb-1">{{ __('admin/ssl_certificates.context_client') }}</div>
                        <div class="fw-semibold">{{ trim(($cert->client?->first_name ?? '').' '.($cert->client?->last_name ?? '')) ?: __('admin/ssl_certificates.none') }}</div>
                        <div class="text-muted small">{{ $cert->client?->email }}</div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-text small mb-1">{{ __('admin/ssl_certificates.context_common_name') }}</div>
                        <div class="fw-semibold">{{ $cert->common_name }}</div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-text small mb-1">{{ __('admin/ssl_certificates.context_product') }}</div>
                        <div class="fw-semibold">{{ $cert->clientProduct?->product?->name ?? __('admin/ssl_certificates.none') }}</div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-text small mb-1">{{ __('admin/ssl_certificates.context_module') }}</div>
                        <div class="fw-semibold">{{ $cert->module }}</div>
                    </div>
                </div>

                @if($cert->csr)
                    <div class="mb-3">
                        <label class="form-label">{{ __('admin/ssl_certificates.context_csr') }}</label>
                        <textarea class="form-control font-monospace small" rows="4" readonly>{{ $cert->csr }}</textarea>
                    </div>
                @endif

                <hr class="my-3">

                <div class="mb-3">
                    <label class="form-label">{{ __('admin/ssl_certificates.field_certificate') }} <span class="text-danger">*</span></label>
                    <textarea class="form-control font-monospace small @error('certificate') is-invalid @enderror"
                              name="certificate" rows="8"
                              placeholder="-----BEGIN CERTIFICATE-----">{{ old('certificate') }}</textarea>
                    @error('certificate') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    <div class="form-text small">{{ __('admin/ssl_certificates.field_certificate_help') }}</div>
                </div>

                <div class="mb-3">
                    <label class="form-label">{{ __('admin/ssl_certificates.field_certificate_chain') }}</label>
                    <textarea class="form-control font-monospace small @error('certificate_chain') is-invalid @enderror"
                              name="certificate_chain" rows="6"
                              placeholder="-----BEGIN CERTIFICATE-----">{{ old('certificate_chain') }}</textarea>
                    @error('certificate_chain') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    <div class="form-text small">{{ __('admin/ssl_certificates.field_certificate_chain_help') }}</div>
                </div>

                <div class="mb-3">
                    <label class="form-label">{{ __('admin/ssl_certificates.field_private_key') }}</label>
                    <textarea class="form-control font-monospace small @error('private_key') is-invalid @enderror"
                              name="private_key" rows="6"
                              placeholder="-----BEGIN PRIVATE KEY-----">{{ old('private_key') }}</textarea>
                    @error('private_key') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    <div class="form-text small">{{ __('admin/ssl_certificates.field_private_key_help') }}</div>
                </div>

                <div class="mb-3">
                    <label class="form-label">{{ __('admin/ssl_certificates.field_issuer') }}</label>
                    <input type="text" class="form-control @error('issuer') is-invalid @enderror"
                           name="issuer" value="{{ old('issuer', $cert->issuer) }}">
                    @error('issuer') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    <div class="form-text small">{{ __('admin/ssl_certificates.field_issuer_help') }}</div>
                </div>

                <x-slot:footer>
                    <x-admin.form-actions
                        :cancel-url="route('admin.ssl-certificates.index')"
                        :submit-text="__('admin/ssl_certificates.submit_install')"
                        submit-icon="fas fa-check"
                    />
                </x-slot:footer>
            </x-admin.card>
        </form>
    </div>
@endsection
